This commit is contained in:
wyf
2025-08-04 10:10:44 +08:00
parent bc14dae324
commit 13ef007f88
13 changed files with 207 additions and 249 deletions

View File

@@ -280,7 +280,7 @@ class _DeviceComponentWidgetState extends State<DeviceComponentWidget> {
1;
}
});
await homeController.getPersonList();
await homeController.getPersonList();
} else {
blueteethBindController.resumeScanning();
blueteethBindController.currentDeviceMac.value = "";
@@ -494,59 +494,130 @@ class _DeviceComponentWidgetState extends State<DeviceComponentWidget> {
}
//获取智能床/床垫mac
// Future<String> getBindTHMAC(
// BuildContext context, BlueToothDataModel device, Map deviceType) async {
// const int maxRetries = 2;
// const Duration timeout = Duration(seconds: 5);
// String? macAddress;
// try {
// // 连接设备
// THapp bledevice = THapp(device: device.scanResult.device);
// await bledevice.connect();
// var res2 = bledevice.isConnected;
// if (!res2) {
// edm.EasyDartModule.logger.error("蓝牙连接失败".tr);
// DailyLogUtils.printLog("蓝牙连接失败".tr);
// TopSlideNotification.show(
// context,
// text: "蓝牙连接失败".tr,
// textColor: themeController.currentColor.sc9,
// );
// throw Exception("蓝牙连接失败".tr);
// }
// blueteethBindController.blueConnectFlag.value = 2;
// blueteethBindController.currentDevice = bledevice;
// await Future.delayed(Duration(seconds: 2));
// if (deviceType['type'] == 3) {
// //智能床垫
// macAddress = await getMacFromType3(bledevice, timeout);
// } else if (deviceType['type'] == 2) {
// //智能床
// macAddress = await getMacFromType2(bledevice, timeout);
// } else {
// throw Exception("不支持的设备类型".tr);
// }
// if (macAddress == null) {
// throw Exception("未能获取到MAC地址".tr);
// }
// // device.macA = macAddress;
// print('MAC地址: $macAddress');
// return macAddress;
// } catch (e) {
// blueteethBindController.currentDeviceMac.value = "";
// edm.EasyDartModule.logger.error("蓝牙获取MAC失败$e");
// DailyLogUtils.printLog("蓝牙获取MAC失败$e");
// TopSlideNotification.show(
// context,
// // text: e.message ?? "设备连接失败,请重试".tr,
// text: "获取不到传感器mac,请重试".tr,
// textColor: themeController.currentColor.sc9,
// );
// rethrow;
// }
// }
Future<String> getBindTHMAC(
BuildContext context, BlueToothDataModel device, Map deviceType) async {
const int maxRetries = 2;
const int maxRetries = 2; // 重试2次
const Duration timeout = Duration(seconds: 5);
String? macAddress;
try {
// 连接设备
THapp bledevice = THapp(device: device.scanResult.device);
await bledevice.connect();
var res2 = bledevice.isConnected;
if (!res2) {
edm.EasyDartModule.logger.error("蓝牙连接失败".tr);
DailyLogUtils.printLog("蓝牙连接失败".tr);
TopSlideNotification.show(
context,
text: "蓝牙连接失败".tr,
textColor: themeController.currentColor.sc9,
);
throw Exception("蓝牙连接失败".tr);
}
blueteethBindController.blueConnectFlag.value = 2;
blueteethBindController.currentDevice = bledevice;
await Future.delayed(Duration(seconds: 2));
if (deviceType['type'] == 3) {
//智能床垫
macAddress = await getMacFromType3(bledevice, timeout);
} else if (deviceType['type'] == 2) {
//智能床
macAddress = await getMacFromType2(bledevice, timeout);
} else {
throw Exception("不支持的设备类型".tr);
}
for (int attempt = 1; attempt <= maxRetries; attempt++) {
try {
// 连接设备
THapp bledevice = THapp(device: device.scanResult.device);
await bledevice.connect();
var res2 = bledevice.isConnected;
if (macAddress == null) {
throw Exception("未能获取到MAC地址".tr);
}
if (!res2) {
edm.EasyDartModule.logger.error("蓝牙连接失败".tr);
DailyLogUtils.printLog("蓝牙连接失败".tr);
TopSlideNotification.show(
context,
text: "蓝牙连接失败".tr,
textColor: themeController.currentColor.sc9,
);
throw Exception("蓝牙连接失败".tr);
}
// device.macA = macAddress;
print('MAC地址: $macAddress');
return macAddress;
} catch (e) {
blueteethBindController.currentDeviceMac.value = "";
edm.EasyDartModule.logger.error("蓝牙获取MAC失败$e");
DailyLogUtils.printLog("蓝牙获取MAC失败$e");
TopSlideNotification.show(
context,
// text: e.message ?? "设备连接失败,请重试".tr,
text: "获取不到传感器mac,请重试".tr,
textColor: themeController.currentColor.sc9,
);
rethrow;
blueteethBindController.blueConnectFlag.value = 2;
blueteethBindController.currentDevice = bledevice;
await Future.delayed(Duration(seconds: 2));
// 根据设备类型获取 MAC
if (deviceType['type'] == 3) {
macAddress = await getMacFromType3(bledevice, timeout);
} else if (deviceType['type'] == 2) {
macAddress = await getMacFromType2(bledevice, timeout);
} else {
throw Exception("不支持的设备类型".tr);
}
if (macAddress != null) {
print('MAC地址: $macAddress');
return macAddress; // 成功获取直接返回
}
// 当前尝试失败,准备重试
DailyLogUtils.printLog("$attempt次尝试未获取到MAC地址");
if (attempt < maxRetries) {
await Future.delayed(Duration(seconds: 1)); // 等待后重试
}
} catch (e) {
DailyLogUtils.printLog("$attempt次蓝牙获取MAC失败: $e");
edm.EasyDartModule.logger.error("$attempt次蓝牙获取MAC失败: $e");
// 最后一次尝试失败后才提示
if (attempt == maxRetries) {
blueteethBindController.currentDeviceMac.value = "";
TopSlideNotification.show(
context,
text: "获取不到传感器mac,请重试".tr,
textColor: themeController.currentColor.sc9,
);
rethrow; // 抛出最后的异常
}
// 否则继续尝试
}
}
// 正常不会到这里
throw Exception("未知错误");
}
fillTHMac(