import 'dart:async'; import 'dart:typed_data'; import 'package:flutter/services.dart'; /// BLE连接状态常量 class BleConnectionState { static const int disconnected = 0; static const int connecting = 1; static const int connected = 2; static const int disconnecting = 3; static const int error = 4; } /// BLE设备数据模型 class BleDevice { final String id; final String name; final int rssi; final List uuids; BleDevice({ required this.id, required this.name, required this.rssi, required this.uuids, }); factory BleDevice.fromMap(Map map) { final uuidsList = (map['uuids'] as List?)?.cast() ?? []; return BleDevice( id: map['id'] as String, name: map['name'] as String? ?? 'Unknown', rssi: map['rssi'] as int, uuids: uuidsList, ); } @override String toString() => 'BleDevice(id: $id, name: $name, rssi: $rssi, uuids: $uuids)'; } /// BLE服务插件 class BleService { static final BleService _instance = BleService._internal(); /// 获取单例 factory BleService() => _instance; // 方法通道 final MethodChannel _methodChannel = const MethodChannel('com.yunqiinnovation.ble_service'); // 事件通道 late final EventChannel _scanEventChannel; late final EventChannel _connectionEventChannel; late final EventChannel _dataEventChannel; late final EventChannel _statusEventChannel; // 事件流 late final Stream> _scanStream; late final Stream _connectionStream; late final Stream _dataStream; late final Stream _statusStream; // 获取事件流 Stream> get onScanResults => _scanStream; Stream get onConnectionChanged => _connectionStream; Stream get onDataReceived => _dataStream; Stream get onStatusUpdated => _statusStream; // 内部构造函数 BleService._internal() { _scanEventChannel = const EventChannel('com.yunqiinnovation.ble_service/scan_events'); _connectionEventChannel = const EventChannel('com.yunqiinnovation.ble_service/connection_events'); _dataEventChannel = const EventChannel('com.yunqiinnovation.ble_service/data_events'); _statusEventChannel = const EventChannel('com.yunqiinnovation.ble_service/status_events'); // 初始化事件流 _scanStream = _scanEventChannel.receiveBroadcastStream().map>((event) { final List devices = (event['devices'] as List?) ?? []; return devices.map((device) => BleDevice.fromMap(device)).toList(); }); _connectionStream = _connectionEventChannel.receiveBroadcastStream(); _dataStream = _dataEventChannel.receiveBroadcastStream(); _statusStream = _statusEventChannel.receiveBroadcastStream(); } /// 初始化BLE服务 Future initialize() async { try { final result = await _methodChannel.invokeMethod('initialize'); return result ?? false; } catch (e) { print('初始化BLE服务失败: $e'); return false; } } /// 注册BLE回调 Future registerCallback() async { try { final result = await _methodChannel.invokeMethod('registerCallback'); return result ?? false; } catch (e) { print('注册BLE回调失败: $e'); return false; } } /// 设置BLE方法回调处理器 void setMethodCallHandler(Future Function(MethodCall call) handler) { _methodChannel.setMethodCallHandler(handler); } /// 开始扫描设备 Future startScan({int timeout = 10000}) async { try { final result = await _methodChannel.invokeMethod( 'startScan', {'timeout': timeout}, ); return result ?? false; } catch (e) { print('开始扫描设备失败: $e'); return false; } } /// 停止扫描设备 Future stopScan() async { try { final result = await _methodChannel.invokeMethod('stopScan'); return result ?? false; } catch (e) { print('停止扫描设备失败: $e'); return false; } } /// 连接到设备 Future connect(String address) async { try { final result = await _methodChannel.invokeMethod( 'connect', {'address': address}, ); return result ?? false; } catch (e) { print('连接设备失败: $e'); return false; } } /// 断开设备连接 Future disconnect() async { try { final result = await _methodChannel.invokeMethod('disconnect'); return result ?? false; } catch (e) { print('断开设备连接失败: $e'); return false; } } /// 检查是否已连接 Future isConnected() async { try { final result = await _methodChannel.invokeMethod('isConnected'); return result ?? false; } catch (e) { print('检查连接状态失败: $e'); return false; } } /// 获取连接状态 Future getConnectionState() async { try { final result = await _methodChannel.invokeMethod('getConnectionState'); return result ?? BleConnectionState.disconnected; } catch (e) { print('获取连接状态失败: $e'); return BleConnectionState.disconnected; } } /// 获取已连接设备地址 Future getConnectedDeviceAddress() async { try { return await _methodChannel.invokeMethod('getConnectedDeviceAddress'); } catch (e) { print('获取已连接设备地址失败: $e'); return null; } } /// 获取电池电量 Future getBatteryLevel() async { try { final result = await _methodChannel.invokeMethod('getBatteryLevel'); return result ?? 0; } catch (e) { print('获取电池电量失败: $e'); return 0; } } /// 获取固件版本 Future getFirmwareVersion() async { try { return await _methodChannel.invokeMethod('getFirmwareVersion'); } catch (e) { print('获取固件版本失败: $e'); return null; } } /// 获取设备版本信息 Future getDeviceVersion() async { try { final result = await _methodChannel.invokeMethod('getDeviceVersion'); return result ?? false; } catch (e) { print('获取设备版本失败: $e'); return false; } } /// 获取电池信息 Future getBatteryInfo() async { try { final result = await _methodChannel.invokeMethod('getBatteryInfo'); return result ?? false; } catch (e) { print('获取电池信息失败: $e'); return false; } } /// 打开编码器 Future openEncoder() async { try { final result = await _methodChannel.invokeMethod('openEncoder'); return result ?? false; } catch (e) { print('打开编码器失败: $e'); return false; } } /// 打开解码器 Future openDecoder() async { try { final result = await _methodChannel.invokeMethod('openDecoder'); return result ?? false; } catch (e) { print('打开解码器失败: $e'); return false; } } /// 关闭编解码器 Future closeCodec() async { try { final result = await _methodChannel.invokeMethod('closeCodec'); return result ?? false; } catch (e) { print('关闭编解码器失败: $e'); return false; } } /// 播放A2DP Future playA2DP() async { try { final result = await _methodChannel.invokeMethod('playA2DP'); return result ?? false; } catch (e) { print('播放A2DP失败: $e'); return false; } } /// 增加音量 Future volumeUp() async { try { final result = await _methodChannel.invokeMethod('volumeUp'); return result ?? false; } catch (e) { print('增加音量失败: $e'); return false; } } /// 减小音量 Future volumeDown() async { try { final result = await _methodChannel.invokeMethod('volumeDown'); return result ?? false; } catch (e) { print('减小音量失败: $e'); return false; } } /// 发送音频数据 Future sendAudioData(Uint8List data) async { try { final result = await _methodChannel.invokeMethod( 'sendAudioData', {'data': data}, ); return result ?? false; } catch (e) { print('发送音频数据失败: $e'); return false; } } /// 清理资源 Future cleanup() async { try { final result = await _methodChannel.invokeMethod('cleanup'); return result ?? false; } catch (e) { print('清理资源失败: $e'); return false; } } /// 获取已扫描到的设备列表 Future> getScannedDevices() async { try { final result = await _methodChannel.invokeMethod>('getScannedDevices'); if (result != null) { return result.map((item) => BleDevice.fromMap(Map.from(item as Map))).toList(); } return []; } catch (e) { print('获取扫描设备列表失败: $e'); return []; } } /// 获取平台版本 Future getPlatformVersion() async { try { return await _methodChannel.invokeMethod('getPlatformVersion'); } catch (e) { print('获取平台版本失败: $e'); return null; } } }