A Java library for building mesh network applications, designed for easy integration with Android and cross-platform projects (including Flutter via MethodChannels).
Add the following to your build.gradle dependencies:
dependencies {
implementation "io.github.meshbase:mesh_base_core:1.1"
}Or, if you use Maven:
<dependency>
<groupId>io.github.meshbase</groupId>
<artifactId>mesh_base_core</artifactId>
<version>1.1</version>
</dependency>Note: If you are developing locally, you may need to publish the library to your local Maven repository:
./gradlew publishToMavenLocal
import io.github.meshbase.mesh_base_core.mesh_manager.MeshManager;
MeshManager meshManager = new MeshManager(context); // context is usually your ActivitymeshManager.on(); // Enable mesh networking
meshManager.off(); // Disable mesh networkingUUID selfId = meshManager.getId();List<Device> neighbors = meshManager.getNeighbors();
for (Device device : neighbors) {
System.out.println(device.getName() + " - " + device.getUuid());
}MeshProtocol<RawBytesBody> protocol = new ConcreteMeshProtocol<>(
ProtocolType.RAW_BYTES_MESSAGE,
10, // remaining hops
-1, // messageId (auto-assigned if -1)
selfId,
destinationUuid,
new RawBytesBody("Hello Mesh!".getBytes())
);
meshManager.send(protocol, new SendListener() {
@Override
public void onAck() {
// Message was acknowledged
}
@Override
public void onError(SendError e) {
// Handle error
}
@Override
public void onResponse(MeshProtocol<?> response) {
// Handle response
}
}, false); // keepMessageIdmeshManager.subscribe(new MeshManagerListener() {
@Override
public void onDataReceivedForSelf(MeshProtocol<?> protocol) {
// Handle incoming data
}
@Override
public void onStatusChange(Status status) {
// Handle status updates
}
@Override
public void onNeighborConnected(Device device) {
// Handle new neighbor
}
@Override
public void onNeighborDisconnected(Device device) {
// Handle neighbor disconnect
}
@Override
public void onError(Exception e) {
// Handle errors
}
});-
MeshManager
on()/off(): Enable/disable mesh networking.getId(): Get this device's unique mesh ID.getNeighbors(): List currently connected mesh neighbors.send(MeshProtocol, SendListener, boolean): Send a message.subscribe(MeshManagerListener): Listen for mesh events.
-
MeshManagerListener
onDataReceivedForSelf(MeshProtocol<?>)onStatusChange(Status)onNeighborConnected(Device)onNeighborDisconnected(Device)onError(Exception)
-
SendListener
onAck()onError(SendError)onResponse(MeshProtocol<?>)
-
MeshProtocol / ConcreteMeshProtocol
- Represents a message in the mesh network.
MeshManager meshManager = new MeshManager(activity);
meshManager.on();
meshManager.subscribe(new MeshManagerListener() {
@Override
public void onDataReceivedForSelf(MeshProtocol<?> protocol) {
System.out.println("Received: " + new String(((RawBytesBody)protocol.body).getContent()));
}
// ... other overrides ...
});For more details, see the JavaDoc or the example code for cross-platform usage.
A Flutter plugin for communicating with the native Java mesh library, enabling mesh networking in your Flutter apps via platform channels.
Add this to your pubspec.yaml:
dependencies:
mesh_base_flutter:
git:
url: https://github.com/hayk2377/mesh.git
path: mesh_base_libraries/mesh_base_flutterOr, if published to pub.dev:
dependencies:
mesh_base_flutter: ^0.0.1Then run:
flutter pub getimport 'package:mesh_base_flutter/mesh_base_flutter.dart';final mesh = MeshBaseFlutter();
final listener = MeshManagerListener(
onDataReceivedForSelf: (protocol) {
// Handle received data
},
onStatusChange: (status) {
// Handle status updates
},
onNeighborConnected: (device) {
// Handle new neighbor
},
onNeighborDisconnected: (device) {
// Handle neighbor disconnect
},
onError: (error) {
// Handle errors
},
);
await mesh.subscribe(listener);
await mesh.turnOn();String selfId = await mesh.getId();List<Device> neighbors = await mesh.getNeighbors();final protocol = MeshProtocol(
messageType: ProtocolType.RAW_BYTES_MESSAGE,
remainingHops: 10,
messageId: -1,
sender: selfId,
destination: neighborId,
body: Uint8List.fromList(utf8.encode("Hello Mesh!")),
);
final result = await mesh.send(protocol: protocol, keepMessageId: false);
if (result.acked) {
// Message was acknowledged
} else if (result.error != null) {
// Handle error
} else if (result.response != null) {
// Handle response
}final protocol = MeshProtocol(
messageType: ProtocolType.RAW_BYTES_MESSAGE,
remainingHops: -1,
messageId: -1,
sender: selfId,
destination: BROADCAST_UUID,
body: Uint8List.fromList(utf8.encode("Broadcast message")),
);
await mesh.send(protocol: protocol, keepMessageId: false);await mesh.unsubscribe();
await mesh.turnOff();-
MeshBaseFlutter
Future<void> turnOn() / turnOff(): Enable/disable mesh networking.Future<String> getId(): Get this device's unique mesh ID.Future<List<Device>> getNeighbors(): List currently connected mesh neighbors.Future<MeshStatus> getStatus(): Get mesh and BLE status.Future<SendResult> send({required MeshProtocol protocol, bool keepMessageId}): Send a message.Future<void> subscribe(MeshManagerListener listener): Listen for mesh events.Future<void> unsubscribe(): Stop listening for events.
-
MeshManagerListener
onDataReceivedForSelf(MeshProtocol data)onStatusChange(MeshStatus status)onNeighborConnected(Device device)onNeighborDisconnected(Device device)onError(dynamic error)
-
MeshProtocol
messageType,remainingHops,messageId,sender,destination,body
-
Device
uuid,name
-
MeshStatus
isOn,statuses(per connection type)
See example/lib/TestScreen.dart for a full UI sample.
For more details, see the Java library documentation or the example app.