我正在使用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);
}
);
您好,我現在面臨這個例外,現在超過2天,您的幫助是更多的讚賞。你能發送片段或代碼嗎? – prasanthMurugan