Android BLE 在断开连接后在后台连接

Android BLE connects in background after disconnect

我们正在使用 RxAndroidBle 1.3.1 从 Android 连接到 BLE 设备。在某些 phone 上(目前我在 LG V20 [LG-H918],Android 7.0 上看到这个),在我们的应用程序断开连接后,phone 将继续连接而没有任何用户输入。

当我们关闭我们的应用程序时,设备会断开连接。然后我们可以杀死我们的应用程序进程。大约二十秒后,phone 重新连接到设备约 2 秒,然后断开连接。此行为无限期地每 10-20 秒重复一次,直到我们重新启动 phone.

实际上,如果我们在连接仍然存在的情况下硬终止应用程序,我们甚至可以获得重复连接。

这些是我们断开连接时的日志:

06-21 10:51:07.464 26640-26640/com.hatchbaby.rest.qa I/Nightlight: [main] Disconnecting: Nightlight.disconnect()
06-21 10:51:07.528 26640-26640/com.hatchbaby.rest.qa D/BluetoothGatt: setCharacteristicNotification() - uuid: 02240003-5efd-47eb-9c1a-de53f7a2b232 enable: false
06-21 10:51:07.538 26640-26640/com.hatchbaby.rest.qa D/RxBle#Radio:   QUEUED RxBleRadioOperationDescriptorWrite(131621266)
06-21 10:51:07.539 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio:  STARTED RxBleRadioOperationDescriptorWrite(131621266)
06-21 10:51:07.543 26640-26640/com.hatchbaby.rest.qa D/BluetoothGatt: setCharacteristicNotification() - uuid: 02260002-5efd-47eb-9c1a-de53f7a2b232 enable: false
06-21 10:51:07.547 26640-26640/com.hatchbaby.rest.qa D/RxBle#Radio:   QUEUED RxBleRadioOperationDescriptorWrite(250764313)
06-21 10:51:07.548 26640-26640/com.hatchbaby.rest.qa D/RxBle#Radio:   QUEUED RxBleRadioOperationDisconnect(160892126)
06-21 10:51:07.562 26640-26640/com.hatchbaby.rest.qa I/Nightlight: [main] Nightlight state: RxBleConnectionState{DISCONNECTED}
06-21 10:51:07.686 26640-26747/com.hatchbaby.rest.qa D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0
06-21 10:51:07.688 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(131621266)
06-21 10:51:07.689 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio:  STARTED RxBleRadioOperationDescriptorWrite(250764313)
06-21 10:51:07.783 26640-26746/com.hatchbaby.rest.qa D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0
06-21 10:51:07.786 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(250764313)
06-21 10:51:07.787 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio:  STARTED RxBleRadioOperationDisconnect(160892126)
06-21 10:51:07.787 26640-26640/com.hatchbaby.rest.qa D/BluetoothManager: getConnectionState()
06-21 10:51:07.787 26640-26640/com.hatchbaby.rest.qa D/BluetoothManager: getConnectedDevices
06-21 10:51:07.793 26640-26640/com.hatchbaby.rest.qa D/BluetoothGatt: cancelOpen() - device: E5:D6:FC:68:FB:55
06-21 10:51:07.797 26640-26653/com.hatchbaby.rest.qa D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=E5:D6:FC:68:FB:55
06-21 10:51:07.797 26640-26653/com.hatchbaby.rest.qa D/RxBle#BluetoothGatt: onConnectionStateChange newState=0 status=0
06-21 10:51:07.803 26640-26640/com.hatchbaby.rest.qa D/BluetoothGatt: close()
06-21 10:51:07.803 26640-26640/com.hatchbaby.rest.qa D/BluetoothGatt: unregisterApp() - mClientIf=6
06-21 10:51:07.807 26640-26690/com.hatchbaby.rest.qa D/RxBle#Radio: FINISHED RxBleRadioOperationDisconnect(160892126)

我很困惑;感谢您的帮助!

确保当您从您的应用程序连接 Gatt 配置文件时,您已将自动连接标志设置为 false 来自 Android documentation 用于 BLE:-

Connecting to a GATT Server The first step in interacting with a BLE device is connecting to it— more specifically, connecting to the GATT server on the device. To connect to a GATT server on a BLE device, you use the connectGatt() method. This method takes three parameters: a Context object, autoConnect (boolean indicating whether to automatically connect to the BLE device as soon as it becomes available), and a reference to a BluetoothGattCallback:

mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

确保调用了 close 方法。

close method

罪魁祸首是 Spotify Connect。它监视任何断开连接的蓝牙设备并立即尝试连接到它。有时它会进入无限循环,有时连接循环最终会停止。但是我们能够通过停止 SC 摆脱这种破坏性的行为,并且我们能够通过安装它在其他手机上实现它。

另见