From b13e52d2ebcb196105f3995484635760b9037ce6 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 2 Mar 2020 06:25:17 -0800 Subject: [PATCH] two items from todo list: * startforegroundservice only if we have a valid radio * when we select a new radio, restart the service --- TODO.md | 5 +- .../mesh/service/RadioInterfaceService.kt | 64 +++++++++++-------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/TODO.md b/TODO.md index 8e6807257..c5f2c7727 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,7 @@ # High priority Work items for soon alpha builds +* use git subtree for androidlib and the protobufs * run services in sim mode on emulator * show offline nodes as greyed out * show time since last contact on the node info card @@ -26,8 +27,6 @@ the channel is encrypted, you can share the the channel key with others by qr co * be smarter about sharing GPS location with the device (to save power), integrate with new network scheduler * stop scan when we start the service - I think this is done, but check * set the radio macaddr by using the service (not by slamming bytes into the Preferences) -* startforegroundservice only if we have a valid radio -* when we select a new radio, restart the service * show bt scan progress centered and towards the bottom of the screen * when a text arrives, move that node info card to the bottom on the window - put the text to the left of the card. with a small arrow/distance/shortname * let the user type texts somewhere @@ -159,3 +158,5 @@ Don't leave device discoverable. Don't let unpaired users do things with device * change info() log strings to debug() * have the foreground service's notification show a summary of network status "connected/disconnected, 5 of 6 nodes, nearest: kevin 5km", * have notification (individually maskable) notifications for received texts - use file:///home/kevinh/packages/android-sdk-linux/docs/reference/android/support/v4/app/NotificationCompat.BigTextStyle.html +* startforegroundservice only if we have a valid radio +* when we select a new radio, restart the service 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 7dd7b4d83..106cdaccd 100644 --- a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt @@ -123,6 +123,11 @@ class RadioInterfaceService : Service(), Logging { private val BTM_OWNER_CHARACTER = UUID.fromString("6ff1d8b6-e2de-41e3-8c0b-8fa384f64eb6") + private const val DEVADDR_KEY = "devAddr" + + /// If our service is currently running, this pointer can be used to reach it (in case setBondedDeviceAddress is called) + private var runningService: RadioInterfaceService? = null + /// This is public only so that SimRadio can bootstrap our message flow fun broadcastReceivedFromRadio(context: Context, payload: ByteArray) { val intent = Intent(RECEIVE_FROMRADIO_ACTION) @@ -133,8 +138,6 @@ class RadioInterfaceService : Service(), Logging { private fun getPrefs(context: Context) = context.getSharedPreferences("radio-prefs", Context.MODE_PRIVATE) - private const val DEVADDR_KEY = "devAddr" - /// Get our bluetooth adapter (should always succeed except on emulator private fun getBluetoothAdapter(context: Context): BluetoothAdapter? { val bluetoothManager = @@ -157,13 +160,18 @@ class RadioInterfaceService : Service(), Logging { addr } - fun setBondedDeviceAddress(context: Context, addr: String?) = + fun setBondedDeviceAddress(context: Context, addr: String?) { getPrefs(context).edit(commit = true) { if (addr == null) this.remove(DEVADDR_KEY) else putString(DEVADDR_KEY, addr) } + + runningService?.let { + it.setEnabled(addr != null) + } + } } @@ -280,12 +288,14 @@ class RadioInterfaceService : Service(), Logging { } override fun onCreate() { + runningService = this super.onCreate() setEnabled(true) } override fun onDestroy() { setEnabled(false) + runningService = null super.onDestroy() } @@ -296,31 +306,35 @@ class RadioInterfaceService : Service(), Logging { /// Open or close a bluetooth connection to our device private fun setEnabled(on: Boolean) { if (on) { - val address = getBondedDeviceAddress(this) - if (address == null) - errormsg("No bonded mesh radio, can't create service") - else { - // Note: this call does no comms, it just creates the device object (even if the - // device is off/not connected) - val device = getBluetoothAdapter(this)?.getRemoteDevice(address) - if (device != null) { - info("Creating radio interface service. device=$address") + if (safe != null) { + info("Skipping radio enable, it is already on") + } else { + val address = getBondedDeviceAddress(this) + if (address == null) + errormsg("No bonded mesh radio, can't create service") + else { + // Note: this call does no comms, it just creates the device object (even if the + // device is off/not connected) + val device = getBluetoothAdapter(this)?.getRemoteDevice(address) + if (device != null) { + info("Creating radio interface service. device=$address") - // Note this constructor also does no comm - val s = SafeBluetooth(this, device) - safe = s + // Note this constructor also does no comm + val s = SafeBluetooth(this, device) + safe = s - // FIXME, pass in true for autoconnect - so we will autoconnect whenever the radio - // 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) - } else { - errormsg("Bluetooth adapter not found, assuming running on the emulator!") + // FIXME, pass in true for autoconnect - so we will autoconnect whenever the radio + // 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) + } else { + errormsg("Bluetooth adapter not found, assuming running on the emulator!") + } + + if (logSends) + sentPacketsLog = BinaryLogFile(this, "sent_log.pb") } - - if (logSends) - sentPacketsLog = BinaryLogFile(this, "sent_log.pb") } } else { info("Closing radio interface service")