Skip to content

Repository files navigation

Mesh Base Java Library

A Java library for building mesh network applications, designed for easy integration with Android and cross-platform projects (including Flutter via MethodChannels).


Installation

Using Maven Central

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

Getting Started

1. Initialize the Mesh Manager

importio.github.meshbase.mesh_base_core.mesh_manager.MeshManager;
MeshManagermeshManager = newMeshManager(context); // context is usually your Activity

2. Turn Mesh On/Off

meshManager.on(); // Enable mesh networkingmeshManager.off(); // Disable mesh networking

3. Get Device ID

UUIDselfId = meshManager.getId();

4. Discover Neighbors

List<Device> neighbors = meshManager.getNeighbors();
for (Devicedevice : neighbors) {
System.out.println(device.getName() + " - " + device.getUuid());
}

5. Send a Message

MeshProtocol<RawBytesBody> protocol = newConcreteMeshProtocol<>(
ProtocolType.RAW_BYTES_MESSAGE,
10, // remaining hops
-1, // messageId (auto-assigned if -1)selfId,
destinationUuid,
newRawBytesBody("Hello Mesh!".getBytes())
);
meshManager.send(protocol, newSendListener() {
@OverridepublicvoidonAck() {
// Message was acknowledged
}
@OverridepublicvoidonError(SendErrore) {
// Handle error
}
@OverridepublicvoidonResponse(MeshProtocol<?> response) {
// Handle response
}
}, false); // keepMessageId

6. Subscribe to Mesh Events

meshManager.subscribe(newMeshManagerListener() {
@OverridepublicvoidonDataReceivedForSelf(MeshProtocol<?> protocol) {
// Handle incoming data
}
@OverridepublicvoidonStatusChange(Statusstatus) {
// Handle status updates
}
@OverridepublicvoidonNeighborConnected(Devicedevice) {
// Handle new neighbor
}
@OverridepublicvoidonNeighborDisconnected(Devicedevice) {
// Handle neighbor disconnect
}
@OverridepublicvoidonError(Exceptione) {
// Handle errors
}
});

API Overview

  • 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.

Example

MeshManagermeshManager = newMeshManager(activity);
meshManager.on();
meshManager.subscribe(newMeshManagerListener() {
@OverridepublicvoidonDataReceivedForSelf(MeshProtocol<?> protocol) {
System.out.println("Received: " + newString(((RawBytesBody)protocol.body).getContent()));
}
// ... other overrides ...
});

License

MIT


For more details, see the JavaDoc or the example code for cross-platform usage.

mesh_base_flutter

A Flutter plugin for communicating with the native Java mesh library, enabling mesh networking in your Flutter apps via platform channels.


Installation

Add this to your pubspec.yaml:

dependencies:
mesh_base_flutter:
git:
url: https://github.com/hayk2377/mesh.gitpath: mesh_base_libraries/mesh_base_flutter

Or, if published to pub.dev:

dependencies:
mesh_base_flutter: ^0.0.1

Then run:

flutter pub get

Getting Started

1. Import the Package

import'package:mesh_base_flutter/mesh_base_flutter.dart';

2. Initialize and Subscribe

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();

3. Get Device ID

String selfId =await mesh.getId();

4. Discover Neighbors

List<Device> neighbors =await mesh.getNeighbors();

5. Send a Message

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
} elseif (result.error !=null) {
// Handle error
} elseif (result.response !=null) {
// Handle response
}

6. Broadcast a Message

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);

7. Unsubscribe and Turn Off

await mesh.unsubscribe();
await mesh.turnOff();

API Overview

  • 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)

Example

See example/lib/TestScreen.dart for a full UI sample.


License

MIT


For more details, see the Java library documentation or the example app.

About

Java library for building mesh network applications

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages