Kotlin-BLE-Library icon indicating copy to clipboard operation
Kotlin-BLE-Library copied to clipboard

Bug with connection

Open MkhytarMkhoian opened this issue 1 year ago • 3 comments

In the tutorial, you say that the connect function is suspended and waits until device is in connected state, but it's not true. I've got the exception STATE_DISCONNECTED when calling discoverServices right after connect. To solve this problem, I use callbacks instead of relying on the described behavior above.

This happened because BLE asynchronous itself under the hood. Could we fix API or make clear documentation?

    // Connect a Bluetooth LE device. This is a suspend function that waits until device is in connected state.
    val connection = blinkyDevice.connect(context) // blinkyDevice from scanner

    // Discover services on the Bluetooth LE Device. This is a suspend function which waits until device discovery is finished.
    val services = connection.discoverServices()

MkhytarMkhoian avatar Feb 12 '24 10:02 MkhytarMkhoian

I've encountered the same problem, solved it using a suspendCancellableCoroutine() and resolving the future once the connection state changes to CONNECTED.

However, I feel a suspending connect function is not a good design, as there is no proper way to close the gatt without its reference, see #135

0x7061 avatar Mar 25 '24 14:03 0x7061

Hi, I'm refactoring the library. A lot of things will change. I'll make sure that connect behaves as in the documentation.

philips77 avatar Mar 26 '24 11:03 philips77

any update on this? tried following the documentation outlined in the README and experiencing the same exact thing. @philips77 Thanks for all your help.

duranaustin avatar Aug 20 '24 18:08 duranaustin