|
|
|
@ -11,27 +11,27 @@ import '../../../core/utils/permission_util.dart'; |
|
|
|
|
|
|
|
/// 配对状态枚举 - 简化版本 |
|
|
|
enum PairingState { |
|
|
|
idle, // 空闲/初始状态 |
|
|
|
checking, // 检查中(蓝牙、权限等) |
|
|
|
waitingClassic, // 等待经典蓝牙连接 |
|
|
|
verifying, // 验证设备 |
|
|
|
associating, // 正在进行BLE设备关联 |
|
|
|
waitingBle, // 等待BLE连接 |
|
|
|
completed, // 配对完成 |
|
|
|
error, // 错误状态 |
|
|
|
idle, // 空闲/初始状态 |
|
|
|
checking, // 检查中(蓝牙、权限等) |
|
|
|
waitingClassic, // 等待经典蓝牙连接 |
|
|
|
verifying, // 验证设备 |
|
|
|
associating, // 正在进行BLE设备关联 |
|
|
|
waitingBle, // 等待BLE连接 |
|
|
|
completed, // 配对完成 |
|
|
|
error, // 错误状态 |
|
|
|
} |
|
|
|
|
|
|
|
/// 错误类型枚举 |
|
|
|
enum ErrorType { |
|
|
|
none, |
|
|
|
bluetoothOff, // 蓝牙关闭 |
|
|
|
noPermission, // 无权限 |
|
|
|
noClassicDevice, // 无经典蓝牙设备 |
|
|
|
deviceNotMatch, // 设备不匹配 |
|
|
|
deviceBound, // 设备已被绑定 |
|
|
|
bluetoothOff, // 蓝牙关闭 |
|
|
|
noPermission, // 无权限 |
|
|
|
noClassicDevice, // 无经典蓝牙设备 |
|
|
|
deviceNotMatch, // 设备不匹配 |
|
|
|
deviceBound, // 设备已被绑定 |
|
|
|
associationFailed, // 关联失败 |
|
|
|
bleTimeout, // BLE连接超时 |
|
|
|
networkError, // 网络错误 |
|
|
|
bleTimeout, // BLE连接超时 |
|
|
|
networkError, // 网络错误 |
|
|
|
} |
|
|
|
|
|
|
|
/// 配对控制器 - 基于简化状态机的实现 |
|
|
|
@ -122,32 +122,27 @@ class PairingController extends GetxController { |
|
|
|
|
|
|
|
/// 监听蓝牙开关状态 |
|
|
|
void _setupBluetoothStateListener() { |
|
|
|
_workers.add( |
|
|
|
ever(_bluetoothManager.bluetoothStateRx, (String state) { |
|
|
|
Logger.d(_tag, '蓝牙状态变化: $state'); |
|
|
|
_handleBluetoothStateChange(state); |
|
|
|
}) |
|
|
|
); |
|
|
|
_workers.add(ever(_bluetoothManager.bluetoothStateRx, (String state) { |
|
|
|
Logger.d(_tag, '蓝牙状态变化: $state'); |
|
|
|
_handleBluetoothStateChange(state); |
|
|
|
})); |
|
|
|
} |
|
|
|
|
|
|
|
/// 监听经典蓝牙连接状态 |
|
|
|
void _setupClassicConnectionListener() { |
|
|
|
_workers.add( |
|
|
|
ever(_bluetoothManager.currentDeviceRx, (BluetoothDevice? device) { |
|
|
|
Logger.d(_tag, '经典蓝牙设备变化: ${device?.name}'); |
|
|
|
_handleClassicDeviceChange(device); |
|
|
|
}) |
|
|
|
); |
|
|
|
_workers |
|
|
|
.add(ever(_bluetoothManager.currentDeviceRx, (BluetoothDevice? device) { |
|
|
|
Logger.d(_tag, '经典蓝牙设备变化: ${device?.name}'); |
|
|
|
_handleClassicDeviceChange(device); |
|
|
|
})); |
|
|
|
} |
|
|
|
|
|
|
|
/// 监听BLE连接状态 |
|
|
|
void _setupBleConnectionListener() { |
|
|
|
_workers.add( |
|
|
|
ever(_bleManager.connectionStateRx, (int state) { |
|
|
|
Logger.d(_tag, 'BLE连接状态变化: $state'); |
|
|
|
_handleBleStateChange(state); |
|
|
|
}) |
|
|
|
); |
|
|
|
_workers.add(ever(_bleManager.connectionStateRx, (int state) { |
|
|
|
Logger.d(_tag, 'BLE连接状态变化: $state'); |
|
|
|
_handleBleStateChange(state); |
|
|
|
})); |
|
|
|
} |
|
|
|
|
|
|
|
// ==================== 状态处理 ==================== |
|
|
|
@ -209,7 +204,8 @@ class PairingController extends GetxController { |
|
|
|
/// 处理BLE状态变化 |
|
|
|
void _handleBleStateChange(int state) { |
|
|
|
if (_currentState.value == PairingState.waitingBle) { |
|
|
|
if (state == 2) { // BLE已连接 |
|
|
|
if (state == 2) { |
|
|
|
// BLE已连接 |
|
|
|
_bleTimeoutTimer?.cancel(); |
|
|
|
_transition(PairingState.completed); |
|
|
|
} |
|
|
|
@ -371,7 +367,6 @@ class PairingController extends GetxController { |
|
|
|
_currentClassicDevice = classicDevice; |
|
|
|
_transition(PairingState.verifying); |
|
|
|
await _verifyDevice(classicDevice); |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
Logger.e(_tag, '状态检查失败: $e'); |
|
|
|
_setError(ErrorType.networkError, '检查失败,请重试'); |
|
|
|
@ -388,8 +383,8 @@ class PairingController extends GetxController { |
|
|
|
final location = await Permission.locationWhenInUse.request(); |
|
|
|
|
|
|
|
return bluetoothScan.isGranted && |
|
|
|
bluetoothConnect.isGranted && |
|
|
|
location.isGranted; |
|
|
|
bluetoothConnect.isGranted && |
|
|
|
location.isGranted; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
@ -405,7 +400,8 @@ class PairingController extends GetxController { |
|
|
|
|
|
|
|
// 检查设备名称是否包含 DeepSound 或 DeapSound |
|
|
|
final deviceName = device.name ?? ''; |
|
|
|
if (deviceName.contains('DeepSound') || deviceName.contains('DeapSound')) { |
|
|
|
if (deviceName.contains('DeepSound') || |
|
|
|
deviceName.contains('DeapSound')) { |
|
|
|
Logger.i(_tag, '设备验证通过'); |
|
|
|
_verifiedDeviceAddress = device.address; |
|
|
|
|
|
|
|
@ -413,13 +409,15 @@ class PairingController extends GetxController { |
|
|
|
final pairedMacAddress = await _bleManager.getPairedMacAddress(); |
|
|
|
Logger.i(_tag, '已关联的MAC地址: $pairedMacAddress'); |
|
|
|
|
|
|
|
if (pairedMacAddress != null && pairedMacAddress.toUpperCase() == device.address.toUpperCase()) { |
|
|
|
if (pairedMacAddress != null && |
|
|
|
pairedMacAddress.toUpperCase() == device.address.toUpperCase()) { |
|
|
|
// 已有关联,检查BLE连接状态 |
|
|
|
Logger.i(_tag, '设备已关联,检查BLE连接状态'); |
|
|
|
final currentBleState = await _bleManager.getConnectionState(); |
|
|
|
Logger.i(_tag, '验证时BLE状态: $currentBleState'); |
|
|
|
|
|
|
|
if (currentBleState == 2) { // BleConnectionState.connected |
|
|
|
if (currentBleState == 2) { |
|
|
|
// BleConnectionState.connected |
|
|
|
Logger.i(_tag, 'BLE已连接,直接完成配对'); |
|
|
|
_transition(PairingState.completed); |
|
|
|
} else { |
|
|
|
@ -452,7 +450,7 @@ class PairingController extends GetxController { |
|
|
|
|
|
|
|
// 调用BLE管理器的关联方法 |
|
|
|
final success = await _bleManager.startDeviceAssociation( |
|
|
|
deviceNamePattern: 'DeapSound', // 或者使用 _currentClassicDevice?.name |
|
|
|
deviceNamePattern: 'DeapSound', // 或者使用 _currentClassicDevice?.name |
|
|
|
macAddress: _verifiedDeviceAddress!, |
|
|
|
); |
|
|
|
|
|
|
|
@ -484,7 +482,8 @@ class PairingController extends GetxController { |
|
|
|
final currentBleState = await _bleManager.getConnectionState(); |
|
|
|
Logger.i(_tag, 'BLE当前连接状态: $currentBleState'); |
|
|
|
|
|
|
|
if (currentBleState == 2) { // BleConnectionState.connected |
|
|
|
if (currentBleState == 2) { |
|
|
|
// BleConnectionState.connected |
|
|
|
Logger.i(_tag, 'BLE已经连接,直接完成配对'); |
|
|
|
_bleTimeoutTimer?.cancel(); |
|
|
|
_transition(PairingState.completed); |
|
|
|
@ -493,7 +492,8 @@ class PairingController extends GetxController { |
|
|
|
|
|
|
|
// 注册后台扫描以开始BLE配对 |
|
|
|
if (_verifiedDeviceAddress != null) { |
|
|
|
final success = await _bleManager.registerBackgroundScan(macAddress: _verifiedDeviceAddress); |
|
|
|
final success = await _bleManager.registerBackgroundScan( |
|
|
|
macAddress: _verifiedDeviceAddress); |
|
|
|
if (!success && _currentState.value == PairingState.waitingBle) { |
|
|
|
_setError(ErrorType.bleTimeout, 'BLE配对启动失败'); |
|
|
|
} |
|
|
|
@ -505,7 +505,8 @@ class PairingController extends GetxController { |
|
|
|
} |
|
|
|
|
|
|
|
/// 导航到主页 |
|
|
|
void _navigateToHome() { |
|
|
|
void _navigateToHome() async { |
|
|
|
await BleManager.to.startAgentService(); |
|
|
|
Future.delayed(const Duration(seconds: 1), () { |
|
|
|
Get.offAllNamed(Routes.home); |
|
|
|
}); |
|
|
|
@ -563,7 +564,8 @@ class PairingController extends GetxController { |
|
|
|
} |
|
|
|
|
|
|
|
/// 用户点击跳过按钮 |
|
|
|
void onSkipTap() { |
|
|
|
void onSkipTap() async { |
|
|
|
await BleManager.to.startAgentService(); |
|
|
|
Logger.i(_tag, '用户选择跳过配对'); |
|
|
|
Get.offAllNamed(Routes.home); |
|
|
|
} |
|
|
|
|