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.
338 lines
9.0 KiB
338 lines
9.0 KiB
import 'package:get/get.dart';
|
|
import 'dart:async';
|
|
import '../../../routes/app_routes.dart';
|
|
import '../../../data/services/classic_bluetooth_service.dart';
|
|
import '../../../data/services/bluetooth_media_button_service.dart';
|
|
import '../../../data/services/voice_interaction_service.dart';
|
|
import '../../../core/utils/logger.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
// 连接状态枚举
|
|
enum ConnectionStatus {
|
|
disconnected, // 未连接
|
|
connecting, // 连接中
|
|
connected, // 已连接
|
|
failed // 连接失败
|
|
}
|
|
|
|
class HomeController extends GetxController {
|
|
static HomeController get to => Get.find();
|
|
|
|
// 经典蓝牙服务
|
|
late ClassicBluetoothService _bluetoothService;
|
|
|
|
// 蓝牙媒体按钮服务
|
|
late BluetoothMediaButtonService _mediaButtonService;
|
|
|
|
// 媒体按钮订阅
|
|
StreamSubscription<MediaButtonEvent>? _mediaButtonSubscription;
|
|
|
|
// 设备连接状态
|
|
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;
|
|
|
|
// 当前连接的经典蓝牙设备
|
|
List<BluetoothDevice> get connectedClassicDevices => _bluetoothService.connectedDevices;
|
|
|
|
// 蓝牙状态
|
|
RxString get bluetoothState => _bluetoothService.bluetoothState;
|
|
|
|
|
|
// 状态更新标志,防止多次重复更新
|
|
bool _isUpdatingStatus = false;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
|
|
// 初始化蓝牙服务和状态监控
|
|
_initBluetoothService();
|
|
|
|
// 初始化蓝牙媒体按钮服务
|
|
// _initMediaButtonService();
|
|
|
|
// 电池模拟更新(实际应用中可以通过蓝牙获取)
|
|
_simulateBatteryUpdate();
|
|
}
|
|
|
|
// 初始化蓝牙服务
|
|
void _initBluetoothService() {
|
|
try {
|
|
// 获取经典蓝牙服务实例
|
|
_bluetoothService = Get.find<ClassicBluetoothService>();
|
|
|
|
// 更新当前连接状态
|
|
_updateConnectionStatus();
|
|
|
|
// 监听蓝牙连接状态变化
|
|
_setupBluetoothListeners();
|
|
|
|
} catch (e) {
|
|
Logger.error('初始化蓝牙服务失败: $e');
|
|
_connectionStatus.value = ConnectionStatus.failed;
|
|
}
|
|
}
|
|
|
|
// 初始化蓝牙媒体按钮服务
|
|
void _initMediaButtonService() {
|
|
try {
|
|
// 获取媒体按钮服务实例
|
|
_mediaButtonService = Get.find<BluetoothMediaButtonService>();
|
|
|
|
// 初始化媒体按钮服务
|
|
_mediaButtonService.initialize().then((success) {
|
|
if (success) {
|
|
// 启动按钮监听
|
|
_mediaButtonService.startButtonListening();
|
|
|
|
// 设置按钮事件监听
|
|
_setupMediaButtonListener();
|
|
|
|
Logger.info('蓝牙媒体按钮服务初始化成功');
|
|
} else {
|
|
Logger.warning('蓝牙媒体按钮服务初始化失败');
|
|
}
|
|
});
|
|
} catch (e) {
|
|
Logger.error('初始化蓝牙媒体按钮服务失败: $e');
|
|
}
|
|
}
|
|
|
|
// 设置媒体按钮事件监听
|
|
void _setupMediaButtonListener() {
|
|
// 取消之前的订阅
|
|
_mediaButtonSubscription?.cancel();
|
|
|
|
// 设置新的订阅
|
|
_mediaButtonSubscription = _mediaButtonService.buttonEvents.listen((event) {
|
|
Logger.info('收到媒体按钮事件: $event');
|
|
|
|
// 处理按钮事件
|
|
if (event.buttonType == MediaButtonType.playPause) {
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
// 电池电量模拟更新
|
|
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() {
|
|
// 如果正在更新,则跳过
|
|
if (_isUpdatingStatus) return;
|
|
|
|
_isUpdatingStatus = true;
|
|
|
|
try {
|
|
// 检查当前设备
|
|
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刷新
|
|
} finally {
|
|
_isUpdatingStatus = false;
|
|
}
|
|
}
|
|
|
|
// 设置蓝牙监听器
|
|
void _setupBluetoothListeners() {
|
|
// 只监听当前设备和蓝牙状态
|
|
|
|
// 监听当前设备状态
|
|
ever(_bluetoothService.currentDevice, (device) {
|
|
if (device != null) {
|
|
_isConnected.value = true;
|
|
_connectionStatus.value = ConnectionStatus.connected;
|
|
} else if (_isConnected.value) {
|
|
// 如果之前连接着设备但现在变为null,那么设备已断开
|
|
_isConnected.value = false;
|
|
_connectionStatus.value = ConnectionStatus.disconnected;
|
|
_deviceBattery.value = null;
|
|
}
|
|
update();
|
|
});
|
|
|
|
// 蓝牙状态变化监听
|
|
ever(_bluetoothService.bluetoothState, (state) {
|
|
if (state == 'off') {
|
|
_isConnected.value = false;
|
|
_connectionStatus.value = ConnectionStatus.disconnected;
|
|
_deviceBattery.value = null;
|
|
update();
|
|
}
|
|
});
|
|
}
|
|
|
|
// 刷新设备信息
|
|
Future<void> refreshDeviceInfo() async {
|
|
return;
|
|
// 如果正在更新,则跳过
|
|
if (_isUpdatingStatus) return;
|
|
|
|
_isUpdatingStatus = true;
|
|
|
|
try {
|
|
// 设置状态为连接中
|
|
_connectionStatus.value = ConnectionStatus.connecting;
|
|
update();
|
|
|
|
// 刷新设备列表
|
|
final hasDevices = await _bluetoothService.refreshConnectedDevices();
|
|
|
|
// 更新状态
|
|
_updateConnectionStatus();
|
|
|
|
// 根据结果更新状态
|
|
if (!hasDevices) {
|
|
Logger.info('刷新设备:未找到设备');
|
|
_connectionErrorMsg.value = '未找到已连接的设备';
|
|
} else {
|
|
_connectionErrorMsg.value = '';
|
|
Logger.info('刷新设备:成功');
|
|
}
|
|
} catch (e) {
|
|
Logger.error('刷新设备信息失败: $e');
|
|
_connectionStatus.value = ConnectionStatus.failed;
|
|
_connectionErrorMsg.value = '无法获取设备信息';
|
|
update();
|
|
} finally {
|
|
_isUpdatingStatus = false;
|
|
}
|
|
}
|
|
|
|
// 重新扫描设备
|
|
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() {
|
|
// 如果蓝牙关闭,优先显示蓝牙状态
|
|
if (_bluetoothService.bluetoothState.value == 'off') {
|
|
return '蓝牙已关闭';
|
|
}
|
|
|
|
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 ?? '未知';
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
void onClose() {
|
|
// 取消按钮事件流订阅
|
|
_mediaButtonSubscription?.cancel();
|
|
|
|
super.onClose();
|
|
}
|
|
}
|