|
|
@ -10,6 +10,7 @@ import 'package:classic_bluetooth/classic_bluetooth.dart' show BluetoothDevice; |
|
|
import '../../../data/services/ble_manager.dart'; |
|
|
import '../../../data/services/ble_manager.dart'; |
|
|
import 'package:agent_service/agent_service.dart'; |
|
|
import 'package:agent_service/agent_service.dart'; |
|
|
import 'package:ble_service/ble_service.dart'; |
|
|
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(); |
|
|
@ -164,10 +165,10 @@ class PairingController extends GetxController { |
|
|
// 辅助方法获取Android SDK版本 |
|
|
// 辅助方法获取Android SDK版本 |
|
|
Future<int> _getAndroidSdkVersion() async { |
|
|
Future<int> _getAndroidSdkVersion() async { |
|
|
if (Platform.isAndroid) { |
|
|
if (Platform.isAndroid) { |
|
|
// 这里需要原生代码支持来获取确切的SDK版本, |
|
|
// 这里需要原生代码支持来获取确切的SDK版本, |
|
|
// 或者使用 device_info_plus 插件 |
|
|
// 或者使用 device_info_plus 插件 |
|
|
// 暂时返回一个较高值以触发新权限请求 |
|
|
// 暂时返回一个较高值以触发新权限请求 |
|
|
return 31; // 假设运行在需要新权限的设备上 |
|
|
return 31; // 假设运行在需要新权限的设备上 |
|
|
} |
|
|
} |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
@ -175,7 +176,8 @@ class PairingController extends GetxController { |
|
|
/// 设置服务状态监听 |
|
|
/// 设置服务状态监听 |
|
|
void _setupServiceStateListeners() { |
|
|
void _setupServiceStateListeners() { |
|
|
// 监听蓝牙服务的蓝牙状态变化 |
|
|
// 监听蓝牙服务的蓝牙状态变化 |
|
|
_bluetoothStateWorker = ever(_bluetoothService.bluetoothState.obs, (String state) { |
|
|
_bluetoothStateWorker = |
|
|
|
|
|
ever(_bluetoothService.bluetoothState.obs, (String state) { |
|
|
_bluetoothState.value = state; |
|
|
_bluetoothState.value = state; |
|
|
_updateStatusDescription(); |
|
|
_updateStatusDescription(); |
|
|
|
|
|
|
|
|
@ -184,12 +186,13 @@ class PairingController extends GetxController { |
|
|
Logger.info('蓝牙已关闭'); |
|
|
Logger.info('蓝牙已关闭'); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
Logger.info('liwei----------------- _setupServiceStateListeners 1'); |
|
|
// 监听当前设备变化 - 使用公开的currentDeviceRx字段 |
|
|
// 监听当前设备变化 - 使用公开的currentDeviceRx字段 |
|
|
_deviceWorker = ever(_bluetoothService.currentDeviceRx, (BluetoothDevice? device) { |
|
|
_deviceWorker = |
|
|
|
|
|
ever(_bluetoothService.currentDeviceRx, (BluetoothDevice? device) { |
|
|
if (device != null && !_isNavigatingToHome) { |
|
|
if (device != null && !_isNavigatingToHome) { |
|
|
Logger.info('配对页面发现设备已连接: ${device.name}'); |
|
|
Logger.info('配对页面发现设备已连接: ${device.name}'); |
|
|
|
|
|
Logger.info('liwei----------------- _setupServiceStateListeners 2'); |
|
|
// 当经典蓝牙设备连接成功,尝试进行Companion关联 |
|
|
// 当经典蓝牙设备连接成功,尝试进行Companion关联 |
|
|
_handleDeviceConnected(); |
|
|
_handleDeviceConnected(); |
|
|
} |
|
|
} |
|
|
@ -199,14 +202,15 @@ class PairingController extends GetxController { |
|
|
|
|
|
|
|
|
/// 处理设备连接成功事件 |
|
|
/// 处理设备连接成功事件 |
|
|
Future<void> _handleDeviceConnected() async { |
|
|
Future<void> _handleDeviceConnected() async { |
|
|
|
|
|
Logger.info('liwei----------------- _handleDeviceConnected 1'); |
|
|
if (!isDeviceConnected || _isNavigatingToHome) return; |
|
|
if (!isDeviceConnected || _isNavigatingToHome) return; |
|
|
|
|
|
Logger.info('liwei----------------- _handleDeviceConnected 2'); |
|
|
// 如果已经完成关联,直接跳转 |
|
|
// 如果已经完成关联,直接跳转 |
|
|
if (_companionAssociated.value) { |
|
|
if (_companionAssociated.value) { |
|
|
_autoNavigateToHome(); |
|
|
_autoNavigateToHome(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
Logger.info('liwei----------------- _handleDeviceConnected 3'); |
|
|
// 启动关联流程 |
|
|
// 启动关联流程 |
|
|
_startCompanionAssociation(); |
|
|
_startCompanionAssociation(); |
|
|
} |
|
|
} |
|
|
@ -225,11 +229,14 @@ 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 if (isCompanionAssociated) { |
|
|
} else if (isCompanionAssociated) { |
|
|
statusDescription.value = '${'connectedDevice'.tr} ${currentDevice!.name}'; |
|
|
statusDescription.value = |
|
|
|
|
|
'${'connectedDevice'.tr} ${currentDevice!.name}'; |
|
|
} else { |
|
|
} else { |
|
|
statusDescription.value = '${'connectedDevice'.tr} ${currentDevice!.name}'; |
|
|
statusDescription.value = |
|
|
|
|
|
'${'connectedDevice'.tr} ${currentDevice!.name}'; |
|
|
} |
|
|
} |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
@ -276,11 +283,15 @@ class PairingController extends GetxController { |
|
|
|
|
|
|
|
|
/// 启动Companion设备关联流程 |
|
|
/// 启动Companion设备关联流程 |
|
|
Future<void> _startCompanionAssociation() async { |
|
|
Future<void> _startCompanionAssociation() async { |
|
|
|
|
|
Logger.info('liwei----------------- _startCompanionAssociation 1'); |
|
|
// 如果不是Android平台,已经跳转到首页,已经完成关联或正在进行关联,直接返回 |
|
|
// 如果不是Android平台,已经跳转到首页,已经完成关联或正在进行关联,直接返回 |
|
|
if (!Platform.isAndroid || _isNavigatingToHome || _companionAssociated.value || _isAssociating) { |
|
|
if (!Platform.isAndroid || |
|
|
|
|
|
_isNavigatingToHome || |
|
|
|
|
|
_companionAssociated.value || |
|
|
|
|
|
_isAssociating) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
Logger.info('liwei----------------- _startCompanionAssociation 2'); |
|
|
// 设置关联状态标志 |
|
|
// 设置关联状态标志 |
|
|
_isAssociating = true; |
|
|
_isAssociating = true; |
|
|
_updateStatusDescription(); |
|
|
_updateStatusDescription(); |
|
|
@ -294,7 +305,7 @@ class PairingController extends GetxController { |
|
|
_autoNavigateToHome(); |
|
|
_autoNavigateToHome(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
Logger.info('liwei----------------- _startCompanionAssociation 3'); |
|
|
// 检查是否已有配对的MAC地址 |
|
|
// 检查是否已有配对的MAC地址 |
|
|
final pairedMac = await _bleService.getPairedMacAddress(); |
|
|
final pairedMac = await _bleService.getPairedMacAddress(); |
|
|
if (pairedMac != null) { |
|
|
if (pairedMac != null) { |
|
|
@ -303,31 +314,32 @@ class PairingController extends GetxController { |
|
|
Logger.info('已关联的Companion设备与当前连接的设备一致'); |
|
|
Logger.info('已关联的Companion设备与当前连接的设备一致'); |
|
|
_companionAssociated.value = true; |
|
|
_companionAssociated.value = true; |
|
|
await _bleService.registerBackgroundScan(); |
|
|
await _bleService.registerBackgroundScan(); |
|
|
_autoNavigateToHome(); |
|
|
// _autoNavigateToHome(); |
|
|
return; |
|
|
// return; |
|
|
} else { |
|
|
} else { |
|
|
// 不一致,清除旧关联 |
|
|
// 不一致,清除旧关联 |
|
|
Logger.info('已关联的Companion设备与当前连接的设备不一致,清除旧关联'); |
|
|
Logger.info('已关联的Companion设备与当前连接的设备不一致,清除旧关联'); |
|
|
await _bleService.clearAssociations(); |
|
|
await _bleService.clearAssociations(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
Logger.info('liwei----------------- _startCompanionAssociation 4'); |
|
|
// 开始关联流程 |
|
|
// 开始关联流程 |
|
|
if (currentDevice != null) { |
|
|
if (currentDevice != null) { |
|
|
Logger.info('开始Companion设备关联流程, 设备MAC: ${currentDevice!.address}'); |
|
|
Logger.info('开始Companion设备关联流程, 设备MAC: ${currentDevice!.address}'); |
|
|
|
|
|
|
|
|
final success = await _bleService.startDeviceAssociation( |
|
|
var success = await _bleService.startDeviceAssociation( |
|
|
deviceNamePattern: currentDevice!.name, |
|
|
deviceNamePattern: currentDevice!.name, |
|
|
macAddress: currentDevice!.address, |
|
|
macAddress: currentDevice!.address, |
|
|
); |
|
|
); |
|
|
|
|
|
success = true; |
|
|
if (success) { |
|
|
if (success) { |
|
|
Logger.info('Companion设备关联成功'); |
|
|
Logger.info('Companion设备关联成功'); |
|
|
_companionAssociated.value = true; |
|
|
_companionAssociated.value = true; |
|
|
|
|
|
|
|
|
// 注册后台扫描,确保应用被杀死后仍能被唤醒 |
|
|
// 注册后台扫描,确保应用被杀死后仍能被唤醒 |
|
|
await _bleService.registerBackgroundScan(macAddress: currentDevice!.address); |
|
|
await _bleService.registerBackgroundScan( |
|
|
|
|
|
macAddress: currentDevice!.address); |
|
|
|
|
|
Logger.info('liwei----------------- _startCompanionAssociation 5'); |
|
|
// 启动BLE后台服务(可选) |
|
|
// 启动BLE后台服务(可选) |
|
|
await _startAgentService(); |
|
|
await _startAgentService(); |
|
|
|
|
|
|
|
|
@ -380,7 +392,6 @@ class PairingController extends GetxController { |
|
|
try { |
|
|
try { |
|
|
if (!isDeviceConnected) return; |
|
|
if (!isDeviceConnected) return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 启动BLE服务,无需传递配置参数,将从.env读取 |
|
|
// 启动BLE服务,无需传递配置参数,将从.env读取 |
|
|
final result = await BleManager.to.startAgentService(); |
|
|
final result = await BleManager.to.startAgentService(); |
|
|
|
|
|
|
|
|
|