2016-11-08 74 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