diff --git a/app/src/main/java/com/geeksville/mesh/service/Constants.kt b/app/src/main/java/com/geeksville/mesh/service/Constants.kt index e5c74a535..53d2594fc 100644 --- a/app/src/main/java/com/geeksville/mesh/service/Constants.kt +++ b/app/src/main/java/com/geeksville/mesh/service/Constants.kt @@ -10,7 +10,8 @@ const val prefix = "com.geeksville.mesh" // a bool true means now connected, false means not const val EXTRA_CONNECTED = "$prefix.Connected" +/// a bool true means we expect this condition to continue until, false means device might come back +const val EXTRA_PERMANENT = "$prefix.Permanent" + const val EXTRA_PAYLOAD = "$prefix.Payload" -const val EXTRA_SENDER = "$prefix.Sender" const val EXTRA_NODEINFO = "$prefix.NodeInfo" -const val EXTRA_TYP = "$prefix.Typ" diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt index 26eee917b..4ec6b3d4b 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -480,7 +480,7 @@ class MeshService : Service(), Logging { try { getPrefs().getString("json", null)?.let { asString -> discardNodeDB() // Get rid of any old state - + val json = Json(JsonConfiguration.Default) val settings = json.parse(SavedSettings.serializer(), asString) myNodeInfo = settings.myInfo @@ -995,11 +995,14 @@ class MeshService : Service(), Logging { when (intent.action) { RadioInterfaceService.RADIO_CONNECTED_ACTION -> { try { + val connected = intent.getBooleanExtra(EXTRA_CONNECTED, false) + val permanent = intent.getBooleanExtra(EXTRA_PERMANENT, false) onConnectionChanged( - if (intent.getBooleanExtra(EXTRA_CONNECTED, false)) - ConnectionState.CONNECTED - else - ConnectionState.DEVICE_SLEEP + when { + connected -> ConnectionState.CONNECTED + permanent -> ConnectionState.DISCONNECTED + else -> ConnectionState.DEVICE_SLEEP + } ) } catch (ex: RemoteException) { // This can happen sometimes (especially if the device is slowly dying due to killing power, don't report to crashlytics 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 ca480ee99..5e929b2a9 100644 --- a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt @@ -228,10 +228,11 @@ class RadioInterfaceService : Service(), Logging { /// we have completed our initial connection private val clientOperations = DeferredExecution() - private fun broadcastConnectionChanged(isConnected: Boolean) { + private fun broadcastConnectionChanged(isConnected: Boolean, isPermanent: Boolean) { debug("Broadcasting connection=$isConnected") val intent = Intent(RADIO_CONNECTED_ACTION) intent.putExtra(EXTRA_CONNECTED, isConnected) + intent.putExtra(EXTRA_PERMANENT, isPermanent) sendBroadcast(intent) } @@ -329,8 +330,8 @@ class RadioInterfaceService : Service(), Logging { } - private fun onDisconnect() { - broadcastConnectionChanged(false) + private fun onDisconnect(isPermanent: Boolean) { + broadcastConnectionChanged(false, isPermanent) isConnected = false } @@ -385,7 +386,7 @@ class RadioInterfaceService : Service(), Logging { } // Now tell clients they can (finally use the api) - broadcastConnectionChanged(true) + broadcastConnectionChanged(true, isPermanent = false) // Immediately broadcast any queued packets sitting on the device doReadFromRadio() @@ -435,7 +436,9 @@ class RadioInterfaceService : Service(), Logging { // comes in range (even if we made this connect call long ago when we got powered on) // see https://stackoverflow.com/questions/40156699/which-correct-flag-of-autoconnect-in-connectgatt-of-ble for // more info - s.asyncConnect(true, ::onConnect, ::onDisconnect) + s.asyncConnect(true, + cb = ::onConnect, + lostConnectCb = { onDisconnect(isPermanent = false) }) } else { errormsg("Bluetooth adapter not found, assuming running on the emulator!") } @@ -447,13 +450,18 @@ class RadioInterfaceService : Service(), Logging { } } } else { - info("Closing radio interface service") - if (logSends) - sentPacketsLog.close() - if (logReceives) - receivedPacketsLog.close() - safe?.close() - safe = null + if (safe != null) { + info("Closing radio interface service") + if (logSends) + sentPacketsLog.close() + if (logReceives) + receivedPacketsLog.close() + safe?.close() + safe = null + onDisconnect(isPermanent = true) // Tell any clients we are now offline + } else { + debug("Radio was not connected, skipping disable") + } } }