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.

Callback onConnectionStateChange after disconnect can put BleManager in inconsistent state

See original GitHub issue

I am seeing a situation where onConnectionStateChangeSafe in BleManager gets called with newState == BluetoothProfile.STATE_CONNECTED after I do a disconnect (call BleManager.disconnect()).

This causes mConnected to be set to true, but mBluetoothDevice is null. This makes both connect do nothing (since mConnected is true) and disconnect to nothing (since mBluetoothDevice is null), so the BleManager is no longer of much use.

Some more rough details on what I’m doing:

  1. Connecting (calling BleManager.connect()).
  2. Enabling notifications for a couple of characteristics. Reading and writing some characteristics.
  3. Disconnecting.
  4. Repeat.

This is not done in quick succession - between the disconnect and the next connect for instance there is a pause several seconds long.

This works fine most of the time, but sometimes, there is a call to onConnectionStateChangeSafe after the disconnect, which causes the problem described above.

Side note: The purpose for this is to implement a communications protocol where characteristics are used to transfer data, and the error handling is specified as disconnecting, reconnecting, and starting over.

I have been using version 2.0-beta10.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
al-brococommented, Nov 8, 2018

I have now, the fix seems to have solved the problem. I have run the same test with the new code 50 times, no issues, connection works beautifully. Thanks again!

0reactions
philips77commented, Nov 7, 2018

By any chance, did you try the version from referenced PR?

Read more comments on GitHub >

github_iconTop Results From Across the Web

onConnectionStateChange returns status22 - Stack Overflow
When was called disconnect(), my cellphone get more connections with the module, however after reconnecting sometimes, the callback onConnectionStateChange ...
Read more >
Ble Android Onconnectionstatechange Not Being Called
Callback onConnectionStateChange after disconnect can put BleManager in inconsistent but the lib should not get stack when it's called. of devices.
Read more >
android.bluetooth.BluetoothGattCallback Example
The disconnection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.
Read more >
Connection establishment fails with GATT ERROR 133 ...
I use BLE SDK 5.40. When I try connecting my device with nRF mobile app, it disconnects immediately throwing Error 133 - GATT...
Read more >
Making Android BLE work — part 2 - Medium
Connection state changes. After calling connectGatt() the stack will let you know the result on the onConnectionStateChange callback.
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