更新
This commit is contained in:
@@ -494,132 +494,132 @@ 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; // 重试2次
|
||||
const int maxRetries = 2;
|
||||
const Duration timeout = Duration(seconds: 5);
|
||||
String? macAddress;
|
||||
|
||||
for (int attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
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));
|
||||
|
||||
// 根据设备类型获取 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; // 抛出最后的异常
|
||||
}
|
||||
|
||||
// 否则继续尝试
|
||||
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));
|
||||
|
||||
// 正常不会到这里
|
||||
throw Exception("未知错误");
|
||||
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; // 重试2次
|
||||
// const Duration timeout = Duration(seconds: 5);
|
||||
// String? macAddress;
|
||||
|
||||
// for (int attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
// 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));
|
||||
|
||||
// // 根据设备类型获取 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(
|
||||
String mac, BlueToothDataModel bleDevice, BuildContext context) async {
|
||||
bool flag = false;
|
||||
|
||||
Reference in New Issue
Block a user