|
|
|
@ -9,59 +9,58 @@ export 'package:classic_bluetooth/classic_bluetooth.dart' show BluetoothDevice; |
|
|
|
/// 蓝牙服务 - 管理经典蓝牙服务 |
|
|
|
class BluetoothManager extends GetxService { |
|
|
|
static BluetoothManager get to => Get.find<BluetoothManager>(); |
|
|
|
|
|
|
|
|
|
|
|
// 蓝牙状态 |
|
|
|
final _bluetoothState = 'unknown'.obs; |
|
|
|
String get bluetoothState => _bluetoothState.value; |
|
|
|
RxString get bluetoothStateRx => _bluetoothState; |
|
|
|
|
|
|
|
|
|
|
|
// 当前经典蓝牙设备 |
|
|
|
final Rx<BluetoothDevice?> currentDeviceRx = Rx<BluetoothDevice?>(null); |
|
|
|
BluetoothDevice? get currentDevice => currentDeviceRx.value; |
|
|
|
|
|
|
|
|
|
|
|
// 订阅 |
|
|
|
StreamSubscription<BluetoothEvent>? _bluetoothSubscription; |
|
|
|
|
|
|
|
|
|
|
|
/// 初始化 |
|
|
|
@override |
|
|
|
void onInit() { |
|
|
|
super.onInit(); |
|
|
|
Logger.info('蓝牙服务初始化'); |
|
|
|
|
|
|
|
|
|
|
|
// 设置蓝牙事件监听 |
|
|
|
_setupBluetoothListener(); |
|
|
|
|
|
|
|
|
|
|
|
// 主动检查当前已连接的设备和蓝牙状态 |
|
|
|
Future.delayed(Duration.zero, () async { |
|
|
|
Logger.info('检查初始蓝牙连接状态'); |
|
|
|
|
|
|
|
|
|
|
|
// 先检查蓝牙是否开启 |
|
|
|
await isBluetoothEnabled(); |
|
|
|
|
|
|
|
|
|
|
|
// 然后刷新设备连接 |
|
|
|
await refreshDeviceConnection(); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 释放资源 |
|
|
|
@override |
|
|
|
void onClose() { |
|
|
|
_bluetoothSubscription?.cancel(); |
|
|
|
super.onClose(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 设置经典蓝牙事件监听 |
|
|
|
void _setupBluetoothListener() { |
|
|
|
_bluetoothSubscription?.cancel(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
_bluetoothSubscription = ClassicBluetooth.events.listen( |
|
|
|
(event) { |
|
|
|
if (event.type == 'stateChanged' && event.state != null) { |
|
|
|
_bluetoothState.value = event.state!; |
|
|
|
Logger.info('蓝牙状态变更: ${event.state}'); |
|
|
|
|
|
|
|
|
|
|
|
// 如果蓝牙关闭,清除设备 |
|
|
|
if (event.state == 'off') { |
|
|
|
currentDeviceRx.value = null; |
|
|
|
@ -69,17 +68,19 @@ class BluetoothManager extends GetxService { |
|
|
|
} else if (event.type == 'deviceConnected' && event.device != null) { |
|
|
|
// 直接从事件中获取设备信息 |
|
|
|
final newDevice = event.device!; |
|
|
|
|
|
|
|
|
|
|
|
// 更新当前设备 |
|
|
|
currentDeviceRx.value = newDevice; |
|
|
|
|
|
|
|
|
|
|
|
Logger.info('经典蓝牙设备已连接: ${newDevice.name} (${newDevice.address})'); |
|
|
|
} else if (event.type == 'deviceDisconnected' && event.device != null) { |
|
|
|
} else if (event.type == 'deviceDisconnected' && |
|
|
|
event.device != null) { |
|
|
|
// 直接从事件中获取设备信息 |
|
|
|
final disconnectedDevice = event.device!; |
|
|
|
|
|
|
|
Logger.info('经典蓝牙设备已断开: ${disconnectedDevice.name} (${disconnectedDevice.address})'); |
|
|
|
|
|
|
|
|
|
|
|
Logger.info( |
|
|
|
'经典蓝牙设备已断开: ${disconnectedDevice.name} (${disconnectedDevice.address})'); |
|
|
|
|
|
|
|
// 如果断开的是当前设备,清除当前设备 |
|
|
|
if (currentDeviceRx.value?.address == disconnectedDevice.address) { |
|
|
|
currentDeviceRx.value = null; |
|
|
|
@ -95,34 +96,36 @@ class BluetoothManager extends GetxService { |
|
|
|
Logger.error('设置蓝牙事件监听器失败: ${e.toString()}'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 刷新设备连接状态 |
|
|
|
Future<void> refreshDeviceConnection() async { |
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
// 如果BLE未连接,则检查经典蓝牙连接 |
|
|
|
final devices = await ClassicBluetooth.getConnectedDevices(); |
|
|
|
|
|
|
|
|
|
|
|
if (devices.isNotEmpty) { |
|
|
|
for (var device in devices) { |
|
|
|
if (device.isActive) { |
|
|
|
// 如果有活动设备,直接使用它 |
|
|
|
currentDeviceRx.value = device; |
|
|
|
Logger.info('已连接经典蓝牙设备: ${device.name} (${device.address})'); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
final newDevice = devices.first; |
|
|
|
|
|
|
|
// 更新当前设备 |
|
|
|
currentDeviceRx.value = newDevice; |
|
|
|
|
|
|
|
Logger.info('已连接经典蓝牙设备: ${newDevice.name} (${newDevice.address})'); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 没有任何连接 |
|
|
|
currentDeviceRx.value = null; |
|
|
|
Logger.info('未找到已连接的蓝牙设备'); |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
Logger.error('刷新设备连接失败: ${e.toString()}'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 检查蓝牙是否已启用 |
|
|
|
Future<bool> isBluetoothEnabled() async { |
|
|
|
try { |
|
|
|
@ -134,5 +137,4 @@ class BluetoothManager extends GetxService { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|