From 5822dddc6c332c87b939173f3a5cc4885f364c0e Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 26 Apr 2020 13:03:35 -0700 Subject: [PATCH] Fix #23: autobug, if we get mystery BLE error 133 while reading messages drop the BLE link and reconnect and try again. --- .../mesh/service/RadioInterfaceService.kt | 30 ++++++++++++------- .../geeksville/mesh/service/SafeBluetooth.kt | 4 ++- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt index c7e43749f..26be612c7 100644 --- a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt @@ -295,19 +295,27 @@ class RadioInterfaceService : Service(), Logging { else { val fromRadio = getCharacteristic(BTM_FROMRADIO_CHARACTER) safe!!.asyncReadCharacteristic(fromRadio) { - val b = it.getOrThrow() - .value.clone() // We clone the array just in case, I'm not sure if they keep reusing the array + try { + val b = it.getOrThrow() + .value.clone() // We clone the array just in case, I'm not sure if they keep reusing the array - if (b.isNotEmpty()) { - debug("Received ${b.size} bytes from radio") - handleFromRadio(b) + if (b.isNotEmpty()) { + debug("Received ${b.size} bytes from radio") + handleFromRadio(b) - // Queue up another read, until we run out of packets - doReadFromRadio(firstRead) - } else { - debug("Done reading from radio, fromradio is empty") - if (firstRead) // If we just finished our initial download, now we want to start listening for notifies - startWatchingFromNum() + // Queue up another read, until we run out of packets + doReadFromRadio(firstRead) + } else { + debug("Done reading from radio, fromradio is empty") + if (firstRead) // If we just finished our initial download, now we want to start listening for notifies + startWatchingFromNum() + } + } catch (ex: BLEException) { + errormsg( + "error during doReadFromRadio", + ex + ) + serviceScope.handledLaunch { retryDueToException() } } } } diff --git a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt index b00774a0e..8dd81830e 100644 --- a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt +++ b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt @@ -418,7 +418,9 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD cb: (Result) -> Unit, lostConnectCb: () -> Unit ) { - logAssert(workQueue.isEmpty() && currentWork == null) // I don't think anything should be able to sneak in front + logAssert(workQueue.isEmpty()) + logAssert(currentWork == null) // I don't think anything should be able to sneak in front + lostConnectCallback = lostConnectCb connectionCallback = if (autoConnect) cb