I have been using RxAndroidBLE 2 only recently and I am looking for a solution where I can enable notification on a characteristic without having to call discoverServices(). In my case the call takes a lot of time (5-10sec). It is ensured that the characteristic exists. I have found several solutions on Internet. However, discoverServices() was called implicitly in each case. My implementation so far looks like...
private void onConnectionReceived(RxBleConnection rxBleConnection) {
rxBleConnection.discoverServices()
.flatMap(rxBleDeviceServices -> {
return rxBleDeviceServices.getCharacteristic(MY_UUID_RX);
})
.flatMapObservable(bluetoothGattCharacteristic -> {
BluetoothGattDescriptor cccDescriptor = bluetoothGattCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIGURATION_UUID);
Completable enableNotificationCompletable = rxBleConnection.writeDescriptor(cccDescriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Completable disableNotificationCompletable = rxBleConnection.writeDescriptor(cccDescriptor, BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE).onErrorComplete();
return rxBleConnection.setupNotification(bluetoothGattCharacteristic, NotificationSetupMode.COMPAT)
.doOnNext(notificationObservable -> notificationHasBeenSetUp())
.flatMap(notificationObservable -> notificationObservable)
.mergeWith(enableNotificationCompletable)
.doOnDispose(disableNotificationCompletable::subscribe); // fire and forget
})
.observeOn(AndroidSchedulers.from(handlerThread.getLooper()))
.subscribe(this::onNotificationReceived, this::onNotificationSetupFailure);
}
Thanks for your support !