Search code examples
javaandroidbluetoothrx-androidrxandroidble

What is the purpose of using a PublishSubject to trigger a disconnect rather than just unsubscribing from a Subscription in RxAndroid?


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.


Solution

  • 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.