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.0 KiB
211 lines
6.0 KiB
import 'package:app_settings/app_settings.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 'dart:io';
|
|
import '../../../data/services/voice_interaction_service.dart';
|
|
/// 简化的配对控制器,仅用于显示已连接经典蓝牙设备和处理基本配对流程
|
|
class PairingController extends GetxController {
|
|
static PairingController get to => Get.find();
|
|
|
|
|
|
|
|
// 经典蓝牙服务
|
|
final ClassicBluetoothService _bluetoothService =
|
|
Get.find<ClassicBluetoothService>();
|
|
|
|
// 语音交互服务
|
|
final VoiceInteractionService _voiceInteractionService =
|
|
Get.find<VoiceInteractionService>();
|
|
|
|
// 是否开始搜索
|
|
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;
|
|
|
|
// 状态描述(用于UI显示)
|
|
final statusDescription = 'initializing'.tr.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 = 'bluetoothTurnedOff'.tr;// 蓝牙已关闭, 请打开蓝牙
|
|
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.initialize();
|
|
|
|
isLoading.value = true;
|
|
statusDescription.value = '正在初始化...';
|
|
|
|
// 确保蓝牙服务已初始化
|
|
if (!await _bluetoothService.initialize()) {
|
|
Logger.error('蓝牙服务初始化失败');
|
|
statusDescription.value =
|
|
'bluetoothInitializationFailure'.tr; // 蓝牙服务初始化失败
|
|
_isBluetoothServiceAvailable.value = false;
|
|
isLoading.value = false;
|
|
return;
|
|
}
|
|
_setupStatusListeners();
|
|
// 检查权限
|
|
if (Platform.isAndroid) {
|
|
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 = 'initializationFailure'.tr; // 初始化失败
|
|
_isBluetoothServiceAvailable.value = false;
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
/// 刷新设备
|
|
Future<void> refreshConnectedDevices() async {
|
|
try {
|
|
isLoading.value = true;
|
|
statusDescription.value = 'searchingDevice'.tr; // 正在搜索设备...
|
|
|
|
final hasDevices = await _bluetoothService.refreshConnectedDevices();
|
|
|
|
// 更新状态描述
|
|
_updateStatusDescription();
|
|
} catch (e) {
|
|
Logger.error('刷新设备失败: $e');
|
|
statusDescription.value = 'failedRefreshDevice'.tr; // 刷新设备失败
|
|
} 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);
|
|
}
|
|
}
|
|
|