更新控制跳转
This commit is contained in:
@@ -25,10 +25,12 @@ import 'package:vbvs_app/pages/mh_page/test/WebviewTestModel.dart';
|
||||
|
||||
class DeviceComponentWidget extends StatefulWidget {
|
||||
BlueToothDataModel bleDevice;
|
||||
var deviceType;
|
||||
|
||||
DeviceComponentWidget({
|
||||
super.key,
|
||||
required this.bleDevice,
|
||||
required this.deviceType,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -212,8 +214,8 @@ class _DeviceComponentWidgetState extends State<DeviceComponentWidget> {
|
||||
blueteethBindController.currentDeviceMac.value =
|
||||
widget.bleDevice.mac;
|
||||
blueteethBindController.updateAll();
|
||||
String mac =
|
||||
await getBindTHMAC(context, widget.bleDevice);
|
||||
String mac = await getBindTHMAC(
|
||||
context, widget.bleDevice, widget.deviceType);
|
||||
if (mac != null && mac.isNotEmpty) {
|
||||
bool flag =
|
||||
await fillTHMac(mac, widget.bleDevice, context);
|
||||
@@ -481,19 +483,17 @@ class _DeviceComponentWidgetState extends State<DeviceComponentWidget> {
|
||||
);
|
||||
}
|
||||
|
||||
//获取传感器mac
|
||||
//获取智能床/床垫mac
|
||||
Future<String> getBindTHMAC(
|
||||
BuildContext context, BlueToothDataModel device) async {
|
||||
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();
|
||||
await bledevice.connect();
|
||||
var res2 = bledevice.isConnected;
|
||||
|
||||
if (!res2) {
|
||||
edm.EasyDartModule.logger.error("蓝牙连接失败");
|
||||
DailyLogUtils.printLog("蓝牙连接失败");
|
||||
@@ -504,58 +504,18 @@ class _DeviceComponentWidgetState extends State<DeviceComponentWidget> {
|
||||
);
|
||||
throw Exception("蓝牙连接失败");
|
||||
}
|
||||
|
||||
blueteethBindController.blueConnectFlag.value = 2;
|
||||
blueteethBindController.currentDevice = bledevice;
|
||||
await Future.delayed(Duration(seconds: 2));
|
||||
var read = bledevice.getresource('fff0/fff1');
|
||||
await read!.characteristic.setNotifyValue(true);
|
||||
var write = bledevice.getresource('fff0/fff2');
|
||||
|
||||
for (int attempt = 0; attempt < maxRetries; attempt++) {
|
||||
var completer = Completer<String>();
|
||||
|
||||
StreamSubscription? subscription;
|
||||
subscription = read.characteristic.onValueReceived.listen((onData) {
|
||||
if (onData.length >= 14) {
|
||||
// 按照你提供的协议,返回数据长度至少 17字节,这里保险起见 14起步
|
||||
String parsedMac = parseMacFromBleResponse(onData);
|
||||
completer.complete(parsedMac);
|
||||
}
|
||||
});
|
||||
|
||||
// 发送查询命令
|
||||
var order = [
|
||||
0xFF,
|
||||
0xFF,
|
||||
0xFF,
|
||||
0xFF,
|
||||
0x00,
|
||||
0x03,
|
||||
0x40,
|
||||
0x01,
|
||||
0x01,
|
||||
0x00,
|
||||
0x45,
|
||||
0xFD
|
||||
];
|
||||
await write!.characteristic.write(order);
|
||||
|
||||
try {
|
||||
macAddress = await completer.future.timeout(timeout);
|
||||
await subscription.cancel();
|
||||
break; // 成功拿到,跳出重试
|
||||
} catch (e) {
|
||||
await subscription.cancel();
|
||||
if (attempt == maxRetries - 1) {
|
||||
TopSlideNotification.show(
|
||||
context,
|
||||
text: "获取MAC地址超时,请重试".tr,
|
||||
textColor: themeController.currentColor.sc9,
|
||||
);
|
||||
throw Exception("获取MAC地址失败");
|
||||
}
|
||||
}
|
||||
if (deviceType['type'] == 3) {
|
||||
//智能床垫
|
||||
macAddress = await getMacFromType3(bledevice, timeout);
|
||||
} else if (deviceType['type'] == 2) {
|
||||
//智能床
|
||||
macAddress = await getMacFromType2(bledevice, timeout);
|
||||
} else {
|
||||
throw Exception("不支持的设备类型");
|
||||
}
|
||||
|
||||
if (macAddress == null) {
|
||||
@@ -629,6 +589,87 @@ class _DeviceComponentWidgetState extends State<DeviceComponentWidget> {
|
||||
);
|
||||
return flag;
|
||||
}
|
||||
|
||||
Future<String> getMacFromType3(THapp bledevice, Duration timeout) async {
|
||||
final read = bledevice.getresource('fff0/fff1');
|
||||
await read!.characteristic.setNotifyValue(true);
|
||||
final write = bledevice.getresource('fff0/fff2');
|
||||
|
||||
const int maxRetries = 2;
|
||||
for (int attempt = 0; attempt < maxRetries; attempt++) {
|
||||
final completer = Completer<String>();
|
||||
final subscription = read.characteristic.onValueReceived.listen((data) {
|
||||
if (data.length >= 14) {
|
||||
completer.complete(parseMacFromBleResponse(data));
|
||||
}
|
||||
});
|
||||
|
||||
final order = [
|
||||
0xFF,
|
||||
0xFF,
|
||||
0xFF,
|
||||
0xFF,
|
||||
0x00,
|
||||
0x03,
|
||||
0x40,
|
||||
0x01,
|
||||
0x01,
|
||||
0x00,
|
||||
0x45,
|
||||
0xFD
|
||||
];
|
||||
await write!.characteristic.write(order);
|
||||
|
||||
try {
|
||||
final mac = await completer.future.timeout(timeout);
|
||||
await subscription.cancel();
|
||||
return mac;
|
||||
} catch (_) {
|
||||
await subscription.cancel();
|
||||
if (attempt == maxRetries - 1) rethrow;
|
||||
}
|
||||
}
|
||||
throw Exception("获取MAC超时");
|
||||
}
|
||||
|
||||
Future<String> getMacFromType2(THapp bledevice, Duration timeout) async {
|
||||
try {
|
||||
final read = bledevice.getresource('ffe0/ffe1');
|
||||
await read!.characteristic.setNotifyValue(true);
|
||||
|
||||
final write =
|
||||
bledevice.getresource('ffe0/ffe1'); // 与 read 同 characteristic
|
||||
const int maxRetries = 2;
|
||||
for (int attempt = 0; attempt < maxRetries; attempt++) {
|
||||
final completer = Completer<String>();
|
||||
final subscription = read.characteristic.onValueReceived.listen((data) {
|
||||
if (data.length >= 17) {
|
||||
completer.complete(parseMacFromTH2Response(data));
|
||||
}
|
||||
});
|
||||
|
||||
final order = [0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x0C, 0x0B, 0x0A];
|
||||
int checksum = order.reduce((a, b) => a + b) & 0xFFFF;
|
||||
order.add(checksum & 0xFF); // 低位
|
||||
order.add((checksum >> 8) & 0xFF); // 高位
|
||||
|
||||
await write!.characteristic.write(order);
|
||||
|
||||
try {
|
||||
final mac = await completer.future.timeout(timeout);
|
||||
await subscription.cancel();
|
||||
return mac;
|
||||
} catch (_) {
|
||||
await subscription.cancel();
|
||||
if (attempt == maxRetries - 1) rethrow;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
ef.log("[获取设备 MAC]:失败:$e");
|
||||
}
|
||||
|
||||
throw Exception("获取MAC超时");
|
||||
}
|
||||
}
|
||||
|
||||
String parseMacFromBleResponse(List<int> data) {
|
||||
@@ -650,3 +691,21 @@ String parseMacFromBleResponse(List<int> data) {
|
||||
throw Exception("BLE返回数据格式不正确");
|
||||
}
|
||||
}
|
||||
|
||||
String parseMacFromTH2Response(List<int> data) {
|
||||
if (data.length < 17) {
|
||||
throw Exception("数据长度不足,无法解析MAC");
|
||||
}
|
||||
|
||||
int status = data[8];
|
||||
if (status != 0x03 && status != 0x04) {
|
||||
throw Exception("未连接心率带");
|
||||
}
|
||||
|
||||
// 提取9~14字节的MAC地址
|
||||
List<int> macBytes = data.sublist(9, 15);
|
||||
return macBytes
|
||||
.map((b) => b.toRadixString(16).padLeft(2, '0'))
|
||||
.join(":")
|
||||
.toUpperCase();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user