From 89258646b0cbf3817da497011e5489faab9c233f Mon Sep 17 00:00:00 2001 From: geeksville Date: Wed, 17 Jun 2020 13:59:32 -0700 Subject: [PATCH 1/2] Fix autobug if service goes away while we are changing devices --- .../com/geeksville/mesh/ui/SettingsFragment.kt | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt index 9b04376a4..9f1eadf5b 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt @@ -16,6 +16,7 @@ import android.hardware.usb.UsbDevice import android.hardware.usb.UsbManager import android.os.Bundle import android.os.ParcelUuid +import android.os.RemoteException import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -429,10 +430,16 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging { /// Change to a new macaddr selection, updating GUI and radio fun changeScanSelection(context: MainActivity, newAddr: String) { - info("Changing device to ${newAddr.anonymize}") - selectedAddress = newAddr - changeDeviceSelection(context, newAddr) - devices.value = devices.value // Force a GUI update + try { + info("Changing device to ${newAddr.anonymize}") + changeDeviceSelection(context, newAddr) + selectedAddress = + newAddr // do this after changeDeviceSelection, so if it throws the change will be discarded + devices.value = devices.value // Force a GUI update + } catch (ex: RemoteException) { + errormsg("Failed talking to service, probably it is shutting down $ex.message") + // ignore the failure and the GUI won't be updating anyways + } } } From 73de2c33c2ae36a7e904c5c7dddb1963459f3413 Mon Sep 17 00:00:00 2001 From: geeksville Date: Wed, 17 Jun 2020 14:02:58 -0700 Subject: [PATCH 2/2] fix autobug if old work items run after gatt is destroyed --- .../main/java/com/geeksville/mesh/service/SafeBluetooth.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 f3cb6caf3..669323f31 100644 --- a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt +++ b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt @@ -608,7 +608,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD cont: Continuation ) = queueWork("reqMtu", cont, 5 * 1000) { isSettingMtu = true - gatt!!.requestMtu(len) + gatt?.requestMtu(len) ?: false } fun asyncRequestMtu( @@ -629,7 +629,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD ) = queueWork("writeC ${c.uuid}", cont) { currentReliableWrite = null c.value = v - gatt!!.writeCharacteristic(c) + gatt?.writeCharacteristic(c) ?: false } fun asyncWriteCharacteristic( @@ -653,7 +653,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD ) = queueWork("rwriteC ${c.uuid}", cont) { logAssert(gatt!!.beginReliableWrite()) currentReliableWrite = c.value.clone() - gatt!!.writeCharacteristic(c) + gatt?.writeCharacteristic(c) ?: false } /* fun asyncWriteReliable(