首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用rxandroidble禁用通知?

如何使用rxandroidble禁用通知?
EN

Stack Overflow用户
提问于 2016-11-08 11:07:12
回答 2查看 1K关注 0票数 1

我目前正在尝试使用rxandroidble来取代我们的应用程序之一的Android的本地BLE API。

如何禁用通知?我能够用示例代码启用它,这个代码:

代码语言:javascript
复制
device.establishConnection(context, false)
.flatMap(rxBleConnection -> rxBleConnection.setupNotification(characteristicUuid)) 
.doOnNext(notificationObservable -> { // OK }) 
.flatMap(notificationObservable -> notificationObservable)     
.subscribe(bytes -> { // OK });

但是在我的产品中,我有一个用例,在这个用例中,我必须根据需要禁用/启用通知。

此外,我试图直接取消订阅/重新连接,而不是禁用/启用通知,但取消订阅命令显然从未执行,我的假设是,因为我的设备具有较高的吞吐量(我的设备以300 -400 it发出通知),这是否可信?

(我知道BLE并不是最适合高吞吐量的技术,但在这里是用于研发目的的:)

谢谢你的帮忙!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-08 12:03:06

每当订阅来自ObservableRxBleConnection.setupNotification()时,启用通知就会发生。若要禁用通知,必须取消对上述订阅的订阅。

有几种方法可以对其进行编码。其中之一是:

代码语言:javascript
复制
    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 */ }
            );

请注意,在上面的示例中,当对sharedConnectionObservable的最后订阅结束时,连接将被关闭。

要启用/禁用不同的特性,可以使用不同的Observable<Boolean>复制和粘贴上述代码,如启用/禁用输入和不同的UUID

票数 1
EN

Stack Overflow用户

发布于 2020-09-03 04:12:32

Kotlin

在kotlin中,使用dispose()禁用通知。

代码语言:javascript
复制
#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()

尝试取消订阅,但它没有工作,仍然不确定哪一个是最好的dispose()还是取消订阅()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40485301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档