question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rapid connect/disconnect sequence fails to properly disconnect from BLE device

See original GitHub issue

Summary

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

  1. Create subscription to rxbledevice.establishConnection()
  2. Wait 0.2s
  3. Call subscription.unsubscribe()
  4. On Android: app log shows connection and disconnect operations completing successfully.
  5. 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:open
  • Created 5 years ago
  • Reactions:2
  • Comments:24 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
AnDrOiD73ddcommented, Jan 18, 2019

@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.

1reaction
aptly-iocommented, Jan 17, 2019

@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 a delay(1000) … what I also might do …

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rapid connect/disconnect sequence fails to properly ... - GitHub
Summary Rapid connect/disconnect sequence fails to properly disconnect from BLE device. When creating subscription to RxBleDevice.
Read more >
Disconnect right after connect And Connect takes long time
The effect comes more often when I connect / disconnect / connect in a quick sequence. 2) Connection takes very long to be...
Read more >
BLE device randomly disconnecting - Stack Overflow
I'm using a BLE device and connecting it to an via using swift. When I turn it on it'll connect, disconnect, then reconnect....
Read more >
Solved: BLE disconnects immediately after connection
If you are not getting any kind of error, the most common reason for an immediate disconnect is a timing mismatch between the...
Read more >
Android 13: Unable to reconnect reliably to bonded peripheral
Connect to the BLE peripheral and discover all services. ... onAclChange: state=DISCONNECTED, device=XX:XX:XX:XX:FA:34 [CONTEXT service_id=49 ] 2022-08-16 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found