Android 与 BLE 设备完成蓝牙通信

Finishing Bluetooth communication from Android with BLE device

完成 带有外围测量设备的 Android 设备的 BLE 通信的正确方法是什么?

当前状态:

  1. if I'm satisfied with received; write stop measuring on characteristic
  2. write power off request
  3. unsubscribe RxBleConnection
  4. close and disconnect BluetoothGatt

当前解决方案的问题是,即使我断开连接并关闭 GATT,它也会在已经关闭的设备上保持连接 30 秒。 (下次测量不能立即开始)

更新日志:

D/RxBle#ConnectionOperationQueue:   QUEUED CharacteristicWriteOperation(222504075)
D/RxBle#ConnectionOperationQueue:  STARTED CharacteristicWriteOperation(222504075)
I/RxBle#ConnectionOperationQueue: Connection operations queue to be terminated (80:EA:CA:00:00:10)
D/RxBle#ClientOperationQueue:   QUEUED DisconnectOperation(90705849)
D/RxBle#ClientOperationQueue:  STARTED DisconnectOperation(90705849)
D/BluetoothManager: getConnectionState()
D/BluetoothManager: getConnectedDevices
D/BluetoothGatt: cancelOpen() - device: 80:EA:CA:00:00:10
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=80:EA:CA:00:00:10
D/RxBle#BluetoothGatt: onConnectionStateChange newState=0 status=0
D/BluetoothGatt: setCharacteristicNotification() - uuid: 3f3e3d3c-3b3a-3938-3736-353433323130 enable: false
E/TestConnection: changeNotificationDisconnected from 80:EA:CA:00:00:10
D/RxBle#ConnectionOperationQueue: FINISHED CharacteristicWriteOperation(222504075)
D/RxBle#Executors$RunnableAdapter: Terminated.
D/BluetoothGatt: close()
D/BluetoothGatt: unregisterApp() - mClientIf=6
D/RxBle#ClientOperationQueue: FINISHED DisconnectOperation(90705849)

你的方法很好

问题似乎出在您的外围设备上。它似乎没有发送任何它正在关闭的信息。如果在外设端正确实施,BluetoothGattCallback 应该接收 .onConnectionStateChange()status=19 其中 corresponds to:

#define GATT_CONN_TERMINATE_PEER_USER       HCI_ERR_PEER_USER               /* 0x13 connection terminate by peer user  */

因为情况并非如此,Android BLE 堆栈认为通信已停止,它等待监督超时启动。默认情况下 Supervision Timeout is hardcoded 20 seconds

虽然可以减少监督超时,但需要由外围设备协商,因此必须对固件进行干预。如果这是必须的,那么正确关闭连接也是一种选择。