Browse Source

Merge commit '9941eacf19d1b7e6b6ed9c3d420db2de4c03897e' into 重写ble

weicu
lxm 1 year ago
parent
commit
81057a79ad
  1. 2
      lib/core/translations/language/zh_cn.dart
  2. 36
      lib/modules/pairing/controllers/pairing_controller.dart
  3. 3
      lib/modules/pairing/views/pairing_view.dart

2
lib/core/translations/language/zh_cn.dart

@ -960,7 +960,7 @@ const Map<String, String> zhCN = {
"pairingSuccessRedirecting": "配对成功!即将跳转到主页...",
// 配对错误状态文本
"ensureConnectedDeepSoundDevice": "请确保连接的是Voitrans设备",
"ensureConnectedDeepSoundDevice": "当前使用设备:%s,请确保连接的是Voitrans支持的设备",
"deviceAlreadyBoundToOtherAccount": "该设备已被其他账号绑定",
"deviceAssociationFailed": "设备关联失败,请重试",
"scanTimeoutEnsureDeviceNearby": "扫描超时,请确保设备在附近",

36
lib/modules/pairing/controllers/pairing_controller.dart

@ -95,6 +95,7 @@ class PairingController extends GetxController with WidgetsBindingObserver {
// ========== 当前设备信息 ==========
BluetoothDevice? _currentClassicDevice;
int? _verifiedDevicePid;
String? _verifiedDeviceName;
String? _verifiedDeviceAddress;
TextEditingController codeInputController = TextEditingController();
@ -837,7 +838,7 @@ class PairingController extends GetxController with WidgetsBindingObserver {
}
print(
'获取到 deviceName: $deviceName address: ${device.address} uuids:$uuids');
if (names.contains(deviceName)) {
if (names.any((name) => name.contains(deviceName))) {
if (currentBleState == 2) {
//已连接直接返回
return 9;
@ -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管理器的自动绑定方法

3
lib/modules/pairing/views/pairing_view.dart

@ -492,7 +492,8 @@ class _PairingViewState extends State<PairingView>
return 'connectDeepSoundViaSystemBluetooth'
.tr; // 请先通过系统蓝牙连接Voitrans设备
case ErrorType.deviceNotMatch:
return 'ensureConnectedDeepSoundDevice'.tr; // 请确保连接的是Voitrans设备
return 'ensureConnectedDeepSoundDevice'.tr.trArgs(
[controller.currentDevice?.name ?? '']); // 请确保连接的是Voitrans设备
case ErrorType.deviceBound:
return 'deviceAlreadyBoundToOtherAccount'.tr; // 该设备已被其他账号绑定
case ErrorType.associationFailed:

Loading…
Cancel
Save