JuulLabs / able

Able: Android Bluetooth Low Energy library
Apache License 2.0
159 stars 11 forks source link

Expose backing BroadcastChannel for characteristic changes #65

Closed twyatt closed 4 years ago

twyatt commented 4 years ago

Flow's onStart is called before subscribing to underlying data stream. When writing to a characteristic and expecting to receive the response via a characteristic changed event, if the Flow is slow to subscribe then the response could be lost.

Problem is described/discussed in Kotlin/kotlinx.coroutines#1758 and will be fixed with Kotlin/kotlinx.coroutines#2034.

The workaround is to use openSubscription().consumeAsFlow() which opens the subscription prior to spinning up the Flow (and in turn will be subscribed before the Flow's onStart).

twyatt commented 4 years ago

https://github.com/Kotlin/kotlinx.coroutines/issues/1758#issuecomment-631577424 provides an elegant/idiomatic means of ensuring we don't drop responses when using Flow (along with async).