diff --git a/mesh_service_example/src/main/java/com/meshtastic/android/meshserviceexample/MainActivity.java b/mesh_service_example/src/main/java/com/meshtastic/android/meshserviceexample/MainActivity.java
index 16f0282e0..b0b41d616 100644
--- a/mesh_service_example/src/main/java/com/meshtastic/android/meshserviceexample/MainActivity.java
+++ b/mesh_service_example/src/main/java/com/meshtastic/android/meshserviceexample/MainActivity.java
@@ -1,3 +1,20 @@
+/*
+ * Copyright (c) 2025 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 .
+ */
+
package com.meshtastic.android.meshserviceexample;
import android.content.BroadcastReceiver;
@@ -10,7 +27,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
-import android.view.View;
+import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
@@ -21,9 +38,11 @@ import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
+import com.geeksville.mesh.DataPacket;
import com.geeksville.mesh.IMeshService;
import com.geeksville.mesh.MessageStatus;
import com.geeksville.mesh.NodeInfo;
+import com.geeksville.mesh.Portnums;
import java.util.Objects;
@@ -32,7 +51,6 @@ public class MainActivity extends AppCompatActivity {
private static final String TAG = "MeshServiceExample";
private IMeshService meshService;
private ServiceConnection serviceConnection;
- private BroadcastReceiver meshtasticReceiver;
private boolean isMeshServiceBound = false;
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
@@ -49,6 +67,22 @@ public class MainActivity extends AppCompatActivity {
TextView mainTextView = findViewById(R.id.mainTextView);
ImageView statusImageView = findViewById(R.id.statusImageView);
+ Button sendButton = findViewById(R.id.sendBtn);
+
+ sendButton.setOnClickListener(v -> {
+ if (meshService != null) {
+ try {
+ byte[] bytes = "Hello from MeshServiceExample".getBytes();
+ DataPacket dataPacket = new DataPacket(DataPacket.ID_BROADCAST, bytes, Portnums.PortNum.TEXT_MESSAGE_APP_VALUE, DataPacket.ID_LOCAL, System.currentTimeMillis(), 0, MessageStatus.UNKNOWN, 3, 0, true);
+ meshService.send(dataPacket);
+ Log.d(TAG, "Message sent successfully");
+ } catch (Exception e) {
+ Log.e(TAG, "Failed to send message", e);
+ }
+ } else {
+ Log.w(TAG, "MeshService is not bound, cannot send message");
+ }
+ });
// Now you can call methods on meshService
serviceConnection = new ServiceConnection() {
@@ -67,7 +101,10 @@ public class MainActivity extends AppCompatActivity {
}
};
- meshtasticReceiver = new BroadcastReceiver() {
+ // Handle the received broadcast
+ // handle node changed
+ // handle position app data
+ BroadcastReceiver meshtasticReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null || intent.getAction() == null) {
@@ -86,7 +123,7 @@ public class MainActivity extends AppCompatActivity {
Log.d(TAG, "NodeInfo: " + ni);
mainTextView.setText("NodeInfo: " + ni);
} catch (Exception e) {
- e.printStackTrace();
+ Log.e(TAG, "onReceive: " + e.getMessage());
return;
}
break;
@@ -97,7 +134,7 @@ public class MainActivity extends AppCompatActivity {
break;
case "com.geeksville.mesh.MESH_CONNECTED": {
String extraConnected = intent.getStringExtra("com.geeksville.mesh.Connected");
- boolean connected = extraConnected.equals("CONNECTED");
+ boolean connected = extraConnected.equalsIgnoreCase("connected");
Log.d(TAG, "Received ACTION_MESH_CONNECTED: " + extraConnected);
if (connected) {
statusImageView.setImageResource(android.R.color.holo_green_light);
@@ -106,7 +143,7 @@ public class MainActivity extends AppCompatActivity {
}
case "com.geeksville.mesh.MESH_DISCONNECTED": {
String extraConnected = intent.getStringExtra("com.geeksville.mesh.Disconnected");
- boolean disconnected = extraConnected.equals("DISCONNECTED");
+ boolean disconnected = extraConnected.equalsIgnoreCase("disconnected");
Log.d(TAG, "Received ACTION_MESH_DISTCONNECTED: " + extraConnected);
if (disconnected) {
statusImageView.setImageResource(android.R.color.holo_red_light);
@@ -137,7 +174,7 @@ public class MainActivity extends AppCompatActivity {
filter.addAction("com.geeksville.mesh.RECEIVED.POSITION_APP");
filter.addAction("com.geeksville.mesh.MESH_CONNECTED");
filter.addAction("com.geeksville.mesh.MESH_DISCONNECTED");
- registerReceiver(meshtasticReceiver, filter, Context.RECEIVER_EXPORTED);
+ registerReceiver(meshtasticReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
Log.d(TAG, "Registered meshtasticPacketReceiver");
while (!bindMeshService()) {
diff --git a/mesh_service_example/src/main/res/layout/activity_main.xml b/mesh_service_example/src/main/res/layout/activity_main.xml
index 7b1f2aa1d..1c2742506 100644
--- a/mesh_service_example/src/main/res/layout/activity_main.xml
+++ b/mesh_service_example/src/main/res/layout/activity_main.xml
@@ -1,4 +1,21 @@
+
+
+
+
\ No newline at end of file
diff --git a/mesh_service_example/src/main/res/values/strings.xml b/mesh_service_example/src/main/res/values/strings.xml
index fcec4106c..2af2b6eaa 100644
--- a/mesh_service_example/src/main/res/values/strings.xml
+++ b/mesh_service_example/src/main/res/values/strings.xml
@@ -1,3 +1,21 @@
+
+
MeshServiceExample
+ Send Hello Message
\ No newline at end of file