mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
start mesh service on boot, store device macaddr in prefs
This commit is contained in:
parent
55d4d769f8
commit
4e6d1be954
8 changed files with 105 additions and 53 deletions
|
|
@ -3,12 +3,13 @@ package com.geeksville.mesh.service
|
|||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.geeksville.android.Logging
|
||||
|
||||
|
||||
class BootCompleteReceiver : BroadcastReceiver() {
|
||||
class BootCompleteReceiver : BroadcastReceiver(), Logging {
|
||||
override fun onReceive(mContext: Context, intent: Intent) {
|
||||
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
|
||||
// FIXME - start listening for bluetooth messages from our device
|
||||
}
|
||||
// FIXME - start listening for bluetooth messages from our device
|
||||
info("Received boot complete announcement, starting mesh service")
|
||||
MeshService.startService(mContext)
|
||||
}
|
||||
}
|
||||
|
|
@ -33,7 +33,7 @@ class RadioNotConnectedException() : Exception("Can't find radio")
|
|||
*/
|
||||
class MeshService : Service(), Logging {
|
||||
|
||||
companion object {
|
||||
companion object : Logging {
|
||||
|
||||
/// Intents broadcast by MeshService
|
||||
const val ACTION_RECEIVED_DATA = "$prefix.RECEIVED_DATA"
|
||||
|
|
@ -50,7 +50,38 @@ class MeshService : Service(), Logging {
|
|||
/// If the radio hasn't yet joined a mesh (i.e. no nodenum assigned)
|
||||
private const val NODE_NUM_NO_MESH = -1
|
||||
|
||||
/// Helper function to start running our service, returns the intent used to reach it
|
||||
/// or null if the service could not be started (no bluetooth or no bonded device set)
|
||||
fun startService(context: Context): Intent? {
|
||||
if (RadioInterfaceService.getBondedDeviceAddress(context) == null) {
|
||||
warn("No mesh radio is bonded, not starting service")
|
||||
return null
|
||||
} else {
|
||||
// bind to our service using the same mechanism an external client would use (for testing coverage)
|
||||
// The following would work for us, but not external users
|
||||
//val intent = Intent(this, MeshService::class.java)
|
||||
//intent.action = IMeshService::class.java.name
|
||||
val intent = Intent()
|
||||
intent.setClassName(
|
||||
"com.geeksville.mesh",
|
||||
"com.geeksville.mesh.service.MeshService"
|
||||
)
|
||||
|
||||
// Before binding we want to explicitly create - so the service stays alive forever (so it can keep
|
||||
// listening for the bluetooth packets arriving from the radio. And when they arrive forward them
|
||||
// to Signal or whatever.
|
||||
|
||||
logAssert(
|
||||
(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
context.startForegroundService(intent)
|
||||
} else {
|
||||
context.startService(intent)
|
||||
}) != null
|
||||
)
|
||||
|
||||
return intent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A mapping of receiver class name to package name - used for explicit broadcasts
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import android.bluetooth.BluetoothManager
|
|||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.IBinder
|
||||
import androidx.core.content.edit
|
||||
import com.geeksville.android.BinaryLogFile
|
||||
import com.geeksville.android.Logging
|
||||
import com.geeksville.concurrent.DeferredExecution
|
||||
|
|
@ -126,6 +127,15 @@ class RadioInterfaceService : Service(), Logging {
|
|||
intent.putExtra(EXTRA_PAYLOAD, payload)
|
||||
context.sendBroadcast(intent)
|
||||
}
|
||||
|
||||
private fun getPrefs(context: Context) =
|
||||
context.getSharedPreferences("radio-prefs", Context.MODE_PRIVATE)
|
||||
|
||||
/// Return the device we are configured to use, or null for none
|
||||
fun getBondedDeviceAddress(context: Context) = getPrefs(context).getString("devAddr", null)
|
||||
|
||||
fun setBondedDeviceAddress(context: Context, addr: String) =
|
||||
getPrefs(context).edit(commit = true) { putString("devAddr", addr) }
|
||||
}
|
||||
|
||||
private val bluetoothAdapter: BluetoothAdapter? by lazy(LazyThreadSafetyMode.NONE) {
|
||||
|
|
@ -244,30 +254,32 @@ class RadioInterfaceService : Service(), Logging {
|
|||
|
||||
// FIXME, let user GUI select which device we are talking to
|
||||
|
||||
// Note: this call does no comms, it just creates the device object (even if the
|
||||
// device is off/not connected)
|
||||
val usetbeam = false
|
||||
val address = if (usetbeam) "B4:E6:2D:EA:32:B7" else "24:6F:28:96:C9:2A"
|
||||
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 = bluetoothAdapter?.getRemoteDevice(address)
|
||||
if (device != null) {
|
||||
info("Creating radio interface service. device=$address")
|
||||
|
||||
val device = bluetoothAdapter?.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 {
|
||||
error("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 {
|
||||
error("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")
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue