import 'package:app_settings/app_settings.dart'; import 'package:get/get.dart'; import 'dart:async'; import '../../../routes/app_routes.dart'; import '../../../data/services/classic_bluetooth_service.dart'; import '../../../core/utils/logger.dart'; import 'dart:io'; import '../../../data/services/voice_interaction_service.dart'; /// 简化的配对控制器,仅用于显示已连接经典蓝牙设备和处理基本配对流程 class PairingController extends GetxController { static PairingController get to => Get.find(); // 经典蓝牙服务 final ClassicBluetoothService _bluetoothService = Get.find(); // 语音交互服务 final VoiceInteractionService _voiceInteractionService = Get.find(); // 是否开始搜索 final _isSearching = false.obs; bool get isSearching => _isSearching.value; // 设备连接状态 final _isConnecting = false.obs; bool get isConnecting => _isConnecting.value; // 蓝牙服务是否可用 final _isBluetoothServiceAvailable = true.obs; bool get isBluetoothServiceAvailable => _isBluetoothServiceAvailable.value; // 当前连接设备 Rx get currentDevice => _bluetoothService.currentDevice; // 连接设备列表 RxList get connectedDevices => _bluetoothService.connectedDevices; // 蓝牙状态 RxString get bluetoothState => _bluetoothService.bluetoothState; // 错误信息 final errorMessage = ''.obs; // 状态描述(用于UI显示) final statusDescription = 'initializing'.tr.obs; // 正在初始化... // 加载状态 final isLoading = false.obs; // 是否正在跳转到主页 bool _isNavigatingToHome = false; @override void onInit() { super.onInit(); _initialize(); } /// 设置状态变化监听 void _setupStatusListeners() { if (!_isBluetoothServiceAvailable.value) return; // 监听蓝牙开关状态 ever(_bluetoothService.bluetoothState, (state) { _updateStatusDescription(); }); // 监听当前设备状态 ever(_bluetoothService.currentDevice, (device) { _updateStatusDescription(); // 如果有设备连接,自动跳转到主页 if (device != null) { _autoNavigateToHome(); } }); } /// 更新状态描述 void _updateStatusDescription() { if (_bluetoothService.bluetoothState.value == 'off') { statusDescription.value = 'bluetoothTurnedOff'.tr;// 蓝牙已关闭, 请打开蓝牙 return; } if (_bluetoothService.currentDevice.value != null) { statusDescription.value = '${'connectedDevice'.tr} ${_bluetoothService.currentDevice.value!.name}';// 已连接到设备: return; } if (_bluetoothService.connectedDevices.isNotEmpty) { statusDescription.value = '${'findConnectedDevice'.tr} ${_bluetoothService.connectedDevices.first.name}';// 找到已连接的设备: return; } if (isLoading.value) { statusDescription.value = 'searchingDevice'.tr; // 正在搜索设备...; return; } statusDescription.value = 'notFound'.tr;//未找到已连接的蓝牙设备 } /// 初始化流程 Future _initialize() async { try { // 初始化语音交互服务 _voiceInteractionService.initialize(); isLoading.value = true; statusDescription.value = 'initializing'.tr; // 正在初始化...; // 确保蓝牙服务已初始化 if (!await _bluetoothService.initialize()) { Logger.error('蓝牙服务初始化失败'); statusDescription.value = 'bluetoothInitializationFailure'.tr; // 蓝牙服务初始化失败 _isBluetoothServiceAvailable.value = false; isLoading.value = false; return; } _setupStatusListeners(); // 检查权限 if (Platform.isAndroid) { statusDescription.value = 'checkingPermissions'.tr; final hasPermission = await _bluetoothService.checkPermissions(); if (!hasPermission) { statusDescription.value = '缺少蓝牙权限'; isLoading.value = false; return; } } // 刷新设备列表 statusDescription.value = '正在搜索设备...'; await refreshConnectedDevices(); // 更新状态描述 _updateStatusDescription(); } catch (e) { Logger.error('初始化配对控制器失败: $e'); statusDescription.value = 'initializationFailure'.tr; // 初始化失败 _isBluetoothServiceAvailable.value = false; } finally { isLoading.value = false; } } /// 刷新设备 Future refreshConnectedDevices() async { try { isLoading.value = true; statusDescription.value = 'searchingDevice'.tr; // 正在搜索设备... final hasDevices = await _bluetoothService.refreshConnectedDevices(); // 更新状态描述 _updateStatusDescription(); } catch (e) { Logger.error('刷新设备失败: $e'); statusDescription.value = 'failedRefreshDevice'.tr; // 刷新设备失败 } finally { isLoading.value = false; } } /// 自动跳转到主页 void _autoNavigateToHome() { // 如果已经在跳转或设备列表为空,则不执行 if (_isNavigatingToHome || _bluetoothService.connectedDevices.isEmpty) return; // 设置跳转标志 _isNavigatingToHome = true; Logger.info('已找到连接的设备,自动跳转到主页'); Future.delayed(const Duration(milliseconds: 500), () { Get.offAllNamed(Routes.home); }); } /// 打开系统蓝牙设置 void openBluetoothSettings() { try { // 使用app_settings插件打开蓝牙设置 AppSettings.openAppSettings(type: AppSettingsType.bluetooth); } catch (e) { Logger.error('打开蓝牙设置失败: $e'); } } /// 刷新设备(兼容旧代码) Future refreshDevices() async { await refreshConnectedDevices(); } /// 跳过连接 void skipConnecting() { // 设置跳转标志防止自动跳转再次触发 _isNavigatingToHome = true; Get.offAllNamed(Routes.home); } }