2016-09-16 89 views

回答

0

RxBleConnection例如onConnectionReceived當連接成功後,如果BLE斷開連接,RxBleConnection將無法像讀取執行任何操作/寫,可以在回調被抓來確定連接失敗

但是最好的方法會去檢查連接失敗回調它的自我確定連接處於不活動

感謝 Swayam

+0

什麼回調?我不記得在庫 –

+0

這樣當你嘗試創建一個如下所示的連接: - connectionSubscription = bleDevice.establishConnection(this,autoConnectToggleSwitch.isChecked()) .subscribe(this :: onConnectionReceived,this :: onConnectionFailure);如果連接失敗,您將收到onConnectionFailure方法的回調,您可以從中確定沒有活動連接 –

0

我假設你想保持RxBleConnection開在非反應性的方式。在這種情況下,你需要自己保持連接狀態的軌跡。你可以用的輔助類做到這一點:

static class ConnectionWithState { 
    RxBleConnection connection; 
    boolean isActive; 
} 

,並建立連接時,只是做:

final ConnectionWithState connectionWithState = new ConnectionWithState(); 
final Subscription connectionSubscription = bleDevice 
     .establishConnection(this, false) 
     .subscribe(
       connection -> { 
        connectionWithState.connection = connection; 
        connectionWithState.isActive = true; 
       }, 
       throwable -> connectionWithState.isActive = false, 
       () -> connectionWithState.isActive = false 
     ); 

和斷開需要你從connectionSubscription退訂。

最好的問候