From bd5710c0405e876fa1181810e0c6f7e5ec4334f0 Mon Sep 17 00:00:00 2001 From: wyf <494641114@qq.com> Date: Mon, 14 Jul 2025 10:07:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=93=9D=E7=89=99=E6=89=AB?= =?UTF-8?q?=E6=8F=8F=E7=95=8C=E9=9D=A2=E4=B8=8D=E8=87=AA=E5=8A=A8=E6=89=AB?= =?UTF-8?q?=E6=8F=8F=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/mht_bluetooth_controller.dart | 13 +- .../device/mht_blueteeth_device_page.dart | 182 +++++++++--------- 2 files changed, 102 insertions(+), 93 deletions(-) diff --git a/lib/pages/mh_page/device/controller/mht_bluetooth_controller.dart b/lib/pages/mh_page/device/controller/mht_bluetooth_controller.dart index f674ef5..02125d2 100644 --- a/lib/pages/mh_page/device/controller/mht_bluetooth_controller.dart +++ b/lib/pages/mh_page/device/controller/mht_bluetooth_controller.dart @@ -189,6 +189,7 @@ class MHTBlueToothController extends GetControllerEx { return res; } else { model.deviceDataStatus = []; + return ApiResponse(code:1, msg: "".tr); } } catch (e) { print("获取设备状态异常: $e"); @@ -218,10 +219,10 @@ class MHTBlueToothController extends GetControllerEx { "macA": bleDevice.macA, if (bleDevice.macB != null && bleDevice.macB!.isNotEmpty) "macB": bleDevice.macB, - if (bleDevice.name!=null && bleDevice.name!.isNotEmpty) - 'param':{ - 'name':bleDevice.name, - }, + if (bleDevice.name != null && bleDevice.name!.isNotEmpty) + 'param': { + 'name': bleDevice.name, + }, }; var response = @@ -237,8 +238,8 @@ class MHTBlueToothController extends GetControllerEx { // personController.currentPersonId.value = res.data['id']; //todo 绑定成功需要返回传感器id currentDeviceMac.value = ""; - if(res.data!=null){ - if(currentFullDevice!=null){ + if (res.data != null) { + if (currentFullDevice != null) { currentFullDevice!.macAID = res.data['macA']; currentFullDevice!.macBID = res.data['macB']; } diff --git a/lib/pages/mh_page/device/mht_blueteeth_device_page.dart b/lib/pages/mh_page/device/mht_blueteeth_device_page.dart index 75ffa9f..c19d92b 100644 --- a/lib/pages/mh_page/device/mht_blueteeth_device_page.dart +++ b/lib/pages/mh_page/device/mht_blueteeth_device_page.dart @@ -112,97 +112,105 @@ class _MHTBlueteethDevicePageState extends State { } void _startScanning() async { - if (!mounted || isScanning || !mhtBlueToothController.shouldScan.value) - return; + try { + if (!mounted || isScanning || !mhtBlueToothController.shouldScan.value) + return; - _scanSubscription?.cancel(); - var bluetoothState = await FlutterBluePlus.isOn; - mhtBlueToothController.model.bluetooth = bluetoothState; - mhtBlueToothController.updateAll(); - - if (!bluetoothState && !_isDialogShowing) { - _isDialogShowing = true; - mhtBlueToothController.model.blueRawData = []; - mhtBlueToothController.model.deviceDataStatus = []; + _scanSubscription?.cancel(); + var bluetoothState = await FlutterBluePlus.isOn; + mhtBlueToothController.model.bluetooth = bluetoothState; mhtBlueToothController.updateAll(); - await _showBluetoothNotEnabledDialog(); - _isDialogShowing = false; - return; - } - if (!isScanning) { - setState(() { - isScanning = true; - }); - - await FlutterBluePlus.startScan(timeout: Duration(seconds: 10)); - - _scanSubscription = FlutterBluePlus.scanResults.listen((results) { - if (!mounted) return; - - final signalThreshold = mhtBlueToothController.model.singal!; - final searchKey = - mhtBlueToothController.search.value.trim().toLowerCase(); - - final filteredResults = results.where((r) { - final localName = r.advertisementData.localName; - final isTarget = r.rssi > signalThreshold && - isTargetDevice( - localName, widget.deviceType['reg'].cast()); - if (!isTarget) return false; - final name = r.advertisementData.advName.toLowerCase(); - String macAddress = r.device.remoteId.str; - final mac = macAddress.replaceAll(':', ''); - final search = searchKey.trim().replaceAll(':', '').toLowerCase(); - - if (search.isNotEmpty && - !name.contains(search) && - !mac.replaceAll(':', '').toLowerCase().contains(search)) { - return false; - } - - return true; - }).map((r) { - return BlueToothDataModel.fromScanResult( - r, widget.deviceType['type']?.toInt(), - bind: false, - name: r.advertisementData.localName, - mac: r.device.remoteId.str.replaceAll(':', '')); - }).toList(); - - final currentDevices = mhtBlueToothController.model.blueRawData ?? []; - final newDevices = []; - - for (var newDevice in filteredResults) { - // 检查设备是否已存在 - final existingIndex = - currentDevices.indexWhere((d) => d.mac == newDevice.mac); - - if (existingIndex >= 0) { - // 更新已有设备信息(如信号强度) - currentDevices[existingIndex] = newDevice; - } else { - // 添加新设备 - newDevices.add(newDevice); - } - } - - setState(() { - mhtBlueToothController.model.blueRawData = [ - ...currentDevices, - ...newDevices - ]; - }); - }); - - await Future.delayed(Duration(seconds: 10)); - await FlutterBluePlus.stopScan(); - - if (mounted) { - setState(() { - isScanning = false; - }); + if (!bluetoothState && !_isDialogShowing) { + _isDialogShowing = true; + mhtBlueToothController.model.blueRawData = []; + mhtBlueToothController.model.deviceDataStatus = []; + mhtBlueToothController.updateAll(); + await _showBluetoothNotEnabledDialog(); + _isDialogShowing = false; + return; } + + if (!isScanning) { + setState(() { + isScanning = true; + }); + + await FlutterBluePlus.startScan(timeout: Duration(seconds: 10)); + + _scanSubscription = FlutterBluePlus.scanResults.listen((results) { + if (!mounted) return; + + final signalThreshold = mhtBlueToothController.model.singal!; + final searchKey = + mhtBlueToothController.search.value.trim().toLowerCase(); + + final filteredResults = results.where((r) { + final localName = r.advertisementData.localName; + final isTarget = r.rssi > signalThreshold && + isTargetDevice( + localName, widget.deviceType['reg'].cast()); + if (!isTarget) return false; + final name = r.advertisementData.advName.toLowerCase(); + String macAddress = r.device.remoteId.str; + final mac = macAddress.replaceAll(':', ''); + final search = searchKey.trim().replaceAll(':', '').toLowerCase(); + + if (search.isNotEmpty && + !name.contains(search) && + !mac.replaceAll(':', '').toLowerCase().contains(search)) { + return false; + } + + return true; + }).map((r) { + return BlueToothDataModel.fromScanResult( + r, widget.deviceType['type']?.toInt(), + bind: false, + name: r.advertisementData.localName, + mac: r.device.remoteId.str.replaceAll(':', '')); + }).toList(); + + final currentDevices = mhtBlueToothController.model.blueRawData ?? []; + final newDevices = []; + + for (var newDevice in filteredResults) { + // 检查设备是否已存在 + final existingIndex = + currentDevices.indexWhere((d) => d.mac == newDevice.mac); + + if (existingIndex >= 0) { + // 更新已有设备信息(如信号强度) + currentDevices[existingIndex] = newDevice; + } else { + // 添加新设备 + newDevices.add(newDevice); + } + } + + setState(() { + mhtBlueToothController.model.blueRawData = [ + ...currentDevices, + ...newDevices + ]; + }); + }); + + await Future.delayed(Duration(seconds: 10)); + await FlutterBluePlus.stopScan(); + + if (mounted) { + setState(() { + isScanning = false; + }); + } + } + } catch (e) { + ef.log("$e"); + } finally { + setState(() { + isScanning = false; + }); } }