|
|
|
@ -2,6 +2,7 @@ import 'dart:async'; |
|
|
|
import 'package:get/get.dart'; |
|
|
|
import 'package:classic_bluetooth/classic_bluetooth.dart'; |
|
|
|
import '../../core/utils/logger.dart'; |
|
|
|
import '../../routes/app_routes.dart'; |
|
|
|
|
|
|
|
// 重新导出蓝牙设备类型,让外部可以通过BluetoothManager访问 |
|
|
|
export 'package:classic_bluetooth/classic_bluetooth.dart' show BluetoothDevice; |
|
|
|
@ -58,6 +59,7 @@ class BluetoothManager extends GetxService { |
|
|
|
try { |
|
|
|
_bluetoothSubscription = ClassicBluetooth.events.listen( |
|
|
|
(event) { |
|
|
|
print("_setupBluetoothListener$event"); |
|
|
|
if (event.type == 'stateChanged' && event.state != null) { |
|
|
|
_bluetoothState.value = event.state!; |
|
|
|
Logger.info('蓝牙状态变更: ${event.state}'); |
|
|
|
@ -70,9 +72,15 @@ class BluetoothManager extends GetxService { |
|
|
|
// 直接从事件中获取设备信息 |
|
|
|
final newDevice = event.device!; |
|
|
|
|
|
|
|
// 更新当前设备 |
|
|
|
currentDeviceRx.value = newDevice; |
|
|
|
|
|
|
|
// 检查当前是否在OTA升级界面,如果是则不跳转 |
|
|
|
if (!_isInOtaUpgradePage()) { |
|
|
|
Get.toNamed(Routes.pairing, arguments: newDevice); |
|
|
|
} else { |
|
|
|
Logger.info('当前在OTA升级界面,跳过配对页面跳转'); |
|
|
|
} |
|
|
|
// 正常模式:更新当前设备并触发监听器 |
|
|
|
Logger.info('经典蓝牙设备已连接: ${newDevice.name} (${newDevice.address})'); |
|
|
|
} else if (event.type == 'deviceDisconnected' && |
|
|
|
event.device != null) { |
|
|
|
@ -138,4 +146,16 @@ class BluetoothManager extends GetxService { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 检查当前是否在OTA升级界面 |
|
|
|
/// 返回true表示当前在OTA界面,不应该跳转到配对页面 |
|
|
|
bool _isInOtaUpgradePage() { |
|
|
|
try { |
|
|
|
final currentRoute = Get.currentRoute; |
|
|
|
return currentRoute == Routes.OTA_UPGRADE; |
|
|
|
} catch (e) { |
|
|
|
Logger.error('检查当前路由失败: ${e.toString()}'); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|