fix: bt notif subscription race condition (#3645)

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
James Rich
2025-11-09 10:43:30 -06:00
committed by GitHub
parent 23db9f2443
commit f0b9a0ff75

View File

@@ -242,39 +242,27 @@ constructor(
}
@OptIn(ExperimentalUuidApi::class)
private fun setupNotifications() {
localScope.launch {
fromNumCharacteristic
?.subscribe()
?.onEach { notifyBytes ->
try {
Timber.d(
"FROMNUM notify, ${notifyBytes.size} bytes: ${
notifyBytes.joinToString(
prefix = "[",
postfix = "]",
) { b -> String.format("0x%02x", b) }
} - reading packet queue",
)
drainPacketQueueAndDispatch("notify")
} catch (ex: Exception) {
Timber.e(ex, "Error handling incoming FROMNUM notify")
}
private suspend fun setupNotifications() {
fromNumCharacteristic
?.subscribe()
?.onEach { notifyBytes ->
try {
Timber.d(
"FROMNUM notify, ${notifyBytes.size} bytes: ${
notifyBytes.joinToString(
prefix = "[",
postfix = "]",
) { b -> String.format("0x%02x", b) }
} - reading packet queue",
)
drainPacketQueueAndDispatch("notify")
} catch (ex: Exception) {
Timber.e(ex, "Error handling incoming FROMNUM notify")
}
?.catch { e -> Timber.e(e, "Error in subscribe flow for fromNumCharacteristic") }
?.onCompletion { cause -> Timber.d("fromNum subscribe flow completed, cause=$cause") }
?.launchIn(scope = localScope)
}
localScope.launch {
try {
fromNumCharacteristic?.setNotifying(true)
drainPacketQueueAndDispatch("initial")
} catch (e: Exception) {
Timber.e(e, "Failed to enable notifications or perform initial drain")
service.onDisconnect(false)
}
}
?.catch { e -> Timber.e(e, "Error in subscribe flow for fromNumCharacteristic") }
?.onCompletion { cause -> Timber.d("fromNum subscribe flow completed, cause=$cause") }
?.launchIn(scope = localScope)
service.onConnect()
}