I'm working with an RXBle library for Android. In order to read from a BLE peripheral (BLE device) I end up setting up a number of subscribes, all within one another, like this:
Disposable scanSubscription = null;
public void startScan() {
scanSubscription = rxBleClient
.scanBleDevices(settings, filter)
.take(1)
.subscribe(
scanResult -> connectDevice(scanResult.getBleDevice()),
throwable -> {}
);
}
public void connectDevice(RxBleDevice device) {
device
.establishConnection(false)
.subscribe(
connection -> requestMtu(connection),
throwable -> {}
);
}
public void requestMtu(final RxBleConnection connection) {
connection
.requestMtu(185)
.subscribe(
mtu -> readCharacteristic(connection),
throwable -> {}
);
}
public void readCharacteristic(final RxBleConnection connection) {
/* this one eventually scanSubscription.dispose!!!!! */
}
Essentially, I have a series of functions that perform an action on a thing, and then subscribe to the resulting Single (I think I'm using the terminology correctly)
My question is, what is a better way to write this?
Even more importantly, what happens to the nested subscribes that I never unsubscribe from? Note that in the first function I'm calling a take(1)...eventually I just dispose the top-level disposable that is calling all of this.