Rapid connect/disconnect sequence fails to properly disconnect from BLE device
See original GitHub issueSummary
Rapid connect/disconnect sequence fails to properly disconnect from BLE device.
When creating subscription to RxBleDevice.establishConnection() and cancelling it prior to connection being established, the physical BLE device remains in connected state (verified through JLink debugger output). This only happens when unsubscribe is called prior to fully establishing connection. If delay is inserted between subscribe/unsubscribe calls, then connect and disconnect sequence works properly.
Library version
implementation “com.polidea.rxandroidble:rxandroidble:1.6.0”
Preconditions
- Unsubscribe has to be called prior to connection being established.
Steps to reproduce actual result
- Create subscription to rxbledevice.establishConnection()
- Wait 0.2s
- Call subscription.unsubscribe()
- On Android: app log shows connection and disconnect operations completing successfully.
- On BLE Device: JLink debugger output shows that device is still connected.
Minimum code snippet reproducing the issue
Logs from the application running with setting RxBleLog.setLogLevel(RxBleLog.VERBOSE)
09-18 12:48:54.670 17038-17173/com.livindi.resident I/RxBle#CancellableSubscription: Scan operation is requested to stop.
09-18 12:48:54.673 17038-17173/com.livindi.resident D/BluetoothAdapter: isLeEnabled(): ON
--------- beginning of system
09-18 12:48:54.702 17038-17096/com.livindi.resident D/RxBle#ClientOperationQueue: QUEUED ScanOperationApi21(185464561)
09-18 12:48:54.703 17038-17092/com.livindi.resident D/RxBle#ClientOperationQueue: STARTED ScanOperationApi21(185464561)
09-18 12:48:54.704 17038-17173/com.livindi.resident I/RxBle#QueueOperation: Scan operation is requested to start.
09-18 12:48:54.706 17038-17173/com.livindi.resident D/BluetoothAdapter: isLeEnabled(): ON
09-18 12:48:54.709 17038-17051/com.livindi.resident D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=7 mScannerId=0
09-18 12:48:54.715 17038-17092/com.livindi.resident D/RxBle#ClientOperationQueue: FINISHED ScanOperationApi21(185464561) in 12 ms
09-18 12:48:54.733 17038-17173/com.livindi.resident I/RxBle#CancellableSubscription: Scan operation is requested to stop.
09-18 12:48:54.734 17038-17173/com.livindi.resident D/BluetoothAdapter: isLeEnabled(): ON
09-18 12:48:54.885 17038-17197/com.livindi.resident D/RxBle#ClientOperationQueue: QUEUED ConnectOperation(237633332)
09-18 12:48:54.885 17038-17092/com.livindi.resident D/RxBle#ClientOperationQueue: STARTED ConnectOperation(237633332)
09-18 12:48:54.888 17038-17173/com.livindi.resident V/RxBle#BleConnectionCompat: Connecting without reflection
09-18 12:48:54.889 17038-17173/com.livindi.resident D/BluetoothGatt: connect() - device: DC:C5:05:C6:9E:0E, auto: false
registerApp()
registerApp() - UUID=bf9c3cc7-0ee0-4e7d-8221-6ec65bab636e
09-18 12:48:54.891 17038-17051/com.livindi.resident D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
09-18 12:48:55.555 17038-17095/com.livindi.resident D/RxBle#ClientOperationQueue: QUEUED ScanOperationApi21(167195673)
09-18 12:48:55.911 17038-17197/com.livindi.resident D/RxBle#ClientOperationQueue: QUEUED DisconnectOperation(217206097)
09-18 12:48:55.912 17038-17197/com.livindi.resident I/RxBle#ConnectionOperationQueue: Connection operations queue to be terminated (DC:C5:05:C6:9E:0E)
09-18 12:48:55.913 17038-17196/com.livindi.resident D/RxBle#Executors$RunnableAdapter: Terminated.
09-18 12:48:55.915 17038-17092/com.livindi.resident D/RxBle#ClientOperationQueue: FINISHED ConnectOperation(237633332) in 1029 ms
09-18 12:48:55.916 17038-17092/com.livindi.resident D/RxBle#ClientOperationQueue: STARTED ScanOperationApi21(167195673)
09-18 12:48:55.918 17038-17173/com.livindi.resident I/RxBle#QueueOperation: Scan operation is requested to start.
09-18 12:48:55.920 17038-17173/com.livindi.resident D/BluetoothAdapter: isLeEnabled(): ON
09-18 12:48:55.925 17038-17051/com.livindi.resident D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=8 mScannerId=0
09-18 12:48:55.930 17038-17092/com.livindi.resident D/RxBle#ClientOperationQueue: FINISHED ScanOperationApi21(167195673) in 14 ms
09-18 12:48:55.932 17038-17092/com.livindi.resident D/RxBle#ClientOperationQueue: STARTED DisconnectOperation(217206097)
09-18 12:48:55.933 17038-17173/com.livindi.resident D/BluetoothManager: getConnectionState()
getConnectedDevices
09-18 12:48:55.939 17038-17173/com.livindi.resident D/BluetoothGatt: close()
unregisterApp() - mClientIf=7
09-18 12:48:55.941 17038-17092/com.livindi.resident D/RxBle#ClientOperationQueue: FINISHED DisconnectOperation(217206097) in 10 ms
Actual result
BLE Device indicates it is still in connected state and never receives DISCONNECTED event. INFO [main] on_ble_evt: CONNECTED
Expected result
BLE Device receives DISCONNECTED event INFO [ble] on_ble_evt: DISCONNECTED. Reason: 0x13
[Edit @dariuszseweryn] Notes:
Related bug on Google’s Issue Tracker: https://issuetracker.google.com/issues/37121223
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:24 (6 by maintainers)
Top GitHub Comments
@aptly-io, another case is described here. There is no question of reconnection. But you are right that a delay is required when reconnecting, otherwise we will get a connection error.
@digitalstreamio @DariuszAniszewski @AnDrOiD73dd This makes me think about my case #530 . Maybe try adding a step waiting for the connection state to become
DISCONNECTED
, only then allow to reopen? It is more deterministic, then next you can still at adelay(1000)
… what I also might do …