-
Notifications
You must be signed in to change notification settings - Fork 436
Open
Labels
Description
I have a simple setup as follows, where I am trying to bond with a host BL device by reading encrypted characteristics. This code sometimes succeeds but fails most of the time.
class BleClient (context: Context): BleManager(context){
fun connectAdapter(): Flow<ConnectionState> = stateAsFlow().also {
connect([BluetoothDevice])
.useAutoConnect(true)
.enqueue()
}
fun bond(): Flow<BondState> =
bondingStateAsFlow().also {
readEncryptedCharacteristic()
}
private fun readEncryptedCharacteristic() {
super.readCharacteristic([ENCRYPTED_CHARACTERISTIC]).enqueue()
}
}
Usage
val bleClient = BleClient(context)
// Wait for adapter connection to be ready
bleClient.connectAdapter().first { it == ConnectionState.Ready }
// Wait until bonded
val bonded = bleClient.bond().first { it = BondState.Bonded }
Log.i(TAG, "Bond state: $bonded")
When bleClient.bond().first { it = BondState.Bonded }
is executed both host and the client displays a pairing dialog, once I click on "confirm" on both machines. The logcat shows the following logs: How is that even possible that reading encrypted characteristics fails but bond state becomes Bonded
?
com.example.app E/BleManager: onCharacteristicRead error 132
com.example.app I/BleClient: Bond state: : no.nordicsemi.android.ble.ktx.state.BondState$Bonded@d9512fe
Note: error 132
means GhattDevice is busy
philips77