|
|
@ -14,6 +14,8 @@ import 'package:ble_service/ble_service.dart'; |
|
|
class PairingController extends GetxController { |
|
|
class PairingController extends GetxController { |
|
|
static PairingController get to => Get.find(); |
|
|
static PairingController get to => Get.find(); |
|
|
|
|
|
|
|
|
|
|
|
// 需要设备 |
|
|
|
|
|
final bool needdeivice = false; |
|
|
// 状态标志 |
|
|
// 状态标志 |
|
|
final isLoading = false.obs; |
|
|
final isLoading = false.obs; |
|
|
|
|
|
|
|
|
@ -28,7 +30,7 @@ class PairingController extends GetxController { |
|
|
|
|
|
|
|
|
// 状态描述(用于UI显示) |
|
|
// 状态描述(用于UI显示) |
|
|
final statusDescription = 'initializing'.tr.obs; |
|
|
final statusDescription = 'initializing'.tr.obs; |
|
|
|
|
|
|
|
|
// 按钮文本 |
|
|
// 按钮文本 |
|
|
final actionButtonText = '去系统设置'.obs; |
|
|
final actionButtonText = '去系统设置'.obs; |
|
|
|
|
|
|
|
|
@ -117,7 +119,7 @@ class PairingController extends GetxController { |
|
|
// 刷新连接设备并检查配对状态 |
|
|
// 刷新连接设备并检查配对状态 |
|
|
statusDescription.value = 'searchingDevice'.tr; |
|
|
statusDescription.value = 'searchingDevice'.tr; |
|
|
await _bluetoothService.refreshDeviceConnection(); |
|
|
await _bluetoothService.refreshDeviceConnection(); |
|
|
|
|
|
|
|
|
// 设备连接时检查配对状态 |
|
|
// 设备连接时检查配对状态 |
|
|
if (isDeviceConnected) { |
|
|
if (isDeviceConnected) { |
|
|
await _checkDevicePairingStatus(); |
|
|
await _checkDevicePairingStatus(); |
|
|
@ -146,7 +148,7 @@ class PairingController extends GetxController { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Logger.info('发现已关联的Companion设备: $pairedMac'); |
|
|
Logger.info('发现已关联的Companion设备: $pairedMac'); |
|
|
|
|
|
|
|
|
// 检查当前连接设备与已保存的设备是否一致 |
|
|
// 检查当前连接设备与已保存的设备是否一致 |
|
|
if (currentDevice!.address != pairedMac) { |
|
|
if (currentDevice!.address != pairedMac) { |
|
|
// MAC地址不匹配 |
|
|
// MAC地址不匹配 |
|
|
@ -154,13 +156,13 @@ class PairingController extends GetxController { |
|
|
statusDescription.value = 'pleaseConnectPairedDevice'.tr; |
|
|
statusDescription.value = 'pleaseConnectPairedDevice'.tr; |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 判断设备名称是否符合要求 |
|
|
// 判断设备名称是否符合要求 |
|
|
if (!_isValidDeviceName(currentDevice!.name)) { |
|
|
if (!_isValidDeviceName(currentDevice!.name)) { |
|
|
statusDescription.value = 'pleaseConnectDeepSoundDevice'.tr; |
|
|
statusDescription.value = 'pleaseConnectDeepSoundDevice'.tr; |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 设备匹配且合规,标记为已配对 |
|
|
// 设备匹配且合规,标记为已配对 |
|
|
Logger.info('当前设备与已配对设备匹配'); |
|
|
Logger.info('当前设备与已配对设备匹配'); |
|
|
_companionAssociated.value = true; |
|
|
_companionAssociated.value = true; |
|
|
@ -223,7 +225,7 @@ class PairingController extends GetxController { |
|
|
_bluetoothState.value = state; |
|
|
_bluetoothState.value = state; |
|
|
_updateStatusDescription(); |
|
|
_updateStatusDescription(); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// 监听设备连接状态变化 |
|
|
// 监听设备连接状态变化 |
|
|
_deviceWorker = |
|
|
_deviceWorker = |
|
|
ever(_bluetoothService.currentDeviceRx, (BluetoothDevice? device) { |
|
|
ever(_bluetoothService.currentDeviceRx, (BluetoothDevice? device) { |
|
|
@ -238,12 +240,13 @@ class PairingController extends GetxController { |
|
|
/// 处理设备连接成功事件 |
|
|
/// 处理设备连接成功事件 |
|
|
Future<void> _handleDeviceConnected() async { |
|
|
Future<void> _handleDeviceConnected() async { |
|
|
if (!isDeviceConnected || _isNavigatingToHome) return; |
|
|
if (!isDeviceConnected || _isNavigatingToHome) return; |
|
|
|
|
|
|
|
|
// 检查配对状态,决定下一步操作 |
|
|
// 检查配对状态,决定下一步操作 |
|
|
await _checkDevicePairingStatus(); |
|
|
await _checkDevicePairingStatus(); |
|
|
|
|
|
|
|
|
// 如果还未配对且设备合规,启动配对流程 |
|
|
// 如果还未配对且设备合规,启动配对流程 |
|
|
if (!_companionAssociated.value && _isValidDeviceName(currentDevice!.name)) { |
|
|
if (!_companionAssociated.value && |
|
|
|
|
|
_isValidDeviceName(currentDevice!.name)) { |
|
|
_startCompanionAssociation(); |
|
|
_startCompanionAssociation(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -256,7 +259,7 @@ class PairingController extends GetxController { |
|
|
actionButtonText.value = '关联DeapSound耳机'; |
|
|
actionButtonText.value = '关联DeapSound耳机'; |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!hasPermissions) { |
|
|
if (!hasPermissions) { |
|
|
statusDescription.value = 'permissionDenied'.tr; |
|
|
statusDescription.value = 'permissionDenied'.tr; |
|
|
actionButtonText.value = '去系统设置'; |
|
|
actionButtonText.value = '去系统设置'; |
|
|
@ -271,9 +274,11 @@ class PairingController extends GetxController { |
|
|
|
|
|
|
|
|
if (isDeviceConnected) { |
|
|
if (isDeviceConnected) { |
|
|
if (Platform.isAndroid && !isCompanionAssociated && _isAssociating) { |
|
|
if (Platform.isAndroid && !isCompanionAssociated && _isAssociating) { |
|
|
statusDescription.value = '${'associatingDevice'.tr} ${currentDevice!.name}'; |
|
|
statusDescription.value = |
|
|
|
|
|
'${'associatingDevice'.tr} ${currentDevice!.name}'; |
|
|
} else { |
|
|
} else { |
|
|
statusDescription.value = '${'connectedDevice'.tr} ${currentDevice!.name}'; |
|
|
statusDescription.value = |
|
|
|
|
|
'${'connectedDevice'.tr} ${currentDevice!.name}'; |
|
|
} |
|
|
} |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
@ -286,21 +291,20 @@ class PairingController extends GetxController { |
|
|
statusDescription.value = 'notFound'.tr; |
|
|
statusDescription.value = 'notFound'.tr; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 启动Companion设备关联流程 |
|
|
/// 启动Companion设备关联流程 |
|
|
Future<void> _startCompanionAssociation() async { |
|
|
Future<void> _startCompanionAssociation() async { |
|
|
// 如果不是Android平台,已跳转,已完成关联或正在进行关联,直接返回 |
|
|
// 如果不是Android平台,已跳转,已完成关联或正在进行关联,直接返回 |
|
|
if (!Platform.isAndroid || |
|
|
if (!Platform.isAndroid || |
|
|
_isNavigatingToHome || |
|
|
_isNavigatingToHome || |
|
|
_companionAssociated.value || |
|
|
_companionAssociated.value || |
|
|
_isAssociating || |
|
|
_isAssociating || |
|
|
!isDeviceConnected) { |
|
|
!isDeviceConnected) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 重置拒绝状态 |
|
|
// 重置拒绝状态 |
|
|
_userRejectedAssociation.value = false; |
|
|
_userRejectedAssociation.value = false; |
|
|
|
|
|
|
|
|
// 设置关联状态标志 |
|
|
// 设置关联状态标志 |
|
|
_isAssociating = true; |
|
|
_isAssociating = true; |
|
|
_updateStatusDescription(); |
|
|
_updateStatusDescription(); |
|
|
@ -314,14 +318,14 @@ class PairingController extends GetxController { |
|
|
_autoNavigateToHome(); |
|
|
_autoNavigateToHome(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 检查设备名称是否符合要求 |
|
|
// 检查设备名称是否符合要求 |
|
|
if (!_isValidDeviceName(currentDevice!.name)) { |
|
|
if (!_isValidDeviceName(currentDevice!.name)) { |
|
|
statusDescription.value = 'pleaseConnectDeepSoundDevice'.tr; |
|
|
statusDescription.value = 'pleaseConnectDeepSoundDevice'.tr; |
|
|
_isAssociating = false; |
|
|
_isAssociating = false; |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Logger.info('开始Companion设备关联流程, 设备MAC: ${currentDevice!.address}'); |
|
|
Logger.info('开始Companion设备关联流程, 设备MAC: ${currentDevice!.address}'); |
|
|
|
|
|
|
|
|
var success = await _bleService.startDeviceAssociation( |
|
|
var success = await _bleService.startDeviceAssociation( |
|
|
@ -329,7 +333,7 @@ class PairingController extends GetxController { |
|
|
macAddress: currentDevice!.address, |
|
|
macAddress: currentDevice!.address, |
|
|
); |
|
|
); |
|
|
_isAssociating = false; |
|
|
_isAssociating = false; |
|
|
|
|
|
|
|
|
if (success) { |
|
|
if (success) { |
|
|
Logger.info('Companion设备关联成功'); |
|
|
Logger.info('Companion设备关联成功'); |
|
|
_companionAssociated.value = true; |
|
|
_companionAssociated.value = true; |
|
|
@ -338,8 +342,7 @@ class PairingController extends GetxController { |
|
|
// 注册后台扫描,确保应用被杀死后仍能被唤醒 |
|
|
// 注册后台扫描,确保应用被杀死后仍能被唤醒 |
|
|
await _bleService.registerBackgroundScan( |
|
|
await _bleService.registerBackgroundScan( |
|
|
macAddress: currentDevice!.address); |
|
|
macAddress: currentDevice!.address); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 关联成功后跳转 |
|
|
// 关联成功后跳转 |
|
|
_autoNavigateToHome(); |
|
|
_autoNavigateToHome(); |
|
|
} else { |
|
|
} else { |
|
|
@ -359,9 +362,9 @@ class PairingController extends GetxController { |
|
|
|
|
|
|
|
|
/// 自动跳转到主页 |
|
|
/// 自动跳转到主页 |
|
|
Future<void> _autoNavigateToHome() async { |
|
|
Future<void> _autoNavigateToHome() async { |
|
|
if (_isNavigatingToHome || !isDeviceConnected) return; |
|
|
if (needdeivice && (_isNavigatingToHome || !isDeviceConnected)) return; |
|
|
|
|
|
|
|
|
// 启动BLE后台服务 |
|
|
// 启动BLE后台服务 |
|
|
await _startAgentService(); |
|
|
await _startAgentService(); |
|
|
|
|
|
|
|
|
_isNavigatingToHome = true; |
|
|
_isNavigatingToHome = true; |
|
|
@ -375,7 +378,7 @@ class PairingController extends GetxController { |
|
|
/// 启动BLE后台服务 |
|
|
/// 启动BLE后台服务 |
|
|
Future<void> _startAgentService() async { |
|
|
Future<void> _startAgentService() async { |
|
|
try { |
|
|
try { |
|
|
if (!isDeviceConnected) return; |
|
|
if (needdeivice && !isDeviceConnected) return; |
|
|
|
|
|
|
|
|
final result = await BleManager.to.startAgentService(); |
|
|
final result = await BleManager.to.startAgentService(); |
|
|
Logger.info(result ? 'BLE后台服务启动成功' : 'BLE后台服务启动失败'); |
|
|
Logger.info(result ? 'BLE后台服务启动成功' : 'BLE后台服务启动失败'); |
|
|
@ -426,7 +429,7 @@ class PairingController extends GetxController { |
|
|
Future<void> openAppSettings() async { |
|
|
Future<void> openAppSettings() async { |
|
|
await AppSettings.openAppSettings(); |
|
|
await AppSettings.openAppSettings(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// 重新开始关联 |
|
|
/// 重新开始关联 |
|
|
void restartAssociation() { |
|
|
void restartAssociation() { |
|
|
if (isDeviceConnected && !isCompanionAssociated) { |
|
|
if (isDeviceConnected && !isCompanionAssociated) { |
|
|
@ -435,8 +438,8 @@ class PairingController extends GetxController { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// 跳过连接 |
|
|
/// 跳过连接 |
|
|
void skipConnecting() async{ |
|
|
void skipConnecting() async { |
|
|
// 启动BLE后台服务 |
|
|
// 启动BLE后台服务 |
|
|
await _startAgentService(); |
|
|
await _startAgentService(); |
|
|
_isNavigatingToHome = true; |
|
|
_isNavigatingToHome = true; |
|
|
Get.offAllNamed(Routes.home); |
|
|
Get.offAllNamed(Routes.home); |
|
|
|