mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-07-06 00:36:40 +10:00
fixes
This commit is contained in:
@@ -613,10 +613,20 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
throw Exception("MeshCore characteristics not found");
|
||||
}
|
||||
|
||||
// Give the device a moment to be ready for descriptor writes
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
|
||||
await _txCharacteristic!.setNotifyValue(true);
|
||||
// Retry setNotifyValue with increasing delays
|
||||
bool notifySet = false;
|
||||
for (int attempt = 0; attempt < 3 && !notifySet; attempt++) {
|
||||
try {
|
||||
if (attempt > 0) {
|
||||
await Future.delayed(Duration(milliseconds: 500 * attempt));
|
||||
}
|
||||
await _txCharacteristic!.setNotifyValue(true);
|
||||
notifySet = true;
|
||||
} catch (e) {
|
||||
debugPrint('setNotifyValue attempt ${attempt + 1}/3 failed: $e');
|
||||
if (attempt == 2) rethrow;
|
||||
}
|
||||
}
|
||||
_notifySubscription = _txCharacteristic!.onValueReceived.listen(_handleFrame);
|
||||
|
||||
_setState(MeshCoreConnectionState.connected);
|
||||
@@ -1185,6 +1195,30 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
await sendFrame(bytes);
|
||||
}
|
||||
|
||||
Future<void> setNodeName(String name) async {
|
||||
if (!isConnected) return;
|
||||
await sendFrame(buildSetAdvertNameFrame(name));
|
||||
}
|
||||
|
||||
Future<void> setNodeLocation({required double lat, required double lon}) async {
|
||||
if (!isConnected) return;
|
||||
await sendFrame(buildSetAdvertLatLonFrame(lat, lon));
|
||||
}
|
||||
|
||||
Future<void> sendSelfAdvert({bool flood = true}) async {
|
||||
if (!isConnected) return;
|
||||
await sendFrame(buildSendSelfAdvertFrame(flood: flood));
|
||||
}
|
||||
|
||||
Future<void> rebootDevice() async {
|
||||
if (!isConnected) return;
|
||||
await sendFrame(buildRebootFrame());
|
||||
}
|
||||
|
||||
Future<void> setPrivacyMode(bool enabled) async {
|
||||
await sendCliCommand('set privacy ${enabled ? 'on' : 'off'}');
|
||||
}
|
||||
|
||||
Future<void> getChannels({int? maxChannels}) async {
|
||||
if (!isConnected) return;
|
||||
|
||||
|
||||
@@ -210,6 +210,11 @@ void writeUint32LE(Uint8List data, int offset, int value) {
|
||||
data[offset + 3] = (value >> 24) & 0xFF;
|
||||
}
|
||||
|
||||
// Helper to write int32 little-endian
|
||||
void writeInt32LE(Uint8List data, int offset, int value) {
|
||||
writeUint32LE(data, offset, value & 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
// Helper to read null-terminated UTF-8 string
|
||||
String readCString(Uint8List data, int offset, int maxLen) {
|
||||
int end = offset;
|
||||
@@ -362,6 +367,39 @@ Uint8List buildSetDeviceTimeFrame(int timestamp) {
|
||||
return frame;
|
||||
}
|
||||
|
||||
// Build CMD_SEND_SELF_ADVERT frame
|
||||
// Format: [cmd][flood_flag]
|
||||
Uint8List buildSendSelfAdvertFrame({bool flood = false}) {
|
||||
return Uint8List.fromList([cmdSendSelfAdvert, flood ? 1 : 0]);
|
||||
}
|
||||
|
||||
// Build CMD_SET_ADVERT_NAME frame
|
||||
// Format: [cmd][name...]
|
||||
Uint8List buildSetAdvertNameFrame(String name) {
|
||||
final nameBytes = utf8.encode(name);
|
||||
final nameLen = nameBytes.length < maxNameSize ? nameBytes.length : maxNameSize - 1;
|
||||
final frame = Uint8List(1 + nameLen);
|
||||
frame[0] = cmdSetAdvertName;
|
||||
frame.setRange(1, 1 + nameLen, nameBytes.sublist(0, nameLen));
|
||||
return frame;
|
||||
}
|
||||
|
||||
// Build CMD_SET_ADVERT_LATLON frame
|
||||
// Format: [cmd][lat x4][lon x4]
|
||||
Uint8List buildSetAdvertLatLonFrame(double lat, double lon) {
|
||||
final frame = Uint8List(9);
|
||||
frame[0] = cmdSetAdvertLatLon;
|
||||
writeInt32LE(frame, 1, (lat * 1000000).round());
|
||||
writeInt32LE(frame, 5, (lon * 1000000).round());
|
||||
return frame;
|
||||
}
|
||||
|
||||
// Build CMD_REBOOT frame
|
||||
// Format: [cmd]["reboot"]
|
||||
Uint8List buildRebootFrame() {
|
||||
return Uint8List.fromList([cmdReboot, ...utf8.encode('reboot')]);
|
||||
}
|
||||
|
||||
// Build CMD_SYNC_NEXT_MESSAGE frame
|
||||
Uint8List buildSyncNextMessageFrame() {
|
||||
return Uint8List.fromList([cmdSyncNextMessage]);
|
||||
|
||||
Reference in New Issue
Block a user