2016-11-08 32 views
1

我目前正在尝试使用rxandroidble来取代我们应用程序之一的Android原生BLE API。如何禁用rxandroidble的通知?

如何禁用通知?我能够与样本代码,使这一个:

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

但在我的产品我有,我有禁用/启用对需求的通知(S)的用例。

另外,我试图直接取消订阅/重新连接,而不是禁用/启用通知,但取消订阅命令永远不会执行显然,我的假设是因为我有一个高吞吐量(我的设备通知在300 - 400Hz),是它合理?

(我知道BLE不高通量最合适的技术,但它是对R & d目的在这里:))

感谢您的帮助!

回答

1

只要从RxBleConnection.setupNotification()订阅Observable,就会发出启用通知。要禁用通知,必须退订上述订阅。

有几种编码方式。其中之一是:

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的复制和粘贴上面的代码。

+0

完美,取消订阅它效果很好!谢谢! – Myx