hondavert-dev/lib/core/bluetooth/bt_service.dart
2026-10-13 00:34:11 +00:00

57 lines
1.5 KiB
Dart

import 'dart:async';
import 'dart:typed_data';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
/// Wraps flutter_bluetooth_serial for SPP/RFCOMM Classic BT.
class BtService {
BluetoothConnection? _connection;
final StreamController<Uint8List> _rawFrameController =
StreamController<Uint8List>.broadcast();
bool get isConnected => _connection?.isConnected ?? false;
Stream<Uint8List> get rawStream => _rawFrameController.stream;
Future<List<BluetoothDevice>> getPairedDevices() async {
return FlutterBluetoothSerial.instance.getBondedDevices();
}
Future<void> connect(BluetoothDevice device) async {
if (_connection != null) {
await disconnect();
}
_connection = await BluetoothConnection.toAddress(device.address);
_connection!.input!.listen(
(Uint8List data) {
if (!_rawFrameController.isClosed) {
_rawFrameController.add(data);
}
},
onDone: () {
_rawFrameController.addError('BT connection closed');
},
onError: (Object e) {
_rawFrameController.addError(e);
},
);
}
Future<void> write(Uint8List data) async {
if (_connection?.isConnected ?? false) {
_connection!.output.add(data);
await _connection!.output.allSent;
}
}
Future<void> disconnect() async {
await _connection?.close();
_connection = null;
}
void dispose() {
disconnect();
_rawFrameController.close();
}
}