Kotlin-BLE-Library
Kotlin-BLE-Library copied to clipboard
Bug with connection
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()
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
Hi, I'm refactoring the library. A lot of things will change. I'll make sure that connect behaves as in the documentation.
any update on this? tried following the documentation outlined in the README and experiencing the same exact thing. @philips77 Thanks for all your help.