You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
4.3 KiB
138 lines
4.3 KiB
import 'dart:async';
|
|
import 'package:get/get.dart';
|
|
import 'package:classic_bluetooth/classic_bluetooth.dart';
|
|
import '../../core/utils/logger.dart';
|
|
|
|
// 重新导出蓝牙设备类型,让外部可以通过BluetoothManager访问
|
|
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;
|
|
}
|
|
} 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) {
|
|
// 直接从事件中获取设备信息
|
|
final disconnectedDevice = event.device!;
|
|
|
|
Logger.info('经典蓝牙设备已断开: ${disconnectedDevice.name} (${disconnectedDevice.address})');
|
|
|
|
// 如果断开的是当前设备,清除当前设备
|
|
if (currentDeviceRx.value?.address == disconnectedDevice.address) {
|
|
currentDeviceRx.value = null;
|
|
Logger.info('已清除当前设备引用');
|
|
}
|
|
}
|
|
},
|
|
onError: (error) {
|
|
Logger.error('蓝牙事件流错误: ${error.toString()}');
|
|
},
|
|
);
|
|
} catch (e) {
|
|
Logger.error('设置蓝牙事件监听器失败: ${e.toString()}');
|
|
}
|
|
}
|
|
|
|
/// 刷新设备连接状态
|
|
Future<void> refreshDeviceConnection() async {
|
|
try {
|
|
|
|
|
|
// 如果BLE未连接,则检查经典蓝牙连接
|
|
final devices = await ClassicBluetooth.getConnectedDevices();
|
|
|
|
if (devices.isNotEmpty) {
|
|
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 {
|
|
final isEnabled = await ClassicBluetooth.isBluetoothEnabled();
|
|
_bluetoothState.value = isEnabled ? 'on' : 'off';
|
|
return isEnabled;
|
|
} catch (e) {
|
|
Logger.error('检查蓝牙状态失败: ${e.toString()}');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
}
|