From e5d6ffc4bdd016bfc34ee1126a43a7160bf0900d Mon Sep 17 00:00:00 2001 From: geeksville Date: Thu, 2 Jul 2020 08:46:25 -0700 Subject: [PATCH] fix #77, see below: root cause in log: the problem is that we are running dropAndReconnect (because of an error diduing writec) at the same time as handleSendToRadio called scheduleReconnect because of the exception fix is: now that BluetoothInterface is smart enough to do its own reconnections, do not auto reconnect in SafeBluetooth. Instead just throw a BLEException and assume the client will reconnect if it wants. --- .../com/geeksville/mesh/service/SafeBluetooth.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 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 ec3bf6dee..ae553b575 100644 --- a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt +++ b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt @@ -157,7 +157,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD if (oldstate == BluetoothProfile.STATE_CONNECTED) { info("Lost connection - aborting current work: $currentWork") - dropAndReconnect() + lostConnection("lost connection") } else if (status == 133) { // We were not previously connected and we just failed with our non-auto connection attempt. Therefore we now need // to do an autoconnection attempt. When that attempt succeeds/fails the normal callbacks will be called @@ -499,8 +499,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD } } - /// Drop our current connection and then requeue a connect as needed - private fun dropAndReconnect() { + private fun lostConnection(reason: String) { /* Supposedly this reconnect attempt happens automatically "If the connection was established through an auto connect, Android will @@ -511,7 +510,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD closeConnection() */ - failAllWork(BLEException("Lost connection")) + failAllWork(BLEException(reason)) // Cancel any notifications - because when the device comes back it might have forgotten about us notifyHandlers.clear() @@ -520,6 +519,11 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD debug("calling lostConnect handler") it.invoke() } + } + + /// Drop our current connection and then requeue a connect as needed + private fun dropAndReconnect() { + lostConnection("lost connection, reconnecting") // Queue a new connection attempt val cb = connectionCallback