0

I'm trying to get a subscription to automatically unsubscribe when it emits an item. The base observable is created like this.

public static Observable<RxBleConnection> setupConnection(RxBleDevice device, PublishSubject<Void> disconnectTrigger) {
    return device
            .establishConnection(false)
            .takeUntil(disconnectTrigger)
            .retry(3)
            .retryWhen(o -> o.delay(RETRY_DELAY, TimeUnit.MILLISECONDS))
            .compose(new ConnectionSharingAdapter());
}

Then I try to combine three read operations into a ProgramModel.

private void readCharacteristics(Action1<ProgramModel> onReadSuccess) {
    mConnectionObservable
            .flatMap(rxBleConnection ->
                    // combines the following three observables into a single observable that is
                    // emitted in onNext of the subscribe
                    Observable.combineLatest(
                            rxBleConnection.readCharacteristic(UUID_SERIAL_NUMBER),
                            rxBleConnection.readCharacteristic(UUID_MACHINE_TYPE),
                            rxBleConnection.readCharacteristic(UUID_CHARACTERISTIC),
                            ProgramModel::new))
            .observeOn(AndroidSchedulers.mainThread())
            .take(1)
            .subscribe(programModel -> {
                programModel.trimSerial();
                onReadSuccess.call(programModel);
            }, BleUtil::logError);
}

So theoretically once a program model is comes through oNext of the subscribe, the subscription will be unsubscribed from. For some reason the operation gets stuck and onNext and onError are never called. If I remove the take(1) this works fine but I don't want to have to deal with holding onto a reference to the subscription and unsubscribing manually. Does anyone know what I'm doing wrong or why onNext is not being called?

David Carek
  • 1,103
  • 1
  • 12
  • 26
  • What is `ConnectionSharingAdapter`. Do the individual `readCharacteristics` produce any items? Put `doOnNext` at various places to see where events disappear. – akarnokd Dec 07 '17 at 15:09
  • The connection sharing adapter is part of `RxAndroidBle`. Since every time subscribe is called a connection to the device is created, so the connection sharing adapter basically makes sure only one is active at a time. I'll try the `doOnNext` – David Carek Dec 07 '17 at 15:12

1 Answers1

0

I needed to call take(1) before the flatMap as well as after. This post sort of explains it Read multiple characteristics from an Android device using library RxAndroidBle

David Carek
  • 1,103
  • 1
  • 12
  • 26
  • From my knowledge of `RxJava` this should not be the case and the code from original post should work as expected—one of subscribe actions should get called. Could you add `RxBleLog.setLogLevel(RxBleLog.VERBOSE)` and more logging if the part where you create `Observable.combineLatest` is called? There must be some cause of the behaviour you are experiencing. – Dariusz Seweryn Dec 07 '17 at 16:59