|
|
@ -83,7 +83,8 @@ class PairingController extends GetxController { |
|
|
_setupAllListeners(); |
|
|
_setupAllListeners(); |
|
|
|
|
|
|
|
|
// 开始初始状态检查 |
|
|
// 开始初始状态检查 |
|
|
Future.delayed(Duration.zero, () { |
|
|
// 延迟稍长时间,确保BluetoothManager有足够时间获取已连接设备 |
|
|
|
|
|
Future.delayed(const Duration(milliseconds: 300), () { |
|
|
_startInitialCheck(); |
|
|
_startInitialCheck(); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
@ -263,7 +264,10 @@ class PairingController extends GetxController { |
|
|
case PairingState.waitingBle: |
|
|
case PairingState.waitingBle: |
|
|
statusText.value = '正在建立安全连接...'; |
|
|
statusText.value = '正在建立安全连接...'; |
|
|
isLoading.value = true; |
|
|
isLoading.value = true; |
|
|
_startBleConnection(); |
|
|
// 延迟一下再调用,给BLE管理器时间更新状态 |
|
|
|
|
|
Future.delayed(const Duration(milliseconds: 200), () { |
|
|
|
|
|
_startBleConnection(); |
|
|
|
|
|
}); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case PairingState.completed: |
|
|
case PairingState.completed: |
|
|
@ -338,14 +342,17 @@ class PairingController extends GetxController { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 3. 检查经典蓝牙连接 |
|
|
// 3. 先刷新设备连接状态,确保获取最新的连接信息 |
|
|
|
|
|
await _bluetoothManager.refreshDeviceConnection(); |
|
|
|
|
|
|
|
|
|
|
|
// 4. 检查经典蓝牙连接 |
|
|
final classicDevice = _bluetoothManager.currentDevice; |
|
|
final classicDevice = _bluetoothManager.currentDevice; |
|
|
if (classicDevice == null) { |
|
|
if (classicDevice == null) { |
|
|
_transition(PairingState.waitingClassic); |
|
|
_transition(PairingState.waitingClassic); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 4. 如果有设备,进行验证 |
|
|
// 5. 如果有设备,进行验证 |
|
|
_currentClassicDevice = classicDevice; |
|
|
_currentClassicDevice = classicDevice; |
|
|
_transition(PairingState.verifying); |
|
|
_transition(PairingState.verifying); |
|
|
await _verifyDevice(classicDevice); |
|
|
await _verifyDevice(classicDevice); |
|
|
@ -387,10 +394,15 @@ class PairingController extends GetxController { |
|
|
Logger.i(_tag, '设备验证通过'); |
|
|
Logger.i(_tag, '设备验证通过'); |
|
|
_verifiedDeviceAddress = device.address; |
|
|
_verifiedDeviceAddress = device.address; |
|
|
|
|
|
|
|
|
// 检查BLE状态 |
|
|
// 检查BLE状态 - 直接从原生层获取 |
|
|
if (_bleManager.isConnected) { |
|
|
final currentBleState = await _bleManager.getConnectionState(); |
|
|
|
|
|
Logger.i(_tag, '验证时BLE状态: $currentBleState'); |
|
|
|
|
|
|
|
|
|
|
|
if (currentBleState == 2) { // BleConnectionState.connected |
|
|
|
|
|
Logger.i(_tag, 'BLE已连接,直接完成配对'); |
|
|
_transition(PairingState.completed); |
|
|
_transition(PairingState.completed); |
|
|
} else { |
|
|
} else { |
|
|
|
|
|
Logger.i(_tag, 'BLE未连接,等待BLE连接'); |
|
|
_transition(PairingState.waitingBle); |
|
|
_transition(PairingState.waitingBle); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
@ -413,8 +425,12 @@ class PairingController extends GetxController { |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// 检查是否已连接 |
|
|
// 检查是否已连接 - 直接从原生层获取最新状态 |
|
|
if (_bleManager.isConnected) { |
|
|
final currentBleState = await _bleManager.getConnectionState(); |
|
|
|
|
|
Logger.i(_tag, 'BLE当前连接状态: $currentBleState'); |
|
|
|
|
|
|
|
|
|
|
|
if (currentBleState == 2) { // BleConnectionState.connected |
|
|
|
|
|
Logger.i(_tag, 'BLE已经连接,直接完成配对'); |
|
|
_bleTimeoutTimer?.cancel(); |
|
|
_bleTimeoutTimer?.cancel(); |
|
|
_transition(PairingState.completed); |
|
|
_transition(PairingState.completed); |
|
|
return; |
|
|
return; |
|
|
|