Finishing Bluetooth communication from Android with BLE device

What is the proper way to finish BLE communication from an Android device with peripheral measuring device?

Current state:

  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

Problem with current solution is, that even if I disconnect and close GATT, It keeps the connection for another 30s on already off the device. (Next measuring cannot start immediately)

Update logs:

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)

Upvotes: 1

Views: 1598

Answers (1)

Dariusz Seweryn
Dariusz Seweryn

Reputation: 3222

Your approach is good.

The problem seems to be with your peripheral. It seems not to send any information that it is going off. If implemented properly on the peripheral side, the BluetoothGattCallback should receive .onConnectionStateChange() with status=19 which corresponds to:

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

Because it is not the case the Android BLE stack thinks that the communication has stalled it waits for the Supervision Timeout to kick in. By default the Supervision Timeout is hardcoded 20 seconds.

While it is possible to decrease the Supervision Timeout it needs to be negotiated by the peripheral so a intervention in the firmware is a must. And if that is a must then a proper closing of the connection is also an option.

Upvotes: 3

Related Questions