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.
255 lines
7.2 KiB
255 lines
7.2 KiB
import 'package:get/get.dart';
|
|
import '../../../routes/app_routes.dart';
|
|
import '../../../data/services/classic_bluetooth_service.dart' as classic;
|
|
import '../../../core/utils/logger.dart';
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart' hide BluetoothDevice;
|
|
import 'package:flutter/material.dart';
|
|
import '../../../data/services/voice_interaction_service.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
// 连接状态枚举
|
|
enum ConnectionStatus {
|
|
disconnected, // 未连接
|
|
connecting, // 连接中
|
|
connected, // 已连接
|
|
failed // 连接失败
|
|
}
|
|
|
|
class HomeController extends GetxController {
|
|
static HomeController get to => Get.find();
|
|
|
|
// 经典蓝牙服务
|
|
late classic.ClassicBluetoothService _bluetoothService;
|
|
|
|
// 设备连接状态
|
|
final _isConnected = false.obs;
|
|
bool get isConnected => _isConnected.value;
|
|
|
|
// 连接状态
|
|
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 _deviceInfo = <String, dynamic>{}.obs;
|
|
Map<String, dynamic> get deviceInfo => _deviceInfo.value;
|
|
|
|
// 连接错误信息
|
|
final _connectionErrorMsg = ''.obs;
|
|
String get connectionErrorMsg => _connectionErrorMsg.value;
|
|
|
|
// 当前连接的经典蓝牙设备
|
|
final _connectedClassicDevices = <classic.BluetoothDevice>[].obs;
|
|
List<classic.BluetoothDevice> get connectedClassicDevices => _connectedClassicDevices;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
|
|
// 初始化蓝牙服务和状态监控
|
|
_initBluetoothService();
|
|
|
|
// 电池模拟更新(实际应用中可以通过蓝牙获取)
|
|
_simulateBatteryUpdate();
|
|
}
|
|
|
|
// 初始化蓝牙服务
|
|
void _initBluetoothService() {
|
|
try {
|
|
// 获取经典蓝牙服务实例
|
|
_bluetoothService = Get.find<classic.ClassicBluetoothService>();
|
|
|
|
// 更新当前连接状态
|
|
_updateConnectionStatus();
|
|
|
|
// 监听蓝牙连接状态变化
|
|
_setupBluetoothListeners();
|
|
|
|
} catch (e) {
|
|
Logger.error('初始化蓝牙服务失败: $e');
|
|
_connectionStatus.value = ConnectionStatus.failed;
|
|
}
|
|
}
|
|
|
|
// 电池电量模拟更新
|
|
void _simulateBatteryUpdate() {
|
|
// 每5分钟更新一次电池电量(模拟)
|
|
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 _updateConnectionStatus() {
|
|
// 检查当前设备
|
|
final currentDevice = _bluetoothService.currentDevice.value;
|
|
|
|
if (currentDevice != null) {
|
|
_isConnected.value = true;
|
|
_connectionStatus.value = ConnectionStatus.connected;
|
|
} else {
|
|
_isConnected.value = false;
|
|
_connectionStatus.value = ConnectionStatus.disconnected;
|
|
}
|
|
|
|
update(); // 通知UI刷新
|
|
}
|
|
|
|
// 设置蓝牙监听器
|
|
void _setupBluetoothListeners() {
|
|
// 监听设备连接
|
|
_bluetoothService.onDeviceConnected((device) {
|
|
Logger.info('监听到蓝牙设备连接: ${device.name}');
|
|
_isConnected.value = true;
|
|
_connectionStatus.value = ConnectionStatus.connected;
|
|
update();
|
|
});
|
|
|
|
// 监听设备断开
|
|
_bluetoothService.onDeviceDisconnected((device) {
|
|
Logger.info('监听到蓝牙设备断开: ${device.name}');
|
|
_isConnected.value = false;
|
|
_connectionStatus.value = ConnectionStatus.disconnected;
|
|
_deviceBattery.value = null;
|
|
update();
|
|
});
|
|
|
|
// 监听蓝牙状态变化
|
|
_bluetoothService.onBluetoothStateChanged((state) {
|
|
Logger.info('监听到蓝牙状态变化: $state');
|
|
|
|
if (state == 'off') {
|
|
_isConnected.value = false;
|
|
_connectionStatus.value = ConnectionStatus.disconnected;
|
|
_deviceBattery.value = null;
|
|
update();
|
|
}
|
|
});
|
|
}
|
|
|
|
// 刷新设备信息
|
|
Future<void> refreshDeviceInfo() async {
|
|
try {
|
|
// 设置状态为连接中
|
|
_connectionStatus.value = ConnectionStatus.connecting;
|
|
update();
|
|
|
|
// 刷新设备列表
|
|
final hasDevices = await _bluetoothService.refreshConnectedDevices();
|
|
|
|
// 更新状态
|
|
_updateConnectionStatus();
|
|
|
|
// 根据结果更新状态
|
|
if (!hasDevices) {
|
|
Logger.info('刷新设备:未找到设备');
|
|
// 这里可以设置状态变量而不是使用snackbar
|
|
_connectionErrorMsg.value = _bluetoothService.errorMessage.value.isEmpty
|
|
? '未找到已连接的设备'
|
|
: _bluetoothService.errorMessage.value;
|
|
} else {
|
|
_connectionErrorMsg.value = '';
|
|
Logger.info('刷新设备:成功');
|
|
}
|
|
} catch (e) {
|
|
Logger.error('刷新设备信息失败: $e');
|
|
_connectionStatus.value = ConnectionStatus.failed;
|
|
_connectionErrorMsg.value = '无法获取设备信息';
|
|
update();
|
|
}
|
|
}
|
|
|
|
// 重新扫描设备
|
|
void rescanDevices() {
|
|
Get.toNamed(Routes.pairing);
|
|
}
|
|
|
|
// 打开翻译功能
|
|
void openTranslation() {
|
|
Get.toNamed(Routes.translation);
|
|
}
|
|
|
|
// 打开AI伴侣功能
|
|
void openAIGirlfriend() {
|
|
Get.toNamed(Routes.chat);
|
|
}
|
|
|
|
// 展示AI功能
|
|
void exploreAIFeatures() {
|
|
// 简单实现,直接跳转到AI聊天页面
|
|
openAIGirlfriend();
|
|
}
|
|
|
|
// 获取连接状态对应的颜色
|
|
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() {
|
|
switch (_connectionStatus.value) {
|
|
case ConnectionStatus.connected:
|
|
return '已连接';
|
|
case ConnectionStatus.connecting:
|
|
return '连接中...';
|
|
case ConnectionStatus.failed:
|
|
return '连接失败';
|
|
case ConnectionStatus.disconnected:
|
|
default:
|
|
return '未连接';
|
|
}
|
|
}
|
|
|
|
// 获取设备名称
|
|
String getDeviceName() {
|
|
final device = _bluetoothService.currentDevice.value;
|
|
return device?.name ?? '未知设备';
|
|
}
|
|
|
|
// 获取信号强度(模拟数据)
|
|
String getSignalStrength() {
|
|
if (!_isConnected.value) {
|
|
return '无信号';
|
|
}
|
|
|
|
// 简单模拟信号强度
|
|
final random = DateTime.now().millisecondsSinceEpoch % 3;
|
|
switch (random) {
|
|
case 0:
|
|
return '良好';
|
|
case 1:
|
|
return '很好';
|
|
default:
|
|
return '极佳';
|
|
}
|
|
}
|
|
|
|
// 获取设备ID
|
|
String getDeviceId() {
|
|
final device = _bluetoothService.currentDevice.value;
|
|
return device?.address ?? '未知';
|
|
}
|
|
}
|