BLE software update works again

This commit is contained in:
geeksville
2020-02-24 18:10:25 -08:00
parent 601aeb83d7
commit 674b417c87
4 changed files with 50 additions and 35 deletions

View File

@@ -153,7 +153,7 @@ class MeshService : Service(), Logging {
if (fusedLocationClient == null) {
val request = LocationRequest.create().apply {
interval =
10 * 1000 // FIXME, do more like once every 5 mins while we are connected to our radio _and_ someone else is in the mesh
5 * 60 * 1000 // FIXME, do more like once every 5 mins while we are connected to our radio _and_ someone else is in the mesh
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
@@ -186,7 +186,6 @@ class MeshService : Service(), Logging {
val client = LocationServices.getFusedLocationProviderClient(this)
// FIXME - should we use Looper.myLooper() in the third param per https://github.com/android/location-samples/blob/432d3b72b8c058f220416958b444274ddd186abd/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms/location/sample/locationupdatesforegroundservice/LocationUpdatesService.java
client.requestLocationUpdates(request, locationCallback, null)

View File

@@ -275,42 +275,11 @@ class RadioInterfaceService : Service(), Logging {
override fun onCreate() {
super.onCreate()
// FIXME, let user GUI select which device we are talking to
val address = getBondedDeviceAddress(this)
if (address == null)
error("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
// 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 {
error("Bluetooth adapter not found, assuming running on the emulator!")
}
if (logSends)
sentPacketsLog = BinaryLogFile(this, "sent_log.pb")
}
setEnabled(true)
}
override fun onDestroy() {
info("Destroying radio interface service")
if (logSends)
sentPacketsLog.close()
safe?.close()
setEnabled(false)
super.onDestroy()
}
@@ -318,6 +287,44 @@ class RadioInterfaceService : Service(), Logging {
return binder;
}
/// Open or close a bluetooth connection to our device
private fun setEnabled(on: Boolean) {
if (on) {
val address = getBondedDeviceAddress(this)
if (address == null)
error("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
// 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 {
error("Bluetooth adapter not found, assuming running on the emulator!")
}
if (logSends)
sentPacketsLog = BinaryLogFile(this, "sent_log.pb")
}
} else {
info("Closing radio interface service")
if (logSends)
sentPacketsLog.close()
safe?.close()
safe = null
}
}
/**
* do a synchronous write operation
*/
@@ -379,6 +386,10 @@ class RadioInterfaceService : Service(), Logging {
}
private val binder = object : IRadioInterfaceService.Stub() {
override fun enableLink(enable: Boolean) = toRemoteExceptions {
setEnabled(enable)
}
// A write of any size to nodeinfo means restart reading
override fun restartNodeInfo() = doWrite(BTM_NODEINFO_CHARACTER, ByteArray(0))