diff --git a/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl b/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl index 3535185c..61daff67 100644 --- a/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl +++ b/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl @@ -62,7 +62,7 @@ interface IMeshService { /// If a macaddress we will try to talk to our device, if null we will be idle. /// Any current connection will be dropped (even if the device address is the same) before reconnecting. /// Users should not call this directly, only used internally by the MeshUtil activity - void setDeviceAddress(String deviceAddr); + boolean setDeviceAddress(String deviceAddr); /// Get basic device hardware info about our connected radio. Will never return NULL. Will throw /// RemoteException if no my node info is available diff --git a/app/src/main/aidl/com/geeksville/mesh/IRadioInterfaceService.aidl b/app/src/main/aidl/com/geeksville/mesh/IRadioInterfaceService.aidl index 8485dfbc..6bd32d13 100644 --- a/app/src/main/aidl/com/geeksville/mesh/IRadioInterfaceService.aidl +++ b/app/src/main/aidl/com/geeksville/mesh/IRadioInterfaceService.aidl @@ -14,5 +14,5 @@ interface IRadioInterfaceService { /// If a macaddress we will try to talk to our device, if null we will be idle. /// Any current connection will be dropped (even if the device address is the same) before reconnecting. /// Users should not call this directly, called only by MeshService - void setDeviceAddress(String deviceAddr); + boolean setDeviceAddress(String deviceAddr); } 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 51d30f5f..1b4de88e 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -608,6 +608,7 @@ class MeshService : Service(), Logging { * discard entire node db & message state - used when downloading a new db from the device */ private fun discardNodeDB() { + debug("Discarding NodeDB") myNodeInfo = null nodeDBbyNodeNum.clear() nodeDBbyID.clear() @@ -1508,8 +1509,12 @@ class MeshService : Service(), Logging { override fun setDeviceAddress(deviceAddr: String?) = toRemoteExceptions { debug("Passing through device change to radio service: $deviceAddr") - discardNodeDB() - radio.service.setDeviceAddress(deviceAddr) + + val res = radio.service.setDeviceAddress(deviceAddr) + if (res) { + discardNodeDB() + } + res } // Note: bound methods don't get properly exception caught/logged, so do that with a wrapper 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 1d79db20..c9bb0652 100644 --- a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt @@ -250,38 +250,49 @@ class RadioInterfaceService : Service(), Logging { } + /** + * Change to a new device + * + * @return true if the device changed, false if no change + */ @SuppressLint("NewApi") - private fun setBondedDeviceAddress(address: String?) { - // Record that this use has configured a radio - GeeksvilleApplication.analytics.track( - "mesh_bond" - ) + private fun setBondedDeviceAddress(address: String?): Boolean { + return if (getBondedDeviceAddress(this) == address && isConnected) { + warn("Ignoring setBondedDevice $address, because we are already using that device") + false + } else { + // Record that this use has configured a new radio + GeeksvilleApplication.analytics.track( + "mesh_bond" + ) - // Ignore any errors that happen while closing old device - ignoreException { - stopInterface() + // Ignore any errors that happen while closing old device + ignoreException { + stopInterface() + } + + // The device address "n" can be used to mean none + + debug("Setting bonded device to $address") + + getPrefs(this).edit(commit = true) { + this.remove(DEVADDR_KEY_OLD) // remove any old version of the key + + if (address == null) + this.remove(DEVADDR_KEY) + else + putString(DEVADDR_KEY, address) + } + + // Force the service to reconnect + startInterface() + true } - - // The device address "n" can be used to mean none - - debug("Setting bonded device to $address") - - getPrefs(this).edit(commit = true) { - this.remove(DEVADDR_KEY_OLD) // remove any old version of the key - - if (address == null) - this.remove(DEVADDR_KEY) - else - putString(DEVADDR_KEY, address) - } - - // Force the service to reconnect - startInterface() } private val binder = object : IRadioInterfaceService.Stub() { - override fun setDeviceAddress(deviceAddr: String?) = toRemoteExceptions { + override fun setDeviceAddress(deviceAddr: String?): Boolean = toRemoteExceptions { setBondedDeviceAddress(deviceAddr) }