我正在使用rxandroidble使用autoconnect = true来持续监控来自传感器的数据。该应用程序会持续扫描以前连接的传感器。
即使手机未连接电源,传感器的数据监控和扫描也应持续整夜。
如果在晚上连接传感器,应用程序将保持整夜连接,即使它暂时断开连接。
但是,如果传感器在夜间断开6小时(因为我拉动了传感器的电池),然后我在早上重新连接传感器电池,手机似乎没有重新连接到传感器。
我经常在Android服务中每8秒扫描一次新传感器,但它不是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);
}
如果连接状态变为断开连接,我不会尝试手动重新连接;扫描或自动连接应该照顾这个问题吗?
我已关闭"应用优化"在Android电池设置中应用程序的称为打盹模式,但我不知道这是否正在做任何事情。
编辑1: 这是我的日志....我们第一次连接好,然后我们尝试连接,然后我们立即断开连接:
>>> 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'}}
谢谢!
第二次编辑:
消费代码
private void RxBleOnConnectionReceived(RxBleDeviceServices services){ //发现服务
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 :(得分:1)
您提出了几个问题:
这是一个关于stackoverflow的好问题。例如:How does doze mode affect background/foreground services, with/without partial/full wakelocks? Cordova Plugin github页面中还存在一个关于在打瞌睡期间使用BLE的问题:https://github.com/thaliproject/Thali_CordovaPlugin/issues/413
RxAndroidBle
是否应该自动重新连接?没有。连接结束后(使用autoconnect
= true或false启动),必须再次调用RxBleDevice.establishConnection
重新建立连接。
从图书馆描述:
自动连接
(...)
乍一看,自动连接概念可能会产生误导。随着 autoconnect标志设置为false,连接将以错误结束 如果一个BLE设备没有广告时 调用RxBleDevice#establishConnection方法。从平台到 平台超时后发出的错误不同,但在 一般来说,它比单秒钟还要几十秒。
将自动连接标志设置为true可让您等到BLE 设备变得可被发现。 RxBleConnection实例不会 发射直到连接完全建立。从经验来看也是如此 处理获取唤醒锁定,因此可以安全地假设你的Android 连接建立后,设备将被唤醒 - 但它不是一个记录的功能,可能会在未来的系统中发生变化 版本。
autoconnect = true
连接是否需要重新连接?很难推断触发scanForDevices()
功能的内容,但看起来它会在4秒后自动完成。如果扫描已开启且用户将呼叫scanForDevices(true)
,则无论如何都会停止扫描。