2026-05-11 09:34:41 +02:00
|
|
|
// lib/services/gnss/bt_serial_gnss_connection.dart
|
|
|
|
|
import 'dart:async';
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
|
|
|
|
|
import 'gnss_connection.dart';
|
2026-05-11 13:50:40 +02:00
|
|
|
import 'gnss_device_service.dart';
|
2026-05-11 09:34:41 +02:00
|
|
|
|
|
|
|
|
class BtSerialGnssConnection implements GnssConnection {
|
|
|
|
|
@override
|
|
|
|
|
GnssConnectionType get type => GnssConnectionType.btSerial;
|
|
|
|
|
|
|
|
|
|
BluetoothConnection? _connection;
|
|
|
|
|
String _messageBuffer = '';
|
|
|
|
|
|
|
|
|
|
final _nmeaController = StreamController<String>.broadcast();
|
|
|
|
|
final _stateController = StreamController<GnssConnectionState>.broadcast();
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Stream<String> get nmeaLines => _nmeaController.stream;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Stream<GnssConnectionState> get connectionState => _stateController.stream;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<void> connect(String address) async {
|
|
|
|
|
_stateController.add(GnssConnectionState.connecting);
|
|
|
|
|
try {
|
|
|
|
|
_connection = await BluetoothConnection.toAddress(address);
|
|
|
|
|
_stateController.add(GnssConnectionState.connected);
|
|
|
|
|
_connection!.input!.listen(
|
|
|
|
|
_onData,
|
|
|
|
|
onDone: () {
|
|
|
|
|
_stateController.add(GnssConnectionState.disconnected);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
_stateController.add(GnssConnectionState.error);
|
|
|
|
|
rethrow;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<void> disconnect() async {
|
|
|
|
|
await _connection?.close();
|
|
|
|
|
_connection = null;
|
|
|
|
|
_stateController.add(GnssConnectionState.disconnected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A meglévő _onDataReceived logika változatlanul
|
|
|
|
|
void _onData(Uint8List data) {
|
|
|
|
|
int backspacesCounter = 0;
|
|
|
|
|
for (var byte in data) {
|
|
|
|
|
if (byte == 8 || byte == 127) backspacesCounter++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Uint8List buffer = Uint8List(data.length - backspacesCounter);
|
|
|
|
|
int bufferIndex = buffer.length;
|
|
|
|
|
backspacesCounter = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = data.length - 1; i >= 0; i--) {
|
|
|
|
|
if (data[i] == 8 || data[i] == 127) {
|
|
|
|
|
backspacesCounter++;
|
|
|
|
|
} else if (backspacesCounter > 0) {
|
|
|
|
|
backspacesCounter--;
|
|
|
|
|
} else {
|
|
|
|
|
buffer[--bufferIndex] = data[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final dataString = String.fromCharCodes(buffer);
|
|
|
|
|
final index = buffer.indexOf(13); // \r
|
|
|
|
|
|
|
|
|
|
String sentence;
|
|
|
|
|
if (~index != 0) {
|
|
|
|
|
sentence = _messageBuffer + dataString.substring(0, index);
|
|
|
|
|
_messageBuffer = dataString.substring(index);
|
|
|
|
|
} else {
|
|
|
|
|
_messageBuffer += dataString;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Soronként kibocsátjuk
|
|
|
|
|
for (final line in sentence.split('\n')) {
|
|
|
|
|
final trimmed = line.trim();
|
|
|
|
|
if (trimmed.isNotEmpty) _nmeaController.add(trimmed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_connection?.close();
|
|
|
|
|
_nmeaController.close();
|
|
|
|
|
_stateController.close();
|
|
|
|
|
}
|
2026-05-16 14:47:34 +02:00
|
|
|
|
|
|
|
|
void sendData(Uint8List data) {
|
|
|
|
|
_connection?.output.add(data);
|
|
|
|
|
}
|
2026-05-11 09:34:41 +02:00
|
|
|
}
|