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.

186 lines
5.5 KiB

import 'package:get/get.dart';
import 'dart:async';
import '../../../routes/app_routes.dart';
import '../../../data/services/classic_bluetooth_service.dart' as classic;
import '../../../core/utils/logger.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
/// 简化的配对控制器,仅用于显示已连接经典蓝牙设备和处理基本配对流程
class PairingController extends GetxController {
static PairingController get to => Get.find();
// 经典蓝牙服务
late classic.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<classic.BluetoothDevice?> get currentDevice => _bluetoothService.currentDevice;
// 连接设备列表
RxList<classic.BluetoothDevice> get connectedDevices => _bluetoothService.connectedDevices;
// 错误信息
RxString get errorMessage => _bluetoothService.errorMessage;
// 连接设备列表(兼容旧代码)
List<classic.BluetoothDevice> get connectedClassicDevices =>
_bluetoothService.currentDevice.value != null
? [_bluetoothService.currentDevice.value!]
: [];
// 状态描述(用于UI显示)
final statusDescription = '正在初始化...'.obs;
// 加载状态
final isLoading = false.obs;
@override
void onInit() {
super.onInit();
_initialize();
}
@override
void onReady() {
super.onReady();
// 设置连接监听器,确保在页面完全加载后设置,避免过早监听可能导致的问题
_setupDeviceListeners();
}
/// 设置设备监听器
void _setupDeviceListeners() {
try {
if (!_isBluetoothServiceAvailable.value) return;
// 监听设备连接事件
_bluetoothService.onDeviceConnected((device) {
Logger.info('检测到蓝牙设备连接: ${device.name}');
// 更新状态并跳转到主页
statusDescription.value = '已连接到设备: ${device.name}';
// 立即跳转到主页
_navigateToHome();
});
} catch (e) {
Logger.error('设置设备监听器失败: $e');
}
}
/// 立即跳转到主页
void _navigateToHome() {
Get.offAllNamed(Routes.home);
}
/// 完整的初始化流程
Future<void> _initialize() async {
try {
isLoading.value = true;
statusDescription.value = '正在初始化...';
// 1. 获取蓝牙服务
_bluetoothService = Get.find<classic.ClassicBluetoothService>();
// 确保蓝牙服务已初始化
if (!await _bluetoothService.initialize()) {
Logger.error('蓝牙服务初始化失败');
statusDescription.value = '蓝牙服务初始化失败';
_isBluetoothServiceAvailable.value = false;
isLoading.value = false;
return;
}
// 2. 检查权限
statusDescription.value = '正在检查权限...';
final hasPermission = await _bluetoothService.checkAndRequestPermissions();
if (!hasPermission) {
statusDescription.value = '缺少蓝牙权限';
isLoading.value = false;
return;
}
// 3. 刷新设备列表
statusDescription.value = '正在搜索设备...';
await refreshConnectedDevices();
// 4. 处理结果
if (_bluetoothService.connectedDevices.isNotEmpty) {
statusDescription.value = '找到已连接的设备';
// 延迟跳转到主页
_autoNavigateToHome();
} else {
// 没有找到设备
statusDescription.value = '未找到已连接的蓝牙设备';
}
} 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();
if (hasDevices) {
statusDescription.value = '找到已连接的设备';
// 延迟跳转到主页
_autoNavigateToHome();
} else {
// 没有找到设备
statusDescription.value = '未找到已连接的蓝牙设备';
}
} catch (e) {
Logger.error('刷新设备失败: $e');
statusDescription.value = '刷新设备失败';
} finally {
isLoading.value = false;
}
}
/// 自动跳转到主页
void _autoNavigateToHome() {
if (_bluetoothService.connectedDevices.isEmpty) return;
Logger.info('已找到连接的设备,自动跳转到主页');
Future.delayed(const Duration(milliseconds: 500), () {
Get.offAllNamed(Routes.home);
});
}
/// 打开系统蓝牙设置
void openBluetoothSettings() {
_bluetoothService.openBluetoothSettings();
}
/// 刷新设备(兼容旧代码)
Future<void> refreshDevices() async {
await refreshConnectedDevices();
}
/// 跳过连接
void skipConnecting() {
Get.offAllNamed(Routes.home);
}
}