mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-05-12 08:42:01 -04:00
feat: Add disconnect broadcast and improve app port handling (#4502)
Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
@@ -16,12 +16,24 @@
|
||||
*/
|
||||
package com.geeksville.mesh.service
|
||||
|
||||
import org.meshtastic.core.api.MeshtasticIntent
|
||||
|
||||
const val PREFIX = "com.geeksville.mesh"
|
||||
|
||||
const val ACTION_NODE_CHANGE = "$PREFIX.NODE_CHANGE"
|
||||
const val ACTION_MESH_CONNECTED = "$PREFIX.MESH_CONNECTED"
|
||||
const val ACTION_CONNECTION_CHANGED = "$PREFIX.CONNECTION_CHANGED"
|
||||
const val ACTION_MESSAGE_STATUS = "$PREFIX.MESSAGE_STATUS"
|
||||
const val ACTION_NODE_CHANGE = MeshtasticIntent.ACTION_NODE_CHANGE
|
||||
const val ACTION_MESH_CONNECTED = MeshtasticIntent.ACTION_MESH_CONNECTED
|
||||
const val ACTION_MESH_DISCONNECTED = MeshtasticIntent.ACTION_MESH_DISCONNECTED
|
||||
const val ACTION_CONNECTION_CHANGED = MeshtasticIntent.ACTION_CONNECTION_CHANGED
|
||||
const val ACTION_MESSAGE_STATUS = MeshtasticIntent.ACTION_MESSAGE_STATUS
|
||||
|
||||
const val ACTION_RECEIVED_TEXT_MESSAGE_APP = MeshtasticIntent.ACTION_RECEIVED_TEXT_MESSAGE_APP
|
||||
const val ACTION_RECEIVED_POSITION_APP = MeshtasticIntent.ACTION_RECEIVED_POSITION_APP
|
||||
const val ACTION_RECEIVED_NODEINFO_APP = MeshtasticIntent.ACTION_RECEIVED_NODEINFO_APP
|
||||
const val ACTION_RECEIVED_TELEMETRY_APP = MeshtasticIntent.ACTION_RECEIVED_TELEMETRY_APP
|
||||
const val ACTION_RECEIVED_ATAK_PLUGIN = MeshtasticIntent.ACTION_RECEIVED_ATAK_PLUGIN
|
||||
const val ACTION_RECEIVED_ATAK_FORWARDER = MeshtasticIntent.ACTION_RECEIVED_ATAK_FORWARDER
|
||||
const val ACTION_RECEIVED_DETECTION_SENSOR_APP = MeshtasticIntent.ACTION_RECEIVED_DETECTION_SENSOR_APP
|
||||
const val ACTION_RECEIVED_PRIVATE_APP = MeshtasticIntent.ACTION_RECEIVED_PRIVATE_APP
|
||||
|
||||
fun actionReceived(portNum: String) = "$PREFIX.RECEIVED.$portNum"
|
||||
|
||||
@@ -29,14 +41,11 @@ fun actionReceived(portNum: String) = "$PREFIX.RECEIVED.$portNum"
|
||||
// standard EXTRA bundle definitions
|
||||
//
|
||||
|
||||
// a bool true means now connected, false means not
|
||||
const val EXTRA_CONNECTED = "$PREFIX.Connected"
|
||||
const val EXTRA_CONNECTED = MeshtasticIntent.EXTRA_CONNECTED
|
||||
const val EXTRA_PROGRESS = "$PREFIX.Progress"
|
||||
|
||||
// / a bool true means we expect this condition to continue until, false means device might come back
|
||||
const val EXTRA_PERMANENT = "$PREFIX.Permanent"
|
||||
|
||||
const val EXTRA_PAYLOAD = "$PREFIX.Payload"
|
||||
const val EXTRA_NODEINFO = "$PREFIX.NodeInfo"
|
||||
const val EXTRA_PACKET_ID = "$PREFIX.PacketId"
|
||||
const val EXTRA_STATUS = "$PREFIX.Status"
|
||||
const val EXTRA_PAYLOAD = MeshtasticIntent.EXTRA_PAYLOAD
|
||||
const val EXTRA_NODEINFO = MeshtasticIntent.EXTRA_NODEINFO
|
||||
const val EXTRA_PACKET_ID = MeshtasticIntent.EXTRA_PACKET_ID
|
||||
const val EXTRA_STATUS = MeshtasticIntent.EXTRA_STATUS
|
||||
|
||||
@@ -118,7 +118,7 @@ constructor(
|
||||
}
|
||||
|
||||
private fun onConnectionChanged(c: ConnectionState) {
|
||||
if (connectionStateHolder.connectionState.value == c && c !is ConnectionState.Connected) return
|
||||
if (connectionStateHolder.connectionState.value == c) return
|
||||
Logger.d { "onConnectionChanged: ${connectionStateHolder.connectionState.value} -> $c" }
|
||||
|
||||
sleepTimeout?.cancel()
|
||||
@@ -134,7 +134,10 @@ constructor(
|
||||
}
|
||||
|
||||
private fun handleConnected() {
|
||||
connectionStateHolder.setState(ConnectionState.Connecting)
|
||||
// The service state remains 'Connecting' until config is fully loaded
|
||||
if (connectionStateHolder.connectionState.value == ConnectionState.Disconnected) {
|
||||
connectionStateHolder.setState(ConnectionState.Connecting)
|
||||
}
|
||||
serviceBroadcasts.broadcastConnection()
|
||||
Logger.d { "Starting connect" }
|
||||
connectTimeMsec = System.currentTimeMillis()
|
||||
|
||||
@@ -137,7 +137,9 @@ constructor(
|
||||
PortNum.POSITION_APP -> handlePosition(packet, dataPacket, myNodeNum)
|
||||
PortNum.NODEINFO_APP -> if (!fromUs) handleNodeInfo(packet)
|
||||
PortNum.TELEMETRY_APP -> handleTelemetry(packet, dataPacket, myNodeNum)
|
||||
else -> shouldBroadcast = handleSpecializedDataPacket(packet, dataPacket, myNodeNum, logUuid, logInsertJob)
|
||||
else ->
|
||||
shouldBroadcast =
|
||||
handleSpecializedDataPacket(packet, dataPacket, myNodeNum, fromUs, logUuid, logInsertJob)
|
||||
}
|
||||
return shouldBroadcast
|
||||
}
|
||||
@@ -146,14 +148,16 @@ constructor(
|
||||
packet: MeshPacket,
|
||||
dataPacket: DataPacket,
|
||||
myNodeNum: Int,
|
||||
fromUs: Boolean,
|
||||
logUuid: String?,
|
||||
logInsertJob: Job?,
|
||||
): Boolean {
|
||||
var shouldBroadcast = false
|
||||
var shouldBroadcast = !fromUs
|
||||
val decoded = packet.decoded ?: return shouldBroadcast
|
||||
when (decoded.portnum) {
|
||||
PortNum.TRACEROUTE_APP -> {
|
||||
tracerouteHandler.handleTraceroute(packet, logUuid, logInsertJob)
|
||||
shouldBroadcast = false
|
||||
}
|
||||
PortNum.ROUTING_APP -> {
|
||||
handleRouting(packet, dataPacket)
|
||||
@@ -181,12 +185,25 @@ constructor(
|
||||
shouldBroadcast = true
|
||||
}
|
||||
|
||||
PortNum.ATAK_PLUGIN,
|
||||
PortNum.ATAK_FORWARDER,
|
||||
PortNum.PRIVATE_APP,
|
||||
-> {
|
||||
shouldBroadcast = true
|
||||
}
|
||||
|
||||
PortNum.RANGE_TEST_APP,
|
||||
PortNum.DETECTION_SENSOR_APP,
|
||||
-> {
|
||||
handleRangeTest(dataPacket, myNodeNum)
|
||||
shouldBroadcast = true
|
||||
}
|
||||
|
||||
else -> {
|
||||
// By default, if we don't know what it is, we should probably broadcast it
|
||||
// so that external apps can handle it.
|
||||
shouldBroadcast = true
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
return shouldBroadcast
|
||||
}
|
||||
@@ -420,7 +437,7 @@ constructor(
|
||||
}
|
||||
if (shouldDisplay) {
|
||||
val now = System.currentTimeMillis() / MILLISECONDS_IN_SECOND
|
||||
if (!batteryPercentCooldowns.containsKey(fromNum)) batteryPercentCooldowns[fromNum] = 0
|
||||
if (!batteryPercentCooldowns.containsKey(fromNum)) batteryPercentCooldowns[fromNum] = 0L
|
||||
if ((now - batteryPercentCooldowns[fromNum]!!) >= BATTERY_PERCENT_COOLDOWN_SECONDS || forceDisplay) {
|
||||
batteryPercentCooldowns[fromNum] = now
|
||||
return true
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.meshtastic.core.model.DataPacket
|
||||
import org.meshtastic.core.model.MessageStatus
|
||||
import org.meshtastic.core.model.NodeInfo
|
||||
import org.meshtastic.core.model.util.toPIIString
|
||||
import org.meshtastic.core.service.ConnectionState
|
||||
import org.meshtastic.core.service.ServiceRepository
|
||||
import java.util.Locale
|
||||
import javax.inject.Inject
|
||||
@@ -47,7 +48,14 @@ constructor(
|
||||
|
||||
/** Broadcast some received data Payload will be a DataPacket */
|
||||
fun broadcastReceivedData(payload: DataPacket) {
|
||||
explicitBroadcast(Intent(MeshService.actionReceived(payload.dataType)).putExtra(EXTRA_PAYLOAD, payload))
|
||||
val action = MeshService.actionReceived(payload.dataType)
|
||||
explicitBroadcast(Intent(action).putExtra(EXTRA_PAYLOAD, payload))
|
||||
|
||||
// Also broadcast with the numeric port number for backwards compatibility with some apps
|
||||
val numericAction = actionReceived(payload.dataType.toString())
|
||||
if (numericAction != action) {
|
||||
explicitBroadcast(Intent(numericAction).putExtra(EXTRA_PAYLOAD, payload))
|
||||
}
|
||||
}
|
||||
|
||||
fun broadcastNodeChange(info: NodeInfo) {
|
||||
@@ -84,12 +92,16 @@ constructor(
|
||||
serviceRepository.setConnectionState(connectionState)
|
||||
explicitBroadcast(intent)
|
||||
|
||||
if (connectionState == ConnectionState.Disconnected) {
|
||||
explicitBroadcast(Intent(ACTION_MESH_DISCONNECTED))
|
||||
}
|
||||
|
||||
// Restore legacy action for other consumers (e.g. mesh_service_example)
|
||||
val legacyIntent =
|
||||
Intent(ACTION_CONNECTION_CHANGED).apply {
|
||||
putExtra(EXTRA_CONNECTED, stateStr)
|
||||
// Legacy boolean extra often expected by older implementations
|
||||
putExtra("connected", connectionState == org.meshtastic.core.service.ConnectionState.Connected)
|
||||
putExtra("connected", connectionState == ConnectionState.Connected)
|
||||
}
|
||||
explicitBroadcast(legacyIntent)
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import no.nordicsemi.kotlin.ble.client.mock.internal.MockRemoteCharacteristic
|
||||
import no.nordicsemi.kotlin.ble.core.CharacteristicProperty
|
||||
import no.nordicsemi.kotlin.ble.core.LegacyAdvertisingSetParameters
|
||||
import no.nordicsemi.kotlin.ble.core.Permission
|
||||
import org.junit.Ignore
|
||||
import org.junit.Test
|
||||
import java.util.UUID
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
@@ -49,6 +50,7 @@ class NordicBleInterfaceDrainTest {
|
||||
|
||||
private fun UUID.toKotlinUuid(): Uuid = Uuid.parse(this.toString())
|
||||
|
||||
@Ignore("Flaky: relies on timing in the Nordic BLE mock library which causes intermittent CI failures")
|
||||
@Test
|
||||
fun `drainPacketQueueAndDispatch reads multiple packets until empty`() = runTest(testDispatcher) {
|
||||
val centralManager = CentralManager.Factory.mock(scope = backgroundScope)
|
||||
|
||||
@@ -27,7 +27,7 @@ dependencies {
|
||||
// Replace 'v2.7.13' with the specific version you need
|
||||
val meshtasticVersion = "v2.7.13"
|
||||
|
||||
// The core AIDL interface
|
||||
// The core AIDL interface and Intent constants
|
||||
implementation("com.github.meshtastic.Meshtastic-Android:meshtastic-android-api:$meshtasticVersion")
|
||||
|
||||
// Data models (DataPacket, MeshUser, NodeInfo, etc.)
|
||||
@@ -77,15 +77,17 @@ override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
||||
|
||||
### 3. Register a BroadcastReceiver
|
||||
|
||||
To receive packets and status updates, register a `BroadcastReceiver`.
|
||||
To receive packets and status updates, register a `BroadcastReceiver`. Use `MeshtasticIntent` constants for the actions.
|
||||
|
||||
**Important:** On Android 13+ (API 33), you **must** use `RECEIVER_EXPORTED` since you are receiving broadcasts from a different application.
|
||||
|
||||
```kotlin
|
||||
// Using constants from org.meshtastic.core.api.MeshtasticIntent
|
||||
val intentFilter = IntentFilter().apply {
|
||||
addAction("com.geeksville.mesh.RECEIVED.TEXT_MESSAGE_APP")
|
||||
addAction("com.geeksville.mesh.NODE_CHANGE")
|
||||
addAction("com.geeksville.mesh.CONNECTION_CHANGED")
|
||||
addAction(MeshtasticIntent.ACTION_RECEIVED_TEXT_MESSAGE_APP)
|
||||
addAction(MeshtasticIntent.ACTION_NODE_CHANGE)
|
||||
addAction(MeshtasticIntent.ACTION_CONNECTION_CHANGED)
|
||||
addAction(MeshtasticIntent.ACTION_MESH_DISCONNECTED)
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
@@ -97,6 +99,6 @@ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
|
||||
## Modules
|
||||
|
||||
* **`core:api`**: Contains `IMeshService.aidl`.
|
||||
* **`core:api`**: Contains `IMeshService.aidl` and `MeshtasticIntent`.
|
||||
* **`core:model`**: Contains Parcelable data classes like `DataPacket`, `MeshUser`, `NodeInfo`.
|
||||
* **`core:proto`**: Contains the generated Protobuf code (Wire).
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2025-2026 Meshtastic LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.meshtastic.core.api
|
||||
|
||||
import org.meshtastic.core.api.MeshtasticIntent.EXTRA_CONNECTED
|
||||
import org.meshtastic.core.api.MeshtasticIntent.EXTRA_NODEINFO
|
||||
import org.meshtastic.core.api.MeshtasticIntent.EXTRA_PACKET_ID
|
||||
import org.meshtastic.core.api.MeshtasticIntent.EXTRA_STATUS
|
||||
|
||||
/**
|
||||
* Constants for Meshtastic Android Intents. These are used by external applications to communicate with the Meshtastic
|
||||
* service.
|
||||
*/
|
||||
object MeshtasticIntent {
|
||||
private const val PREFIX = "com.geeksville.mesh"
|
||||
|
||||
/** Broadcast when a node's information changes. Extra: [EXTRA_NODEINFO] */
|
||||
const val ACTION_NODE_CHANGE = "$PREFIX.NODE_CHANGE"
|
||||
|
||||
/** Broadcast when the mesh radio connects. Extra: [EXTRA_CONNECTED] */
|
||||
const val ACTION_MESH_CONNECTED = "$PREFIX.MESH_CONNECTED"
|
||||
|
||||
/** Broadcast when the mesh radio disconnects. */
|
||||
const val ACTION_MESH_DISCONNECTED = "$PREFIX.MESH_DISCONNECTED"
|
||||
|
||||
/** Legacy broadcast for connection changes. Extra: [EXTRA_CONNECTED] */
|
||||
const val ACTION_CONNECTION_CHANGED = "$PREFIX.CONNECTION_CHANGED"
|
||||
|
||||
/** Broadcast for message status updates. Extras: [EXTRA_PACKET_ID], [EXTRA_STATUS] */
|
||||
const val ACTION_MESSAGE_STATUS = "$PREFIX.MESSAGE_STATUS"
|
||||
|
||||
/** Received a text message. */
|
||||
const val ACTION_RECEIVED_TEXT_MESSAGE_APP = "$PREFIX.RECEIVED.TEXT_MESSAGE_APP"
|
||||
|
||||
/** Received a position update. */
|
||||
const val ACTION_RECEIVED_POSITION_APP = "$PREFIX.RECEIVED.POSITION_APP"
|
||||
|
||||
/** Received node info. */
|
||||
const val ACTION_RECEIVED_NODEINFO_APP = "$PREFIX.RECEIVED.NODEINFO_APP"
|
||||
|
||||
/** Received telemetry data. */
|
||||
const val ACTION_RECEIVED_TELEMETRY_APP = "$PREFIX.RECEIVED.TELEMETRY_APP"
|
||||
|
||||
/** Received ATAK Plugin data. */
|
||||
const val ACTION_RECEIVED_ATAK_PLUGIN = "$PREFIX.RECEIVED.ATAK_PLUGIN"
|
||||
|
||||
/** Received ATAK Forwarder data. */
|
||||
const val ACTION_RECEIVED_ATAK_FORWARDER = "$PREFIX.RECEIVED.ATAK_FORWARDER"
|
||||
|
||||
/** Received detection sensor data. */
|
||||
const val ACTION_RECEIVED_DETECTION_SENSOR_APP = "$PREFIX.RECEIVED.DETECTION_SENSOR_APP"
|
||||
|
||||
/** Received private app data. */
|
||||
const val ACTION_RECEIVED_PRIVATE_APP = "$PREFIX.RECEIVED.PRIVATE_APP"
|
||||
|
||||
// standard EXTRA bundle definitions
|
||||
const val EXTRA_CONNECTED = "$PREFIX.Connected"
|
||||
const val EXTRA_PAYLOAD = "$PREFIX.Payload"
|
||||
const val EXTRA_NODEINFO = "$PREFIX.NodeInfo"
|
||||
const val EXTRA_PACKET_ID = "$PREFIX.PacketId"
|
||||
const val EXTRA_STATUS = "$PREFIX.Status"
|
||||
}
|
||||
@@ -36,5 +36,5 @@ This module contains the Parcelable data classes used by the Meshtastic Android
|
||||
This module is typically used as a dependency of `core:api` but can be used independently if you need to work with Meshtastic data structures.
|
||||
|
||||
```kotlin
|
||||
implementation("com.github.meshtastic.Meshtastic-Android:meshtastic-android-model:v2.7.12")
|
||||
```
|
||||
implementation("com.github.meshtastic.Meshtastic-Android:meshtastic-android-model:v2.7.13")
|
||||
```
|
||||
|
||||
@@ -19,3 +19,21 @@ classDef unknown fill:#FFADAD,stroke:#000,stroke-width:2px,color:#000;
|
||||
|
||||
```
|
||||
<!--endregion-->
|
||||
|
||||
## Meshtastic Protobuf Definitions
|
||||
|
||||
This module contains the generated Kotlin and Java code from the Meshtastic Protobuf definitions. It uses the [Wire](https://github.com/square/wire) library for efficient and clean model generation.
|
||||
|
||||
### Key Components
|
||||
|
||||
* **Port Numbers**: Defines the `PortNum` enum for identifying different types of data payloads.
|
||||
* **Mesh Protocol**: Contains the core `MeshPacket` and protocol message definitions.
|
||||
* **Modules**: Includes definitions for telemetry, position, administration, and more.
|
||||
|
||||
### Usage
|
||||
|
||||
This module is typically used as a dependency of `core:api` and `core:model`.
|
||||
|
||||
```kotlin
|
||||
implementation("com.github.meshtastic.Meshtastic-Android:meshtastic-android-proto:v2.7.13")
|
||||
```
|
||||
|
||||
@@ -39,6 +39,7 @@ import androidx.compose.material3.dynamicLightColorScheme
|
||||
import androidx.compose.material3.lightColorScheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import org.meshtastic.core.api.MeshtasticIntent
|
||||
import org.meshtastic.core.service.IMeshService
|
||||
|
||||
private const val TAG: String = "MeshServiceExample"
|
||||
@@ -83,19 +84,19 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
val intentFilter =
|
||||
IntentFilter().apply {
|
||||
addAction("com.geeksville.mesh.NODE_CHANGE")
|
||||
addAction("com.geeksville.mesh.CONNECTION_CHANGED")
|
||||
addAction("com.geeksville.mesh.MESH_CONNECTED")
|
||||
addAction("com.geeksville.mesh.MESH_DISCONNECTED")
|
||||
addAction("com.geeksville.mesh.MESSAGE_STATUS")
|
||||
addAction("com.geeksville.mesh.RECEIVED.TEXT_MESSAGE_APP")
|
||||
addAction("com.geeksville.mesh.RECEIVED.POSITION_APP")
|
||||
addAction("com.geeksville.mesh.RECEIVED.TELEMETRY_APP")
|
||||
addAction("com.geeksville.mesh.RECEIVED.NODEINFO_APP")
|
||||
addAction("com.geeksville.mesh.RECEIVED.ATAK_PLUGIN")
|
||||
addAction("com.geeksville.mesh.RECEIVED.ATAK_FORWARDER")
|
||||
addAction("com.geeksville.mesh.RECEIVED.DETECTION_SENSOR_APP")
|
||||
addAction("com.geeksville.mesh.RECEIVED.PRIVATE_APP")
|
||||
addAction(MeshtasticIntent.ACTION_NODE_CHANGE)
|
||||
addAction(MeshtasticIntent.ACTION_CONNECTION_CHANGED)
|
||||
addAction(MeshtasticIntent.ACTION_MESH_CONNECTED)
|
||||
addAction(MeshtasticIntent.ACTION_MESH_DISCONNECTED)
|
||||
addAction(MeshtasticIntent.ACTION_MESSAGE_STATUS)
|
||||
addAction(MeshtasticIntent.ACTION_RECEIVED_TEXT_MESSAGE_APP)
|
||||
addAction(MeshtasticIntent.ACTION_RECEIVED_POSITION_APP)
|
||||
addAction(MeshtasticIntent.ACTION_RECEIVED_TELEMETRY_APP)
|
||||
addAction(MeshtasticIntent.ACTION_RECEIVED_NODEINFO_APP)
|
||||
addAction(MeshtasticIntent.ACTION_RECEIVED_ATAK_PLUGIN)
|
||||
addAction(MeshtasticIntent.ACTION_RECEIVED_ATAK_FORWARDER)
|
||||
addAction(MeshtasticIntent.ACTION_RECEIVED_DETECTION_SENSOR_APP)
|
||||
addAction(MeshtasticIntent.ACTION_RECEIVED_PRIVATE_APP)
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
|
||||
Reference in New Issue
Block a user