1

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)
Pritam
  • 339
  • 3
  • 23
  • Usually disconnection (unsubscribe from `RxBleDevice.establishConnection()`) is enough to finish communication. It seems that you want to achieve a bit different result like powering off your device via BLE command. It appears that your peripheral is powering off without properly closing the connection making the Android to timeout after the supervision timeout. Could you show your code and logs from the application? – Dariusz Seweryn Jan 23 '18 at 10:41
  • @DariuszSeweryn updated post with logs. Operation(222504075) shut downs device. It works, in spite of it never hits finished. In code, I timeout it after few second, so I avoid default 20 s timeout. On iOS thanks to different Gatt implementation this precess works properly. :) – Tomáš Havlíček Jan 23 '18 at 11:43

1 Answers1

3

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.

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
  • Thank you for your answer, and also your time. :) I thought this, however I'm glad you confirmed. Perhaps this will help somebody in future. – Tomáš Havlíček Jan 23 '18 at 12:39