0

I'm working on a bluetooth project that uses RxAndroidBle for bluetooth communication. I came across two different ways that the subscriptions are cleaned up. I was wondering if anyone could explain the differences and benefits of each if there are any. The two examples are as follows.

First: using a PublishSubject to trigger a disconnect with the bluetooth device

Code Sample: https://github.com/Polidea/RxAndroidBle/blob/master/sample/src/main/java/com/polidea/rxandroidble/sample/example4_characteristic/CharacteristicOperationExampleActivity.java

Second: disconnecting with the bluetooth device by unsubscribing from the Subscription

Code Sample: https://github.com/Polidea/RxAndroidBle/blob/master/sample/src/main/java/com/polidea/rxandroidble/sample/example2_connection/ConnectionExampleActivity.java

My main focus is on the triggerDisconnect() methods in each of the examples. In what ways is the PublishSubject way different from just keeping a reference to the Subscription and then unsubscribing?

I do apologize for how open ended this question is but I'm not sure how better to explain it.

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
David Carek
  • 1,103
  • 1
  • 12
  • 26

2 Answers2

1

No, PublishSubject disconnectTriggerSubject help construct connectionObservable using this prepareConnectionObservable() method earlier. Then act as a proxy to pass null to onNext() method of the observable.

Then inside onNext() of this subscription:

.subscribe(
      characteristic -> {
           updateUI(characteristic);
           Log.i(getClass().getSimpleName(), "Hey, connection has been established!");
      },
      this::onConnectionFailure,
      this::onConnectionFinished
);

the updateUI(characteristic) method is get called with null value.

nhoxbypass
  • 9,695
  • 11
  • 48
  • 71
1

I think I figured it out. The .takeUntil(disconnectTriggerSubject) is the key to understanding the disconnectTriggerSubject. takeUntil() means that items emitted from the connection observable (after subscribing of course) will be emitted until an item is emitted from disconnectTriggerSubject. Since disconnectTriggerSubject is a PublishSubject it is both an observer and observable. Due to it being an observable it can emit items through the onNext() method. So, calling disconnectTriggerSubject.onNext(null) causes takeUntil(disconnectTriggerSubject) to execute, which prevents any new items from the connectionObservable to be emitted. This is essentially the same as unsubscribe since it "Stops the receipt of notifications on the Subscriber that was registered when this Subscription was received." http://reactivex.io/RxJava/javadoc/rx/Subscription.html

David Carek
  • 1,103
  • 1
  • 12
  • 26