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.
231 lines
6.1 KiB
231 lines
6.1 KiB
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 'package:flutter/material.dart';
|
|
import '../../explore/controllers/explore_controller.dart';
|
|
|
|
// 连接状态枚举
|
|
enum ConnectionStatus {
|
|
disconnected, // 未连接
|
|
connecting, // 连接中
|
|
connected, // 已连接
|
|
failed // 连接失败
|
|
}
|
|
|
|
class HomeController extends GetxController {
|
|
static HomeController get to => Get.find();
|
|
final ExploreController exploreController = Get.find<ExploreController>();
|
|
|
|
// 经典蓝牙服务
|
|
final ClassicBluetoothService _bluetoothService = Get.find<ClassicBluetoothService>();
|
|
|
|
// 连接状态
|
|
final _connectionStatus = ConnectionStatus.disconnected.obs;
|
|
ConnectionStatus get connectionStatus => _connectionStatus.value;
|
|
|
|
// 电池状态(模拟数据)
|
|
final _deviceBattery = Rx<int?>(null);
|
|
int? get deviceBattery => _deviceBattery.value;
|
|
|
|
// 固件版本(模拟数据)
|
|
final String firmwareVersion = 'v2.4.1';
|
|
|
|
// 连接错误信息
|
|
final _connectionErrorMsg = ''.obs;
|
|
String get connectionErrorMsg => _connectionErrorMsg.value;
|
|
|
|
// 从蓝牙服务中直接获取的属性
|
|
Rx<BluetoothDevice?> get currentDevice => _bluetoothService.currentDevice;
|
|
List<BluetoothDevice> get connectedDevices => _bluetoothService.connectedDevices;
|
|
RxString get bluetoothState => _bluetoothService.bluetoothState;
|
|
|
|
// 蓝牙是否已连接
|
|
bool get isConnected => currentDevice.value != null;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
|
|
// 初始化
|
|
_initBluetooth();
|
|
|
|
// 电池模拟更新
|
|
_simulateBatteryUpdate();
|
|
}
|
|
|
|
// 初始化蓝牙
|
|
Future<void> _initBluetooth() async {
|
|
try {
|
|
// 确保蓝牙服务已初始化
|
|
if (!(await _bluetoothService.initialize())) {
|
|
_connectionStatus.value = ConnectionStatus.failed;
|
|
_connectionErrorMsg.value = '初始化蓝牙服务失败';
|
|
return;
|
|
}
|
|
|
|
// 设置监听器
|
|
_setupBluetoothListeners();
|
|
|
|
// 更新初始状态
|
|
_updateConnectionStatus();
|
|
|
|
} catch (e) {
|
|
Logger.error('初始化蓝牙失败: $e');
|
|
_connectionStatus.value = ConnectionStatus.failed;
|
|
_connectionErrorMsg.value = e.toString();
|
|
}
|
|
}
|
|
|
|
// 设置蓝牙状态监听
|
|
void _setupBluetoothListeners() {
|
|
// 监听当前设备变化
|
|
ever(currentDevice, (_) {
|
|
_updateConnectionStatus();
|
|
});
|
|
|
|
// 监听蓝牙状态变化
|
|
ever(bluetoothState, (_) {
|
|
_updateConnectionStatus();
|
|
});
|
|
}
|
|
|
|
// 更新连接状态
|
|
void _updateConnectionStatus() {
|
|
if (bluetoothState.value == 'off') {
|
|
_connectionStatus.value = ConnectionStatus.disconnected;
|
|
_deviceBattery.value = null;
|
|
update();
|
|
return;
|
|
}
|
|
|
|
if (currentDevice.value != null) {
|
|
_connectionStatus.value = ConnectionStatus.connected;
|
|
} else {
|
|
_connectionStatus.value = ConnectionStatus.disconnected;
|
|
_deviceBattery.value = null;
|
|
}
|
|
|
|
update();
|
|
}
|
|
|
|
// 刷新设备连接
|
|
Future<void> refreshDeviceConnection() async {
|
|
try {
|
|
_connectionStatus.value = ConnectionStatus.connecting;
|
|
update();
|
|
|
|
final success = await _bluetoothService.refreshConnectedDevices();
|
|
|
|
if (!success) {
|
|
_connectionErrorMsg.value = '未找到已连接的设备';
|
|
} else {
|
|
_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.value == '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.value?.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.value?.address ?? 'unknown'.tr; // 未知
|
|
}
|
|
}
|
|
|