https://kotlinlang.org logo
Title
f

Francis Mariano

07/14/2022, 7:24 PM
Hello everyone. How are you?? Please, can you give me some idea about the following trouble???
override suspend fun dataOad(characteristic: Characteristic) : Flow<ByteArray> {
    return peripheral.value.observe(characteristic)
        .onStart { Log.debug { "AudioProcessorImpl - dataOad - onStart" } }
        .onEach { Log.debug { "AudioProcessorImpl - dataOad - onEach" } }
        .onCompletion { Log.debug { "AudioProcessorImpl - dataOad - onCompletion" } }
}
If I cancel the scope that calls dataOad and create it again the observe works fine. But if the peripheral close the connection and I reconnect, the observer does not work anymore 😞 It is necessary close the application and reopen it.
t

travis

07/14/2022, 11:29 PM
Are you using the same
Peripheral
object (the
peripheral
that is referenced in your example) to reconnect? i.e. you’re not creating a new
Peripheral
when you reconnect, right?
f

Francis Mariano

07/15/2022, 1:34 PM
Hello Travis. Yes, it is the same object.
I think I know what is happening , but dunno is my error or bug in kable or expected behavior in kable. When I call the observe for first time the library sets the characteristic notification :
2022-07-15 10:43:31.483 29445-29445/com.me.stxbt D/Kable/Peripheral: 54:6C:0E:35:75:17 setCharacteristicNotification
    service: 00000427-0000-1000-8000-00805f9b34fb
    characteristic: 00000430-0000-1000-8000-00805f9b34fb
    value: true
2022-07-15 10:43:31.486 29445-29445/com.me.stxbt D/Kable/Peripheral: 54:6C:0E:35:75:17 write
    service: 00000427-0000-1000-8000-00805f9b34fb
    characteristic: 00000430-0000-1000-8000-00805f9b34fb
    descriptor: 00002902-0000-1000-8000-00805f9b34fb
2022-07-15 10:43:31.535 29445-29739/com.me.stxbt D/Kable/Callback: 54:6C:0E:35:75:17 onDescriptorWrite
    service: 00000427-0000-1000-8000-00805f9b34fb
    characteristic: 00000430-0000-1000-8000-00805f9b34fb
    descriptor: 00002902-0000-1000-8000-00805f9b34fb
    status: GATT_SUCCESS(0)
When I cancel the scope :
2022-07-15 10:44:33.759 29445-29445/com.me.stxbt D/Kable/Peripheral: 54:6C:0E:35:75:17 write
    service: 00000427-0000-1000-8000-00805f9b34fb
    characteristic: 00000430-0000-1000-8000-00805f9b34fb
    descriptor: 00002902-0000-1000-8000-00805f9b34fb
The characteristic notification is not disabled. when I recreate the scope and calling the observe again, the setCharacteristicNotification is not executed, but I received the
onCharacteristicChanged
because it was not disabled previously. But if the peripheral (hardware) close the connection (peripheral is turn off) its characteristic notification is disabled. So when I reconnect and calling the observe again, the the setCharacteristicNotification is not executed, in this case I do not receive the
onCharacteristicChanged
.
I have some news. As workaround I will keep always alive the scope of the observer. When the scope keep alive the kable library always calls setCharacteristicNotification and write when observe a characteristic again even if the peripheral (hardware) is turned off. So can be a problem with library? Steps for reproduction: 1 - Observe a characteristic. 2- Cancel the scope of observation. 3- Turn off the peripheral (hardware) and turn on it. 4- Reconnect with the peripheral. 5- Observe the characteristic again.
t

travis

07/17/2022, 7:14 AM
Thanks for sharing your workaround! Unfortunately I’m swamped with another project at the moment, so don’t have a lot of time to work on Kable (in the near term). I hope to get back to Kable development after the current project I’m on wraps up. In the mean time, I’ve created https://github.com/JuulLabs/kable/issues/358 to track this issue. Thanks again!
f

Francis Mariano

07/18/2022, 7:31 PM
Okay Travis. Thanks for your support.