2016-12-01 147 views
1

我正在使用rxandroidble使用autoconnect = true來連續監測傳感器中的數據。該應用程序不斷掃描它以前連接的傳感器。rxandroidble:長時間斷開設備連接:應用程序進入休眠狀態

即使手機沒有連接到電源,傳感器的數據監控和掃描也應該持續整夜。

如果晚上連接了傳感器,即使該應用暫時斷開,該應用也將保持連接整夜。但是,如果傳感器在夜間斷開連續6個小時(因爲我拉動了傳感器的電池),然後在早上重新連接傳感器電池,手機就不會重新連接到傳感器。

我每隔8秒就會不斷掃描Android服務中的新傳感器,但它不是WakefulService,也不是由WakefulIntent啓動。它應該是?

任何想法可能會發生什麼? rxandroidble設計用於在這種情況下繼續掃描傳感器(可連接設備,超出範圍8小時,然後回到範圍內)? 或者我需要在斷開連接後手動嘗試重新連接,然後rxandroid將不斷嘗試重新連接。

這裏是我的掃描碼:

public boolean scanForDevices(boolean on){ 


    if(on){ 
     if (!mBluetoothAdapter.isEnabled()) { 
      Log.e(TAG, "scanForDevices: bluetooth not enabled, scan failed"); 
      return false; // bluetooth disabled 
     } 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 

      if (TheApplication.getInstance().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       Log.e(TAG, "scanForDevices: ACCESS_COARSE_LOCATION permission not granted, scan failed"); 
       return false; // App doesn't have permission granted 
      } 
     } 

     if (isScanning()) { 
      scanSubscription.unsubscribe(); 
     } else { 

      scanSubscription = RxBleClientSingleton.getInstance().getRxBleClient().scanBleDevices() 
        .observeOn(AndroidSchedulers.mainThread()) 
        .doOnError(this::onScanFailure) 
        .doOnUnsubscribe(this::clearSubscription) 
        .subscribe(this::onScanResult, this::onScanFailure); 


      //Ensure we won't Scan forever (save battery) 
      if (EnablePeriodicScan == true) { 
       handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         if (isScanning()) 
          scanForDevices(false); 
        } 
       }, 4000); 
      } 

     } 
    }else{ 
     //Turn scanning off 
     if(isScanning()){ 
      scanSubscription.unsubscribe(); 
     } 
    } 
    return true; 
} 

這裏是我連接代碼:

public void RxBleConnect() { 



    autoConnectSetting = true; 



    if (false == sensorConnectionState.equals(SensorConnectionState.Disconnected)) { 
     Log.d(TAG, "RxBleConnect: cannot connect, #" + allowReconnect + " : " + macAddress + " isn't disconnected, currently in " + sensorConnectionState.toString()); 

     if ((sensorConnectionState.equals(SensorConnectionState.Connected)) && (allowReconnect > 15)) { /// reached our limit even though we are connected? 
      allowReconnect = 0; // reset count 
      Disconnect(); // disconnect sensor. 
      Log.d(TAG, "RxBleConnect: someone keeps knocking, appears that " + macAddress + " isn't as connected as we thought."); 
     } 
     allowReconnect ++; 

     return; // dont reattempt connection if already attempting to connect 
    } 

    Log.d(TAG, "RxBleConnect: connecting to sensor...."); 

    SystemClock.sleep(400); 


    connectionObservable = 
      bleDevice.establishConnection(TheApplication.getInstance(), autoConnectSetting) 

      .compose(new ConnectionSharingAdapter()) 
      .observeOn(AndroidSchedulers.mainThread()) 


      .doOnUnsubscribe(this::RxBleDisconnect) 
      .takeUntil(disconnectTriggerSubject) 
      .doOnError(throwable -> { 
       Log.d(TAG, "rxBleConnection doOnError: " + throwable); 
       Disconnect(); // triggerDisconnect(); 
      }); 



    connectionSubscription = connectionObservable 
      .flatMap(RxBleConnection::discoverServices) 
      .subscribe(this::RxBleOnConnectionReceived, this::RxBleOnConnectionFailure); 



} 

