-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Peripheral.write() not sending request #794
Comments
Can you provide logs, please? |
The data related to the blocking version
non-blocking version
|
I try to add a delay, it works. But how much time should be delayed depends on devices. My old device need more delay than my new device to work properly. suspend fun readState1(): String {
val deferred = peripheral.async { state1Flow.first() }
delay(1000)
peripheral.write(customCharacteristic, byteArrayOf(/*command2*/), WriteType.WithResponse)
Timber.d("request sent")
return deferred.await()
} |
Oh, if a You should use suspend fun readState1(): String =
state1Flow.onSubscription {
peripheral.write(customCharacteristic, byteArrayOf(/*command2*/), WriteType.WithResponse)
}.first()
More details can be found here. |
I tried this but it didn't work. suspend fun readState1(): String =
state1Flow
.shareIn(peripheral, SharingStarted./*tried all of them*/)
.onSubscription {
peripheral.write(customCharacteristic, byteArrayOf(/*command2*/), WriteType.WithResponse)
}.first() I use |
For the private val incoming = state1Flow
.shareIn(peripheral, SharingStarted.Eagerly)
suspend fun readState1(): String =
incoming.onSubscription {
peripheral.write(customCharacteristic, byteArrayOf(/*command2*/), WriteType.WithResponse)
}.first() Using |
I have a device like the one below. The custom characteristic accepts write requests that allows reading, writing, subscribing and unsubscribing different data. The corresponding data is emitted through notification.
However, the
await()
inreadState1()
is blocking the coroutine scope. It seems that the request is not sent to the device. So no matching data is emitted fromstate1Flow
and the coroutine scope is stuck forever. But when I start a coroutine and collect the source data flow or its derivative outside (e.g.connect()
method orviewmodelScope
), the peripheral scope is not blocked and it runs without problem. I discover this by chance but I don't know what makes the difference.I also tried adding that line inside the method, but it is still blocking.
The text was updated successfully, but these errors were encountered: