import 'package:get/get.dart'; import 'dart:async'; import '../../../routes/app_routes.dart'; import '../../../core/utils/logger.dart'; import 'package:flutter/material.dart'; import '../../explore/controllers/explore_controller.dart'; import 'package:classic_bluetooth/classic_bluetooth.dart'; // 连接状态枚举 enum ConnectionStatus { disconnected, // 未连接 connecting, // 连接中 connected, // 已连接 failed // 连接失败 } class HomeController extends GetxController { static HomeController get to => Get.find(); final ExploreController exploreController = Get.find(); // 连接状态 final _connectionStatus = ConnectionStatus.disconnected.obs; ConnectionStatus get connectionStatus => _connectionStatus.value; // 电池状态(模拟数据) final _deviceBattery = Rx(null); int? get deviceBattery => _deviceBattery.value; // 固件版本(模拟数据) final String firmwareVersion = 'v2.4.1'; // 连接错误信息 final _connectionErrorMsg = ''.obs; String get connectionErrorMsg => _connectionErrorMsg.value; // 蓝牙状态和设备信息 final _bluetoothState = 'unknown'.obs; String get bluetoothState => _bluetoothState.value; final Rx _currentDevice = Rx(null); BluetoothDevice? get currentDevice => _currentDevice.value; // 蓝牙事件订阅 StreamSubscription? _bluetoothSubscription; // 蓝牙是否已连接 bool get isConnected => currentDevice != null; @override void onInit() { super.onInit(); // 初始化 _initBluetooth(); // 电池模拟更新 _simulateBatteryUpdate(); } @override void onClose() { _bluetoothSubscription?.cancel(); super.onClose(); } // 初始化蓝牙 Future _initBluetooth() async { try { _connectionStatus.value = ConnectionStatus.connecting; // 检查蓝牙状态 final isEnabled = await ClassicBluetooth.isBluetoothEnabled(); _bluetoothState.value = isEnabled ? 'on' : 'off'; if (!isEnabled) { _connectionStatus.value = ConnectionStatus.disconnected; _connectionErrorMsg.value = '蓝牙未开启'; return; } // 设置蓝牙事件监听 _setupBluetoothListener(); // 获取已连接设备 await refreshDeviceConnection(); } catch (e) { Logger.error('初始化蓝牙失败: $e'); _connectionStatus.value = ConnectionStatus.failed; _connectionErrorMsg.value = e.toString(); } } // 设置蓝牙事件监听 void _setupBluetoothListener() { _bluetoothSubscription?.cancel(); _bluetoothSubscription = ClassicBluetooth.events.listen((event) { if (event.type == 'stateChanged' && event.state != null) { _bluetoothState.value = event.state!; // 如果蓝牙关闭,清空设备信息 if (event.state == 'off') { _currentDevice.value = null; _connectionErrorMsg.value = '蓝牙未开启'; } _updateConnectionStatus(); } else if (event.type == 'deviceConnected' && event.device != null) { final newDevice = event.device!; // 更新当前设备 _currentDevice.value = newDevice; _connectionErrorMsg.value = ''; _updateConnectionStatus(); } else if (event.type == 'deviceDisconnected' && event.device != null) { final disconnectedDevice = event.device!; // 如果断开的是当前设备,清除当前设备 if (_currentDevice.value?.address == disconnectedDevice.address) { _currentDevice.value = null; _connectionErrorMsg.value = '设备已断开连接'; } _updateConnectionStatus(); } }, onError: (error) { Logger.error('蓝牙事件流错误: $error'); }); } // 更新连接状态 void _updateConnectionStatus() { if (bluetoothState == 'off') { _connectionStatus.value = ConnectionStatus.disconnected; _deviceBattery.value = null; update(); return; } if (currentDevice != null) { _connectionStatus.value = ConnectionStatus.connected; } else { _connectionStatus.value = ConnectionStatus.disconnected; _deviceBattery.value = null; } update(); } // 刷新设备连接 Future refreshDeviceConnection() async { try { _connectionStatus.value = ConnectionStatus.connecting; update(); final devices = await ClassicBluetooth.getConnectedDevices(); if (devices.isNotEmpty) { _currentDevice.value = devices.first; _connectionErrorMsg.value = ''; } else { _currentDevice.value = null; _connectionErrorMsg.value = '未找到已连接的设备'; } } catch (e) { Logger.error('刷新设备连接失败: $e'); _connectionErrorMsg.value = '无法刷新设备连接'; } finally { _updateConnectionStatus(); } } // 电池电量模拟更新 void _simulateBatteryUpdate() { Future.delayed(const Duration(seconds: 2), () { if (isConnected) { final timestamp = DateTime.now().millisecondsSinceEpoch; _deviceBattery.value = 50 + (timestamp % 50); // 50-99的随机值 } else { _deviceBattery.value = null; } // 递归调用实现定时更新 _simulateBatteryUpdate(); }); } // 重新扫描设备 void rescanDevices() { Get.toNamed(Routes.pairing); } // 打开翻译功能 void openTranslation() { Get.toNamed(Routes.translation); } // 打开AI伴侣功能 void openAIGirlfriend() { exploreController.onAIFeatureTap(2); //进入AI女友页面 } // 展示AI功能 void exploreAIFeatures() { Get.toNamed(Routes.chat); } // 获取连接状态对应的颜色 Color getConnectionStatusColor() { switch (_connectionStatus.value) { case ConnectionStatus.connected: return Colors.green; case ConnectionStatus.connecting: return Colors.amber; case ConnectionStatus.failed: return Colors.red; case ConnectionStatus.disconnected: default: return Colors.grey; } } // 获取连接状态对应的文本 String getConnectionStatusText() { // 如果蓝牙关闭,优先显示蓝牙状态 if (bluetoothState == 'off') { return 'bluetoothOff'.tr; // 蓝牙已关闭 } switch (_connectionStatus.value) { case ConnectionStatus.connected: return 'connected'.tr; // 已连接 case ConnectionStatus.connecting: return 'connecting'.tr; // 连接中... case ConnectionStatus.failed: return 'connectionFailure'.tr; // 连接失败 case ConnectionStatus.disconnected: default: return 'unconnected'.tr; // 未连接 } } // 获取设备名称 String getDeviceName() { return currentDevice?.name ?? 'unknownEquipment'.tr; // 未知设备 } // 获取信号强度(模拟数据) String getSignalStrength() { if (!isConnected) { return 'zeroSignal'.tr; // 无信号 } // 简单模拟信号强度 final random = DateTime.now().millisecondsSinceEpoch % 3; switch (random) { case 0: return 'good'.tr; // 良好 case 1: return 'fine'.tr; // 很好 default: return 'excellent'.tr; // 极佳 } } // 获取设备ID String getDeviceId() { return currentDevice?.address ?? 'unknown'.tr; // 未知 } }