如果連接狀態更改爲斷開,我不嘗試手動重新連接;掃描或autoconnect應該照顧這個問題的權利?

我已經關閉「應用優化」又名打盹模式在Android的電池設置的應用程序,但我不知道,如果是做任何事情。

編輯1: 這裏是我的日誌....我們連接OK第一次,那麼我們嘗試連接,然後我們立即斷開:

>>> FIRST CONNECTION <<< 
12-09 21:23:46.904 8992-8992/appName D/PeripheralManager: RxBleConnect: connecting to sensor...., auto connect = false 
onConnectionStateChange newState=2 status=0 
12-09 21:23:09.298 8992-8992/appName D/PeripheralManager: onConnectionStateChange: new state RxBleConnectionState{CONNECTED} 
12-09 21:23:46.831 8992-9004/appName D/BluetoothGatt: onClientConnectionState() - status=8 clientIf=5 device=E0:CF:8D:98:69:6A 
12-09 21:23:46.832 8992-9004/appName D/RxBle#BluetoothGatt: onConnectionStateChange newState=0 status=8 
12-09 21:23:46.897 8992-8992/appName D/PeripheralManager: onConnectionStateChange: new state RxBleConnectionState{DISCONNECTED} 
12-09 21:23:46.897 8992-8992/appName D/Peripheral Manager: setConnectionState: Set e0:cf:8d:98:69:6a connection state to Disconnected 
12-09 21:23:46.898 8992-8992/appName E/PeripheralManager: ### sendDisconnectNotification: mid = 2698601 

>>> ATTEMPT RECONNECT HERE <<<< 
12-09 21:23:46.904 8992-8992/appName D/PeripheralManager: RxBleConnect: connecting to sensor...., auto connect = true 
12-09 21:23:46.915 8992-9004/appName D/RxBle#Radio: QUEUED RxBleRadioOperationDisconnect(250971465) 
12-09 21:23:46.916 8992-9070/appName D/RxBle#Radio: STARTED RxBleRadioOperationDisconnect(250971465) 
12-09 21:23:46.922 8992-9004/appName D/BluetoothGatt: setCharacteristicNotification() - uuid: f8c00003-159f-11e6-92f5-0002a5d5c51b enable: false 
12-09 21:23:46.928 8992-9004/appName D/RxBle#Radio: QUEUED RxBleRadioOperationDescriptorWrite(49261423) 
12-09 21:23:47.306 8992-8992/appName D/RxBle#Radio: QUEUED RxBleRadioOperationConnect(200794900) 
12-09 21:23:47.316 8992-8992/appName D/PeripheralManager: onConnectionStateChange: new state RxBleConnectionState{CONNECTING} 
12-09 21:23:47.316 8992-8992/appName D/Peripheral Manager: setConnectionState: Set e0:cf:8d:98:69:6a connection state to Connecting… 
12-09 21:23:47.340 8992-8992/appName D/BluetoothManager: getConnectionState() 
12-09 21:23:47.340 8992-8992/appName D/BluetoothManager: getConnectedDevices 
12-09 21:23:47.347 8992-9070/appName D/RxBle#Radio: FINISHED RxBleRadioOperationDisconnect(250971465) 
12-09 21:23:47.348 8992-9070/appName D/RxBle#Radio: STARTED RxBleRadioOperationDescriptorWrite(49261423) 
12-09 21:23:47.348 8992-8992/appName D/PeripheralManager: rxBleConnection doOnError: BleGattException{status=8, bleGattOperation=BleGattOperation{description='CONNECTION_STATE'}} 
12-09 21:23:47.355 8992-8992/appName D/PeripheralManager: observeConnectionStateChanges FINISHED 
12-09 21:23:47.355 8992-8992/appName D/PeripheralManager: rxBleConnection doOnError: BleGattException{status=8, bleGattOperation=BleGattOperation{description='CONNECTION_STATE'}} 

謝謝!

2日編輯:

