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.

313 lines
8.9 KiB

import 'package:app_settings/app_settings.dart';
import 'package:get/get.dart';
import 'dart:async';
import 'package:permission_handler/permission_handler.dart';
import '../../../routes/app_routes.dart';
import '../../../core/utils/logger.dart';
import 'dart:io';
import '../../../data/services/bluetooth_manager.dart';
import 'package:classic_bluetooth/classic_bluetooth.dart' show BluetoothDevice;
import '../../../data/services/ble_manager.dart';
/// 配对控制器,负责检查蓝牙权限、状态,并处理配对流程
class PairingController extends GetxController {
static PairingController get to => Get.find();
// 状态标志
final isLoading = false.obs;
// 权限是否已授予
final _hasPermissions = false.obs;
bool get hasPermissions => _hasPermissions.value;
// 蓝牙状态
final _bluetoothState = 'unknown'.obs;
String get bluetoothState => _bluetoothState.value;
bool get isBluetoothOn => _bluetoothState.value == 'on';
// 状态描述(用于UI显示)
final statusDescription = 'initializing'.tr.obs;
// 是否正在跳转到主页
bool _isNavigatingToHome = false;
// 当前连接设备信息
BluetoothDevice? get currentDevice => _bluetoothService.currentDevice;
// 是否已连接设备
bool get isDeviceConnected => currentDevice != null;
// 服务状态监听
Worker? _bluetoothStateWorker;
Worker? _deviceWorker;
// 蓝牙服务
final _bluetoothService = Get.find<BluetoothManager>();
@override
void onInit() {
super.onInit();
_initialize();
}
@override
void onClose() {
_bluetoothStateWorker?.dispose();
_deviceWorker?.dispose();
super.onClose();
}
/// 初始化流程
Future<void> _initialize() async {
try {
isLoading.value = true;
statusDescription.value = 'checkingPermissions'.tr;
// 检查并请求权限
if (!await _checkAndRequestPermissions()) {
statusDescription.value = 'permissionDenied'.tr;
return;
}
// 设置服务状态监听
_setupServiceStateListeners();
// 检查蓝牙状态
statusDescription.value = 'checkingBluetoothStatus'.tr;
final isEnabled = await _bluetoothService.isBluetoothEnabled();
_bluetoothState.value = isEnabled ? 'on' : 'off';
if (!isEnabled) {
statusDescription.value = 'bluetoothTurnedOff'.tr;
return;
}
// 刷新已连接设备
statusDescription.value = 'searchingDevice'.tr;
// 设备连接时启动BLE后台服务
_startBleService();
await refreshConnectedDevices();
} catch (e) {
Logger.error('初始化配对控制器失败: $e');
statusDescription.value = 'initializationFailure'.tr;
} finally {
isLoading.value = false;
_updateStatusDescription();
}
}
/// 检查并请求必要的蓝牙权限
Future<bool> _checkAndRequestPermissions() async {
Map<Permission, PermissionStatus> statuses;
List<Permission> permissionsToRequest = [];
if (Platform.isAndroid) {
// Android 12+ (SDK 31+) 需要 SCAN 和 CONNECT
// Android 6+ (SDK 23+) 需要 LOCATION
permissionsToRequest.add(Permission.locationWhenInUse); // 基本定位权限
permissionsToRequest.add(Permission.microphone);
// Android 12及以上版本才需要请求蓝牙扫描和连接权限
if (await _getAndroidSdkVersion() >= 31) {
permissionsToRequest.add(Permission.bluetoothScan);
permissionsToRequest.add(Permission.bluetoothConnect);
}
} else if (Platform.isIOS) {
permissionsToRequest.add(Permission.bluetooth);
}
statuses = await permissionsToRequest.request();
bool allGranted = true;
statuses.forEach((permission, status) {
if (!status.isGranted) {
allGranted = false;
Logger.warning('权限 ${permission.toString()} 未被授予: $status');
}
});
_hasPermissions.value = allGranted;
return allGranted;
}
// 辅助方法获取Android SDK版本
Future<int> _getAndroidSdkVersion() async {
if (Platform.isAndroid) {
// 这里需要原生代码支持来获取确切的SDK版本,
// 或者使用 device_info_plus 插件
// 暂时返回一个较高值以触发新权限请求
return 31; // 假设运行在需要新权限的设备上
}
return 0;
}
/// 设置服务状态监听
void _setupServiceStateListeners() {
// 监听蓝牙服务的蓝牙状态变化
_bluetoothStateWorker = ever(_bluetoothService.bluetoothState.obs, (String state) {
_bluetoothState.value = state;
_updateStatusDescription();
// 如果蓝牙关闭,可能需要更新UI或提示用户
if (state == 'off') {
Logger.info('蓝牙已关闭');
}
});
// 监听当前设备变化 - 使用公开的currentDeviceRx字段
_deviceWorker = ever(_bluetoothService.currentDeviceRx, (BluetoothDevice? device) {
if (device != null && !_isNavigatingToHome) {
Logger.info('配对页面发现设备已连接: ${device.name}');
_autoNavigateToHome();
}
_updateStatusDescription();
});
}
/// 更新状态描述
void _updateStatusDescription() {
if (!hasPermissions) {
statusDescription.value = 'permissionDenied'.tr;
return;
}
if (bluetoothState == 'off') {
statusDescription.value = 'bluetoothTurnedOff'.tr;
return;
}
if (isDeviceConnected) {
// 有设备连接时,显示设备名称
statusDescription.value = '${'connectedDevice'.tr} ${currentDevice!.name}';
return;
}
if (isLoading.value) {
statusDescription.value = 'searchingDevice'.tr;
return;
}
statusDescription.value = 'notFound'.tr;
}
/// 刷新已连接设备
Future<void> refreshConnectedDevices() async {
if (!hasPermissions) {
statusDescription.value = 'permissionDenied'.tr;
return;
}
try {
isLoading.value = true;
statusDescription.value = 'searchingDevice'.tr;
// 使用蓝牙服务刷新连接
await _bluetoothService.refreshDeviceConnection();
// 检查连接状态
if (isDeviceConnected) {
Logger.info('刷新发现设备已连接: ${currentDevice!.name}');
_autoNavigateToHome();
} else {
Logger.info('刷新未发现已连接设备');
}
} catch (e) {
Logger.error('刷新设备失败: $e');
statusDescription.value = 'failedRefreshDevice'.tr;
} finally {
isLoading.value = false;
_updateStatusDescription();
}
}
/// 自动跳转到主页
void _autoNavigateToHome() {
if (_isNavigatingToHome) return;
// 如果没有连接,不进行跳转
if (!isDeviceConnected) return;
_isNavigatingToHome = true;
Logger.info('已找到连接的设备 ${currentDevice!.name},自动跳转到主页');
Future.delayed(const Duration(milliseconds: 500), () {
Get.offAllNamed(Routes.home);
});
}
/// 启动BLE后台服务
Future<void> _startBleService() async {
try {
// if (!isDeviceConnected) return;
// 确保麦克风权限已被授予
final microphoneStatus = await Permission.microphone.status;
if (!microphoneStatus.isGranted) {
Logger.warning('麦克风权限未授予,语音交互需要此权限');
final result = await Permission.microphone.request();
if (!result.isGranted) {
Logger.error('麦克风权限被拒绝,语音功能将无法正常工作');
}
}
// 启动BLE服务,无需传递配置参数,将从.env读取
final result = await BleManager.to.startBleService();
if (result) {
Logger.info('BLE后台服务启动成功');
} else {
Logger.error('BLE后台服务启动失败');
}
} catch (e) {
Logger.error('启动BLE后台服务发生错误: $e');
}
}
/// 停止BLE后台服务
Future<void> _stopBleService() async {
try {
final result = await BleManager.to.stopBleService();
if (result) {
Logger.info('BLE后台服务停止成功');
} else {
Logger.error('BLE后台服务停止失败');
}
} catch (e) {
Logger.error('停止BLE后台服务发生错误: $e');
}
}
/// 打开系统蓝牙设置
void openBluetoothSettings() {
try {
if (!hasPermissions) {
openAppSettings();
} else if (Platform.isAndroid && !isBluetoothOn) {
AppSettings.openAppSettings(type: AppSettingsType.bluetooth);
} else if (Platform.isIOS) {
openAppSettings();
} else {
AppSettings.openAppSettings(type: AppSettingsType.bluetooth);
}
} catch (e) {
Logger.error('打开设置失败: $e');
}
}
/// 打开应用设置
Future<void> openAppSettings() async {
await AppSettings.openAppSettings();
}
/// 跳过连接
void skipConnecting() {
_isNavigatingToHome = true;
Get.offAllNamed(Routes.home);
}
}