Lilly
12/02/2020, 2:19 PMprivate suspend fun sendPacketWithAcknowledgement(packet: RequestPacket) {
try {
withTimeout(PACKET_CONFIRMATION_TIMEOUT) {
sendPacket(packet.bytes)
}
// Wait here until ack packet received
// mutex.lock() // I thought this will suspend the code here and on mutex.unlock() it will resume here
} catch (e: TimeoutCancellationException) {
// do sth.
}
}
On caller site:
sendPacketWithAcknowledgement(StartCommunication()) // should be sequential
isCommunicationEstablished = true
The caller code should behave sequential, so that isCommunicationEstablished = true
is only processed, when the ack packet is received and the suspension is resumed manually. I thought mutex would fit here but I guess I didn't use it the right away. It's a bluetooth classic scenario btw. So does someone know how I can achieve this behavior?okarm
12/02/2020, 8:55 PMsuspendCoroutine
function to convert callback-based code to suspending code.
https://elizarov.medium.com/callbacks-and-kotlin-flows-2b53aa2525cfLilly
12/03/2020, 2:59 AM