代碼消耗

私人無效RxBleOnConnectionReceived(RxBleDeviceServices服務){// 發現服務

connectionsCount ++; 

    Log.d(TAG, "RxBleOnConnectionReceived: Discovered services"); 


    setConnectionState(SensorConnectionState.Connected); 

    try { 



    connectionSubscription = connectionObservable 
      .flatMap(rxBleConnection -> rxBleConnection.setupNotification(BluetoothLeUart.RX_UUID)) 
      .doOnError(throwable -> { 
       Log.d(TAG, "RxBleOnConnectionReceived setupNotification doOnError: " + throwable); 
       Disconnect(); 
      }) 
      .doOnNext(notificationObservable -> { 
       // Notification has been set up 
      }) 
      .flatMap(notificationObservable -> notificationObservable) // <-- Notification has been set up, now observe value changes. 
      .observeOn(AndroidSchedulers.mainThread()) 
      .doOnError(throwable -> { 
       Log.d(TAG, "RxBleOnConnectionReceived doOnError: " + throwable); 
      }) 
      .subscribe(
        bytes -> { 
         // Given characteristic has been changes, here is the value. 
        processReceivedMessage(bytes); 

        }, 
        throwable -> { 
         /*handle throwable*/ 

         Log.e(TAG, "RxBleOnConnectionReceived: processReceivedMessage: " + throwable); 
        } 
      ); 
+0

您好,我現在面臨這個例外,現在超過2天,您的幫助是更多的讚賞。你能發送片段或代碼嗎? – prasanthMurugan

回答

1

有你問幾個問題:

  • 什麼樣的服務,而手機進入休眠模式使用?

這是一個很好的問題,在stackoverflow已可用。比如這裏:How does doze mode affect background/foreground services, with/without partial/full wakelocks?還有科爾多瓦插件github上頁約瞌睡期間使用BLE的一個問題:https://github.com/thaliproject/Thali_CordovaPlugin/issues/413

  • RxAndroidBle認爲,如果設備失去連接時自動重新連接?

號連接後結束它必須再次調用RxBleDevice.establishConnection重新建立(羯羊它是使用autoconnect = true或false開始)。 從庫描述:

自動連接

(...)

自動連接概念可能第一眼會產生誤導。隨着設置爲false 自動連接標誌,如果一個BLE裝置不是廣告時 RxBleDevice#establishConnection方法被稱爲連接將結束一個錯誤 。從平臺到 平臺超時之後,發生錯誤的程度不同,但在 一般情況下,它比幾秒鐘要好幾十秒。

將自動連接標誌設置爲true允許您等到BLE 設備變爲可發現狀態。在連接完全建立之前,RxBleConnection實例將不會被髮射出 。從經驗中, 也可以處理獲取喚醒鎖,因此假設您的Android 設備在建立連接後會被喚醒 - ,但它不是一個記錄功能,並且可能會在將來的系統 版本中發生變化。

  • 威爾掃描或autoconnect = true連接需要重新連接的照顧?

很難推斷什麼是觸發scanForDevices()函數,但它看起來像它會在4秒後自動完成。如果掃描開啓並且用戶將撥打scanForDevices(true),則無論如何將停止掃描。

+0

非常感謝。那麼EstablishConnection會不斷嘗試連接,直到找到該設備?或者在快速超時後失敗?我想生病嘗試並回報,除非你有測試結果。 是的,scanForDevices在rxble初始化後調用,並且每8秒運行一次,持續時間爲4秒。但我相信,當應用程序進入深度睡眠時,它將被阻止做任何事情... – seano

+0

我編輯了答案。短篇小說:'autoconnect = false' - >如果設備不可用,則在30秒後超時,'autoconnect = true' - >超時,直到設備可用。 從這裏:https://github.com/thaliproject/Thali_CordovaPlugin/issues/413你可以閱讀關於可能的場景與瞌睡。顯然,如果服務正在運行,它並沒有太大的改變。 –

+0

再次感謝,請看下面我的回覆...仍然有困難... – seano

相關問題