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.

211 lines
6.2 KiB

import 'package:deep_voice/data/services/voice_interaction_service.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 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:app_settings/app_settings.dart';
import 'package:flutter/services.dart';
/// 简化的配对控制器,仅用于显示已连接经典蓝牙设备和处理基本配对流程
class PairingController extends GetxController {
static PairingController get to => Get.find();
// 经典蓝牙服务
late ClassicBluetoothService _bluetoothService;
// 是否开始搜索
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<BluetoothDevice?> get currentDevice => _bluetoothService.currentDevice;
// 连接设备列表
RxList<BluetoothDevice> get connectedDevices => _bluetoothService.connectedDevices;
// 蓝牙状态
RxString get bluetoothState => _bluetoothService.bluetoothState;
// 错误信息
final errorMessage = ''.obs;
// 连接设备列表(兼容旧代码)
List<BluetoothDevice> get connectedClassicDevices =>
_bluetoothService.currentDevice.value != null
? [_bluetoothService.currentDevice.value!]
: [];
// 状态描述(用于UI显示)
final statusDescription = '正在初始化...'.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 = '蓝牙已关闭,请打开蓝牙';
return;
}
if (_bluetoothService.currentDevice.value != null) {
statusDescription.value = '已连接到设备: ${_bluetoothService.currentDevice.value!.name}';
return;
}
if (_bluetoothService.connectedDevices.isNotEmpty) {
statusDescription.value = '找到已连接的设备: ${_bluetoothService.connectedDevices.first.name}';
return;
}
if (isLoading.value) {
statusDescription.value = '正在搜索设备...';
return;
}
statusDescription.value = '未找到已连接的蓝牙设备';
}
/// 初始化流程
Future<void> _initialize() async {
try {
// 初始化语音交互服务
VoiceInteractionService.to.initialize();
VoiceInteractionService.to.startService();
isLoading.value = true;
statusDescription.value = '正在初始化...';
// 获取蓝牙服务
_bluetoothService = Get.find<ClassicBluetoothService>();
// 确保蓝牙服务已初始化
if (!await _bluetoothService.initialize()) {
Logger.error('蓝牙服务初始化失败');
statusDescription.value = '蓝牙服务初始化失败';
_isBluetoothServiceAvailable.value = false;
isLoading.value = false;
return;
}
_setupStatusListeners();
// 检查权限
// statusDescription.value = '正在检查权限...';
// 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 = '初始化失败';
_isBluetoothServiceAvailable.value = false;
} finally {
isLoading.value = false;
}
}
/// 刷新设备
Future<void> refreshConnectedDevices() async {
try {
isLoading.value = true;
statusDescription.value = '正在搜索设备...';
final hasDevices = await _bluetoothService.refreshConnectedDevices();
// 更新状态描述
_updateStatusDescription();
} catch (e) {
Logger.error('刷新设备失败: $e');
statusDescription.value = '刷新设备失败';
} 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<void> refreshDevices() async {
await refreshConnectedDevices();
}
/// 跳过连接
void skipConnecting() {
// 设置跳转标志防止自动跳转再次触发
_isNavigatingToHome = true;
Get.offAllNamed(Routes.home);
}
}