properly mark when we are disconnected

This commit is contained in:
geeksville 2020-06-14 16:43:36 -07:00
parent e94c059acb
commit 7cca69be0d
3 changed files with 17 additions and 4 deletions

View file

@ -326,6 +326,7 @@ class BluetoothInterface(val service: RadioInterfaceService, val address: String
ignoreException {
s.closeConnection()
}
service.onDisconnect(false) // assume we will fail
delay(1000) // Give some nasty time for buggy BLE stacks to shutdown (500ms was not enough)
reconnectJob = null // Any new reconnect requests after this will be allowed to run
warn("Attempting reconnect")
@ -364,7 +365,7 @@ class BluetoothInterface(val service: RadioInterfaceService, val address: String
isFirstSend = true
// Now tell clients they can (finally use the api)
service.broadcastConnectionChanged(true, isPermanent = false)
service.onConnect()
// Immediately broadcast any queued packets sitting on the device
doReadFromRadio(true)

View file

@ -135,7 +135,7 @@ class RadioInterfaceService : Service(), Logging {
startInterface() // If bluetooth just got turned on, try to restart our ble link
}
fun broadcastConnectionChanged(isConnected: Boolean, isPermanent: Boolean) {
private fun broadcastConnectionChanged(isConnected: Boolean, isPermanent: Boolean) {
debug("Broadcasting connection=$isConnected")
val intent = Intent(RADIO_CONNECTED_ACTION)
intent.putExtra(EXTRA_CONNECTED, isConnected)
@ -163,8 +163,20 @@ class RadioInterfaceService : Service(), Logging {
)
}
private var isConnected = false
fun onConnect() {
if (!isConnected) {
isConnected = true
broadcastConnectionChanged(true, false)
}
}
fun onDisconnect(isPermanent: Boolean) {
broadcastConnectionChanged(false, isPermanent)
if (isConnected) {
isConnected = false
broadcastConnectionChanged(false, isPermanent)
}
}

View file

@ -161,7 +161,7 @@ class SerialInterface(private val service: RadioInterfaceService, val address: S
io.writeAsync(wakeBytes)
// Now tell clients they can (finally use the api)
service.broadcastConnectionChanged(true, isPermanent = false)
service.onConnect()
}
} else {
errormsg("Can't find device")