|
|
|
@ -95,6 +95,7 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
|
|
|
|
|
// ========== 当前设备信息 ========== |
|
|
|
BluetoothDevice? _currentClassicDevice; |
|
|
|
int? _verifiedDevicePid; |
|
|
|
String? _verifiedDeviceName; |
|
|
|
String? _verifiedDeviceAddress; |
|
|
|
TextEditingController codeInputController = TextEditingController(); |
|
|
|
@ -861,6 +862,7 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
|
String macFromHex = extractMacFromHex(hexData, device.address); |
|
|
|
if (macFromHex.isNotEmpty) { |
|
|
|
String code = ""; |
|
|
|
_verifiedDevicePid = extractAndReverseBytesToInt(hexData, 6, 7); |
|
|
|
// 找到匹配的设备,提取授权码 |
|
|
|
if (Platform.isIOS) { |
|
|
|
// iOS设备,提取13-22字节的授权码 |
|
|
|
@ -1010,7 +1012,14 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
|
|
|
|
|
//确认绑定设备 |
|
|
|
void confirmBindDevice() async { |
|
|
|
if (currentDevice == null) { |
|
|
|
Logger.e(_tag, '当前设备为空!'); |
|
|
|
_transition(PairingState.verifying); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
final response = await Api.binddevice({ |
|
|
|
"pid": _verifiedDevicePid, |
|
|
|
"code": deviceCode.value, |
|
|
|
"devicename": currentDevice?.name, |
|
|
|
"devicemac": currentDevice?.address, // 使用 address 替代 deviceId |
|
|
|
@ -1050,6 +1059,31 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
|
.toUpperCase(); |
|
|
|
} |
|
|
|
|
|
|
|
int extractAndReverseBytesToInt(String hex, int start, int end) { |
|
|
|
List<int> bytes = []; |
|
|
|
// 将十六进制字符串解析为字节列表 |
|
|
|
for (int i = 0; i < hex.length; i += 2) { |
|
|
|
bytes.add(int.parse(hex.substring(i, i + 2), radix: 16)); |
|
|
|
} |
|
|
|
|
|
|
|
// 截取指定范围的字节(包含end索引) |
|
|
|
List<int> slice = bytes.sublist(start, end + 1); |
|
|
|
// 反转字节顺序 |
|
|
|
List<int> reversed = slice.toList(); |
|
|
|
|
|
|
|
String pid = reversed |
|
|
|
.map((b) => b.toRadixString(16).padLeft(2, '0')) |
|
|
|
.join() |
|
|
|
.toUpperCase(); |
|
|
|
Logger.i(_tag, 'liwei-------------- 解析道Pid $pid'); |
|
|
|
// 将反转后的字节列表转换为整数 |
|
|
|
int result = 0; |
|
|
|
for (int b in reversed) { |
|
|
|
result = (result << 8) | (b & 0xFF); // 按字节拼接成整数 |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/// 用户点击授权码输入框 |
|
|
|
void onAutoCodeInputTap() async { |
|
|
|
// 调用BLE管理器的自动绑定方法 |
|
|
|
|