feat: Add send message service example (#2203)

This commit is contained in:
James Rich 2025-06-21 16:39:01 +00:00 committed by GitHub
parent a693ee5c81
commit 6f70aabfd4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 90 additions and 7 deletions

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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()) {

View file

@ -1,4 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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 <https://www.gnu.org/licenses/>.
-->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
@ -37,4 +54,15 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/sendBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send_hello_message"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.852" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -1,3 +1,21 @@
<!--
~ 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 <https://www.gnu.org/licenses/>.
-->
<resources>
<string name="app_name">MeshServiceExample</string>
<string name="send_hello_message">Send Hello Message</string>
</resources>