How to disable a notification with rxandroidble?

自古美人都是妖i 提交于 2021-01-28 23:49:53

问题


I'm currently trying to use rxandroidble in order to replace the native BLE API of Android of one of our app.

How to disable a notification? I'm able to enable it with the sample code, this one:

device.establishConnection(context, false)
.flatMap(rxBleConnection -> rxBleConnection.setupNotification(characteristicUuid)) 
.doOnNext(notificationObservable -> { // OK }) 
.flatMap(notificationObservable -> notificationObservable)     
.subscribe(bytes -> { // OK });

But in my product I have a use case where I have to disable / enable the notification(s) on demand.

Plus, I tried to directly unsubscribe / reconnect instead of disable / enable the notification but the unsubscribe command is never executed apparently, my hypothesis is because I have a high throughput (my device notifies at 300 - 400Hz), is it plausible?

(I know that BLE is not the most appropriate technology for high throughput but it's for R&D purpose here :) )

Thanks for your help!


回答1:


Enabling notifications happens whenever the Observable from RxBleConnection.setupNotification() will be subscribed. To disable the notification one must unsubscribe from the above subscription.

There are several ways in which it can be coded. One of them is:

    final RxBleDevice rxBleDevice = // your RxBleDevice
    final Observable<RxBleConnection> sharedConnectionObservable = rxBleDevice.establishConnection(this, false).share();

    final Observable<Boolean> firstNotificationStateObservable = // an observable that will emit true when notification should be enabled and false when disabled
    final UUID firstNotificationUuid = // first of the needed UUIDs to enable / disable
    final Subscription subscription = firstNotificationStateObservable
            .distinctUntilChanged() // to be sure that we won't get more than one enable commands
            .filter(enabled -> enabled) // whenever it will emit true
            .flatMap(enabled -> sharedConnectionObservable // we take the shared connection
                    .flatMap(rxBleConnection -> rxBleConnection.setupNotification(firstNotificationUuid)) // enable the notification
                    .flatMap(notificationObservable -> notificationObservable) // and take the bytes
                    .takeUntil(firstNotificationStateObservable.filter(enabled1 -> !enabled1)) // and we are subscribing to this Observable until we want to disable - note that only the observable from sharedConnectionObservable will be unsubscribed
            )
            .subscribe(
                    notificationBytes -> { /* handle the bytes */ },
                    throwable -> { /* handle exception */ }
            );

Note that in the above example the connection will be closed whenever the last subscription to sharedConnectionObservable will end.

To enable / disable different characteristics you can copy and paste the above code with different Observable<Boolean> as enable / disable inputs and different UUID's.




回答2:


Kotlin

In kotlin, Use dispose() to disable the notification.

#ENABLE NOTIFICATION
val result = device.establishConnection(false)
.flatMap(rxBleConnection -> rxBleConnection.setupNotification(characteristicUuid))
.doOnNext(notificationObservable -> {
    // Notification has been set up
})
.flatMap(notificationObservable -> notificationObservable) // <-- Notification has been set up, now observe value changes.
.subscribe(
    bytes -> {
        // Given characteristic has been changes, here is the value.
    },
    throwable -> {
        // Handle an error here.
    }
);


#DISABLE NOTIFICATION
result.dispose()

Tried to unsubscribe but it didn't work, still not sure which one is best dispose() or unsubscribe()



来源:https://stackoverflow.com/questions/40485301/how-to-disable-a-notification-with-rxandroidble

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!