|
|
@ -810,6 +810,7 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
orElse: () => UserDevice( |
|
|
orElse: () => UserDevice( |
|
|
id: -1, |
|
|
id: -1, |
|
|
uid: '', |
|
|
uid: '', |
|
|
|
|
|
productid: -1, |
|
|
devicetype: -1, |
|
|
devicetype: -1, |
|
|
devicename: '', |
|
|
devicename: '', |
|
|
devicemac: '', |
|
|
devicemac: '', |
|
|
@ -840,15 +841,21 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
orElse: () => UserDevice( |
|
|
orElse: () => UserDevice( |
|
|
id: -1, |
|
|
id: -1, |
|
|
uid: '', |
|
|
uid: '', |
|
|
|
|
|
productid: -1, |
|
|
devicetype: -1, |
|
|
devicetype: -1, |
|
|
devicename: '', |
|
|
devicename: '', |
|
|
devicemac: '', |
|
|
devicemac: '', |
|
|
license: ''), |
|
|
license: ''), |
|
|
); |
|
|
); |
|
|
if (existingDevice.id != -1) { |
|
|
if (existingDevice.id != -1) { |
|
|
|
|
|
_verifiedDevicePid = existingDevice.productid; |
|
|
_verifiedDeviceName = device.name; |
|
|
_verifiedDeviceName = device.name; |
|
|
_verifiedDeviceAddress = device.address; |
|
|
_verifiedDeviceAddress = device.address; |
|
|
// 设备时ble 蓝牙 且已绑定 |
|
|
// 已绑定设备直接用 license 作为 deviceCode(即绑定时扫到的 SN) |
|
|
|
|
|
if (existingDevice.license.isNotEmpty) { |
|
|
|
|
|
deviceCode.value = existingDevice.license; |
|
|
|
|
|
Logger.i(_tag, '[YIDONG] 已绑定设备,从license提取 deviceCode=${existingDevice.license}'); |
|
|
|
|
|
} |
|
|
return 10; |
|
|
return 10; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -885,14 +892,15 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
code = extractAndReverseBytes(hexData, 13, 22); |
|
|
code = extractAndReverseBytes(hexData, 13, 22); |
|
|
_verifiedDevicePid = |
|
|
_verifiedDevicePid = |
|
|
extractAndReverseBytesToInt(hexData, 4, 5); |
|
|
extractAndReverseBytesToInt(hexData, 4, 5); |
|
|
print('找到匹配设备,获取到 code: $code'); |
|
|
final pidHexIos = _verifiedDevicePid?.toRadixString(16).toUpperCase() ?? ''; |
|
|
|
|
|
Logger.i(_tag, '[YIDONG] iOS BLE扫描 MAC匹配 device=${device.name} pidHex=$pidHexIos code=$code'); |
|
|
if (code != "") { |
|
|
if (code != "") { |
|
|
_bleManager.stopScan(); |
|
|
_bleManager.stopScan(); |
|
|
deviceCode.value = code; |
|
|
deviceCode.value = code; |
|
|
String? uuids = deviceInfo["id"]; |
|
|
String? uuids = deviceInfo["id"]; |
|
|
_verifiedDeviceName = device.name; |
|
|
_verifiedDeviceName = device.name; |
|
|
_verifiedDeviceAddress = uuids; |
|
|
_verifiedDeviceAddress = uuids; |
|
|
print('找到匹配设备,获取到 uuids: $uuids'); |
|
|
Logger.i(_tag, '[YIDONG] iOS deviceCode提取成功 code=$code uuid=$uuids'); |
|
|
//检查是否已经绑定 |
|
|
//检查是否已经绑定 |
|
|
final existingDevice = User.instance.devices.firstWhere( |
|
|
final existingDevice = User.instance.devices.firstWhere( |
|
|
(d) => |
|
|
(d) => |
|
|
@ -902,6 +910,7 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
orElse: () => UserDevice( |
|
|
orElse: () => UserDevice( |
|
|
id: -1, |
|
|
id: -1, |
|
|
uid: '', |
|
|
uid: '', |
|
|
|
|
|
productid: -1, |
|
|
devicetype: -1, |
|
|
devicetype: -1, |
|
|
devicename: '', |
|
|
devicename: '', |
|
|
devicemac: '', |
|
|
devicemac: '', |
|
|
@ -921,12 +930,14 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
code = extractAndReverseBytes(hexData, 15, 24); |
|
|
code = extractAndReverseBytes(hexData, 15, 24); |
|
|
_verifiedDevicePid = |
|
|
_verifiedDevicePid = |
|
|
extractAndReverseBytesToInt(hexData, 6, 7); |
|
|
extractAndReverseBytesToInt(hexData, 6, 7); |
|
|
print('找到匹配设备,获取到 code: $code'); |
|
|
final pidHexAndroid = _verifiedDevicePid?.toRadixString(16).toUpperCase() ?? ''; |
|
|
|
|
|
Logger.i(_tag, '[YIDONG] Android BLE扫描 MAC匹配 device=${device.name} mac=${device.address} pidHex=$pidHexAndroid code=$code'); |
|
|
if (code != "") { |
|
|
if (code != "") { |
|
|
_bleManager.stopScan(); |
|
|
_bleManager.stopScan(); |
|
|
deviceCode.value = code; |
|
|
deviceCode.value = code; |
|
|
_verifiedDeviceName = device.name; |
|
|
_verifiedDeviceName = device.name; |
|
|
_verifiedDeviceAddress = device.address; |
|
|
_verifiedDeviceAddress = device.address; |
|
|
|
|
|
Logger.i(_tag, '[YIDONG] Android deviceCode提取成功 code=$code mac=${device.address}'); |
|
|
//检查是否已经绑定 |
|
|
//检查是否已经绑定 |
|
|
final existingDevice = User.instance.devices.firstWhere( |
|
|
final existingDevice = User.instance.devices.firstWhere( |
|
|
(d) => |
|
|
(d) => |
|
|
@ -936,6 +947,7 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
orElse: () => UserDevice( |
|
|
orElse: () => UserDevice( |
|
|
id: -1, |
|
|
id: -1, |
|
|
uid: '', |
|
|
uid: '', |
|
|
|
|
|
productid: -1, |
|
|
devicetype: -1, |
|
|
devicetype: -1, |
|
|
devicename: '', |
|
|
devicename: '', |
|
|
devicemac: '', |
|
|
devicemac: '', |
|
|
@ -1178,26 +1190,92 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 检测是否为移动精灵(YIDONG)设备,并写入 GetStorage |
|
|
|
|
|
void _detectAndSaveYidongDevice() { |
|
|
|
|
|
final yidongPid = AppConfig.env('YIDONG_PID') ?? ''; |
|
|
|
|
|
if (yidongPid.isEmpty) { |
|
|
|
|
|
Logger.i(_tag, '[YIDONG] 未配置 YIDONG_PID,跳过检测'); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 优先使用本次扫描解析的 pid,否则从已绑定设备列表中查找 |
|
|
|
|
|
int? pid = _verifiedDevicePid; |
|
|
|
|
|
String pidSource = 'BLE扫描'; |
|
|
|
|
|
if (pid == null) { |
|
|
|
|
|
final mac = _verifiedDeviceAddress ?? _currentClassicDevice?.address ?? ''; |
|
|
|
|
|
Logger.i(_tag, '[YIDONG] 本次未扫描到pid,从已绑定设备查找 mac=$mac'); |
|
|
|
|
|
if (mac.isNotEmpty) { |
|
|
|
|
|
final matched = User.instance.devices.firstWhere( |
|
|
|
|
|
(d) => |
|
|
|
|
|
d.devicemac.toUpperCase().replaceAll(':', '') == |
|
|
|
|
|
mac.toUpperCase().replaceAll(':', ''), |
|
|
|
|
|
orElse: () => UserDevice( |
|
|
|
|
|
id: -1, uid: '', productid: -1, |
|
|
|
|
|
devicetype: -1, devicename: '', devicemac: '', license: ''), |
|
|
|
|
|
); |
|
|
|
|
|
if (matched.id != -1) { |
|
|
|
|
|
pid = matched.productid; |
|
|
|
|
|
pidSource = '已绑定设备'; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (pid == null) { |
|
|
|
|
|
Logger.i(_tag, '[YIDONG] 无法获取设备pid,跳过检测'); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 将 int pid 转为十六进制字符串(不补零,大写)与 YIDONG_PID 比对 |
|
|
|
|
|
final pidHex = pid.toRadixString(16).toUpperCase(); |
|
|
|
|
|
final isYidong = pidHex == yidongPid.toUpperCase(); |
|
|
|
|
|
|
|
|
|
|
|
Logger.i(_tag, '[YIDONG] 设备检测: pidHex=$pidHex yidongPid=${yidongPid.toUpperCase()} source=$pidSource matched=$isYidong'); |
|
|
|
|
|
|
|
|
|
|
|
if (isYidong) { |
|
|
|
|
|
_storage.write('device_agent_type', 'yidong'); |
|
|
|
|
|
// deviceCode 来源: |
|
|
|
|
|
// 新设备 → BLE 扫描从广播包提取 |
|
|
|
|
|
// 已绑定设备 → authbindingDevice 从 existingDevice.license 赋值 |
|
|
|
|
|
if (deviceCode.value.isNotEmpty) { |
|
|
|
|
|
final prevCode = _storage.read<String>('device_sts_code') ?? ''; |
|
|
|
|
|
// SN 变了说明换了设备,清除旧凭证,强制重新向移动平台认证 |
|
|
|
|
|
if (prevCode != deviceCode.value) { |
|
|
|
|
|
_storage.remove('sts_device_id'); |
|
|
|
|
|
_storage.remove('sts_device_secret'); |
|
|
|
|
|
Logger.i(_tag, '[YIDONG] SN 变更($prevCode → ${deviceCode.value}),已清除旧凭证,强制重新认证'); |
|
|
|
|
|
} |
|
|
|
|
|
_storage.write('device_sts_code', deviceCode.value); |
|
|
|
|
|
Logger.i(_tag, '[YIDONG] 识别成功,写入 device_sts_code=${deviceCode.value}'); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 既无扫描结果也无 license,清除所有凭证,认证失败会提示用户 |
|
|
|
|
|
_storage.remove('device_sts_code'); |
|
|
|
|
|
_storage.remove('sts_device_id'); |
|
|
|
|
|
_storage.remove('sts_device_secret'); |
|
|
|
|
|
Logger.i(_tag, '[YIDONG] deviceCode 为空,已清除所有凭证'); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
_storage.remove('device_agent_type'); |
|
|
|
|
|
_storage.remove('device_sts_code'); |
|
|
|
|
|
_storage.remove('sts_device_id'); |
|
|
|
|
|
_storage.remove('sts_device_secret'); |
|
|
|
|
|
Logger.i(_tag, '[YIDONG] 非移动精灵设备,已清除相关存储'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/// 导航到主页 |
|
|
/// 导航到主页 |
|
|
void _navigateToHome() { |
|
|
void _navigateToHome() { |
|
|
// Future.delayed(const Duration(milliseconds: 500), () async { |
|
|
// 检测是否为移动精灵(YIDONG)设备 |
|
|
|
|
|
_detectAndSaveYidongDevice(); |
|
|
// 启动AgentService服务 |
|
|
// 启动AgentService服务 |
|
|
try { |
|
|
try { |
|
|
Logger.i(_tag, '启动AgentService服务'); |
|
|
Logger.i(_tag, '启动AgentService服务'); |
|
|
_bleManager.startAgentService(); |
|
|
_bleManager.startAgentService(); |
|
|
// _bleManager.registerBackgroundScan(macAddress: _verifiedDeviceAddress); |
|
|
|
|
|
} catch (e) { |
|
|
} catch (e) { |
|
|
Logger.e(_tag, '启动AgentService失败: $e'); |
|
|
Logger.e(_tag, '启动AgentService失败: $e'); |
|
|
} |
|
|
} |
|
|
//print('lxm-上一个路由: ${Get.previousRoute.toString()}'); |
|
|
// 已在 Home 页时不重复导航,避免两个 HomeView 同时存在导致 Duplicate GlobalKey |
|
|
// if (Get.previousRoute == Routes.home) { |
|
|
if (Get.currentRoute == Routes.home) return; |
|
|
// 如果是从home页过来的,则直接返回 |
|
|
|
|
|
// Get.back(); |
|
|
|
|
|
// } else { |
|
|
|
|
|
// 否则,跳转到home页并清空之前的路由栈 |
|
|
|
|
|
Get.offAllNamed(Routes.home); |
|
|
Get.offAllNamed(Routes.home); |
|
|
// } |
|
|
|
|
|
// }); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// ==================== 用户操作 ==================== |
|
|
// ==================== 用户操作 ==================== |
|
|
@ -1259,6 +1337,7 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
UserDevice( |
|
|
UserDevice( |
|
|
id: 0, // 临时设备ID |
|
|
id: 0, // 临时设备ID |
|
|
uid: User.instance.uid, |
|
|
uid: User.instance.uid, |
|
|
|
|
|
productid: _verifiedDevicePid ?? -1, |
|
|
devicetype: 1, |
|
|
devicetype: 1, |
|
|
devicemac: _currentClassicDevice?.address ?? '', |
|
|
devicemac: _currentClassicDevice?.address ?? '', |
|
|
devicename: _currentClassicDevice?.name ?? 'Unknown Device', |
|
|
devicename: _currentClassicDevice?.name ?? 'Unknown Device', |
|
|
|