0

My app effectively has a queue of BLE operations to perform with my peripheral. Each operation begins by establishing a connection to the peripheral, which returns Observable<RxBleConnection>. The first item in the queue initiates the connection, and the subsequent operations simply receive this (shared) RxBleConnection.

In simplied form, the queue is executed via:

Observable.concatDelayError(queuedOperations)

If the connection cannot be established, or it is dropped during one operation, the remaining queued operations each retry, which attempt to re-establish the connection.

I decided to modify the behavior such that once the connection became invalid, the queued operations should always receive the now-invalid RxBleConnection rather than re-establishing a new connection. The retry logic is still executed but fails immediately in these instances --- there are other reasons the operation can fail, which have nothing to do with the connection.

To produce this behavior, I compose a PublishSubject directly after the Observable<RxBleConnection>. This subject simply delegates the original RxBleConnection --- see code below. If the connection reaches an error state, then subsequent subscriptions to the subject will get the error; otherwise, the shared connection is returned. This is exactly the behavior I wanted, and it appears to work as designed when an error occurs; however, I am having problems now when everything succeeds.

Before the change, once all the operations in the queue were consumed, the connection was automatically released. With the addition of the PublishSubject, though, the operations are successful, but the connection remains open. Using debugging statements, I verified that the subject's onUnsubscribe and onTerminate are never called. The original RxBleConnection eventually times out --- and its onUnsubscribe and onTerminate do get called.

Was wondering what I am doing incorrectly that is causing the app to remain connected to the peripheral.

private Observable.Transformer<RxBleConnection, RxBleConnection> createConnectionSubject() {
    return rxBleConnectionObservable -> {
        final PublishSubject<RxBleConnection> subject = PublishSubject.create();

        rxBleConnectionObservable.subscribe(
            subject::onNext,
            subject::onError,
            subject::onCompleted);

        return subject;
    };
}
Steve Yohanan
  • 757
  • 5
  • 17

1 Answers1

0

The PublishSubject does not pass the unsubscription event to the upstream.

Maybe you should check out the ConnectionSharingAdapter class?

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21