From d282c7911e167831dbb426be4a49d7c9606928b4 Mon Sep 17 00:00:00 2001 From: geeksville Date: Thu, 11 Jun 2020 16:32:33 -0700 Subject: [PATCH] immediately fail bluetooth links if we get a read or write error --- .../mesh/service/BluetoothInterface.kt | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt b/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt index 16d7fe453..73d2c70c8 100644 --- a/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt +++ b/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt @@ -210,28 +210,34 @@ class BluetoothInterface(val service: RadioInterfaceService, val address: String /// Send a packet/command out the radio link override fun handleSendToRadio(a: ByteArray) { - safe?.let { s -> - val uuid = BTM_TORADIO_CHARACTER - debug("queuing ${a.size} bytes to $uuid") + try { + safe?.let { s -> + val uuid = BTM_TORADIO_CHARACTER + debug("queuing ${a.size} bytes to $uuid") - // Note: we generate a new characteristic each time, because we are about to - // change the data and we want the data stored in the closure - val toRadio = getCharacteristic(uuid) - toRadio.value = a + // Note: we generate a new characteristic each time, because we are about to + // change the data and we want the data stored in the closure + val toRadio = getCharacteristic(uuid) + toRadio.value = a - s.asyncWriteCharacteristic(toRadio) { r -> - try { - r.getOrThrow() - debug("write of ${a.size} bytes completed") + s.asyncWriteCharacteristic(toRadio) { r -> + try { + r.getOrThrow() + debug("write of ${a.size} bytes completed") - if (isFirstSend) { - isFirstSend = false - doReadFromRadio(false) + if (isFirstSend) { + isFirstSend = false + doReadFromRadio(false) + } + } catch (ex: Exception) { + warn("error during asyncWriteCharacteristic - disconnecting, ${ex.message}") + service.serviceScope.handledLaunch { retryDueToException() } } - } catch (ex: Exception) { - errormsg("Ignoring sendToRadio exception: $ex") } } + } catch (ex: BLEException) { + warn("error during handleSendToRadio - disconnecting, ${ex.message}") + service.serviceScope.handledLaunch { retryDueToException() } } } @@ -257,9 +263,7 @@ class BluetoothInterface(val service: RadioInterfaceService, val address: String startWatchingFromNum() } } catch (ex: BLEException) { - warn( - "error during doReadFromRadio - disconnecting, ${ex.message}" - ) + warn("error during doReadFromRadio - disconnecting, ${ex.message}") service.serviceScope.handledLaunch { retryDueToException() } } }