Search code examples
androidbluetooth-lowenergyrxandroidble

RxAndroidBLE setupNotification misses first notification


The ble firmware I connect to immediately sends the current values upon notification subscription.

For example, subscribing to the Battery Level notify characteristic will return 50% when subscribed to, and continue to notify whenever changes occur.

This works well with sample ble apps and the value is received, processed, and displayed after the subscription, but not with rxandroidble.

I am using the following to setup the notification. It works for subsequent notifications but does not appear to receive the initial notification as if it is still setting up the notification:

Disposable notifyDisposable = connectionObservable
                    .flatMap(rxBleConnection -> rxBleConnection.setupNotification(characteristic))
                    .doOnNext(notificationObservable -> L.d(TAG, "notification setup for: " + characteristic.toString()))
                    .flatMap(notificationObservable -> notificationObservable)
                    .subscribe(
                            data -> onNotificationReceived(data, notifyEvent),
                            this::onNotificationSetupFailure
                    );

Is there a way to have the setupNotification prepared before it writes the flag to subscribe, so that it can immediately handle the data received?


Solution

  • Yes, there is an API for changing the behaviour of notification setup: RxBleConnection.setupNotification(BluetoothGattCharacteristic/UUID, NotificationSetupMode).

    In your case simply use:

    rxBleConnection.setupNotification(characteristic, NotificationSetupMode.QUICK_SETUP);