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 BleService { static final BleService _instance = BleService._internal(); /// 获取单例 factory BleService() => _instance; // 方法通道 final MethodChannel _methodChannel = const MethodChannel('com.yunqiinnovation.ble_service'); // 事件通道 - 简化为只使用数据和状态通道 late final EventChannel _dataEventChannel; late final EventChannel _statusEventChannel; // 事件流 late final Stream _dataStream; late final Stream _statusStream; // 获取事件流 Stream get onDataReceived => _dataStream; // 状态流直接传递原始数据 Stream get onStatusUpdated => _statusStream; // 内部构造函数 BleService._internal() { _dataEventChannel = const EventChannel('com.yunqiinnovation.ble_service/data_events'); _statusEventChannel = const EventChannel('com.yunqiinnovation.ble_service/status_events'); // 初始化事件流 _dataStream = _dataEventChannel.receiveBroadcastStream(); _statusStream = _statusEventChannel.receiveBroadcastStream(); } /// 检查是否已连接 Future isConnected() async { try { final result = await _methodChannel.invokeMethod('isConnected'); return result ?? false; } catch (e) { print('检查连接状态失败: $e'); return false; } } /// 获取电池电量 Future getBatteryLevel() async { try { final result = await _methodChannel.invokeMethod('getBatteryLevel'); return result ?? 0; } catch (e) { print('获取电池电量失败: $e'); return 0; } } /// 获取设备版本信息 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; } } /// 启动BLE后台服务 /// [config] 配置参数,可包含: /// - scan_interval: 扫描间隔时间(毫秒) /// - scan_duration: 扫描持续时间(毫秒) /// - notification_title: 通知标题 /// - notification_content: 通知内容 /// - mac: 设备地址 /// - device_name: 设备名称 Future startBleService(Map config) async { try { final result = await _methodChannel.invokeMethod('startBleService', config); return result ?? false; } catch (e) { print('启动BLE后台服务失败: $e'); return false; } } /// 停止BLE后台服务 Future stopBleService() async { try { final result = await _methodChannel.invokeMethod('stopBleService'); return result ?? false; } catch (e) { print('停止BLE后台服务失败: $e'); return false; } } /// 获取已连接设备地址 - 使用其他方式获取设备地址,不再直接调用此方法 Future getConnectedDeviceAddress() async { // 简化版处理,返回空值 return null; } }