From 8bf60c76034cae6defb10e30bc0f08e4c20b627c Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 20 Apr 2020 11:10:53 -0700 Subject: [PATCH] make BLE exceptions strongly typed, so I can trigger closing down connections if needed --- .../java/com/geeksville/mesh/service/SafeBluetooth.kt | 8 +++++--- 1 file changed, 5 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 aab1abfd9..529c0f938 100644 --- a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt +++ b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt @@ -51,6 +51,8 @@ class BluetoothStateReceiver(val onChanged: (Boolean) -> Unit) : BroadcastReceiv class SafeBluetooth(private val context: Context, private val device: BluetoothDevice) : Logging, Closeable { + class BLEException(msg: String) : IOException(msg) + /// Timeout before we declare a bluetooth operation failed var timeoutMsec = 30 * 1000L @@ -178,7 +180,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD closeConnection() */ - failAllWork(IOException("Lost connection")) + failAllWork(BLEException("Lost connection")) // Cancel any notifications - because when the device comes back it might have forgotten about us notifyHandlers.clear() @@ -338,7 +340,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD debug("work ${work.tag} is completed, resuming status=$status, res=$res") if (status != 0) - work.completion.resumeWithException(IOException("Bluetooth status=$status while doing ${work.tag}")) + work.completion.resumeWithException(BLEException("Bluetooth status=$status while doing ${work.tag}")) else work.completion.resume(Result.success(res) as Result) } @@ -492,7 +494,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD private fun closeConnection() { - failAllWork(IOException("Connection closing")) + failAllWork(BLEException("Connection closing")) if (gatt != null) { info("Closing our GATT connection")