【问题标题】:Android BLE connects in background after disconnect断开连接后,Android BLE 在后台连接
【发布时间】:2017-06-19 22:28:55
【问题描述】:

我们正在使用 RxAndroidBle 1.3.1 从 Android 连接到 BLE 设备。在某些手机上(目前我在 LG V20 [LG-H918]、Android 7.0 上看到这种情况),在我们的应用断开连接后,手机将继续连接而无需任何用户输入。

当我们关闭应用时,设备会断开连接。然后我们可以杀死我们的应用程序进程。 20 秒左右后,手机重新连接到设备约 2 秒,然后断开连接。这种行为每 10-20 秒无限重复一次,直到我们重新启动手机。

实际上,如果我们在连接仍然存在时硬杀应用程序,我们甚至会获得重复连接。

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

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)

我很困惑;任何帮助表示赞赏!

【问题讨论】:

  • 你能展示你的代码和应用程​​序的日志吗?
  • 对我来说似乎是 Android 中的一个错误。
  • 用日志编辑过的帖子。代码太长且涉及,但在断开连接后,我们取消订阅Observable<RxBleConnection>
  • 罪魁祸首是 Spotify Connect。 stackoverflow.com/questions/43160195/…

标签: android bluetooth-lowenergy rxandroidble


【解决方案1】:

确保你调用了close 方法。

close method

【讨论】:

  • 我正在使用 RxAndroidBle 库;您实际上并没有致电close(),而是取消了与该连接有关的所有 Observables 的订阅。事实上,我在日志中看到了断开无线电操作,因此它似乎可以正确断开连接。
【解决方案2】:

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

连接到 GATT 服务器 与 BLE 交互的第一步 设备正在连接到它——更具体地说,连接到 GATT 设备上的服务器。要连接到 BLE 设备上的 GATT 服务器,您 使用 connectGatt() 方法。此方法接受三个参数: 上下文对象,autoConnect(布尔值,指示是否 BLE 设备一变成就自动连接 可用),以及对 BluetoothGattCallback 的引用:

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

【讨论】:

  • 如果你在 BluetoothGatt 对象上正确调用了 disconnect() ,那么无论你将 autoConnect 设置为 true 还是 false 都没有影响。
  • @Emil:我在暗示远程端堆栈上的一个潜在错误 - 由 OP 应用程序中的自动连接引起 - 所以想排除这种情况
  • 自动连接为假。如上所述,在 RxAndroidBle 中,您不能直接调用 disconnect();相反,您取消订阅对该连接感兴趣的 Rx Observables。
【解决方案3】:

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

另见Android BLE unexpectedly and repeatedly reconnects to peripheral

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 2020-05-06
    相关资源
    最近更新 更多