修复蓝牙扫描界面不自动扫描的bug

This commit is contained in:
wyf
2025-07-14 10:07:07 +08:00
parent 27273ff18d
commit bd5710c040
2 changed files with 102 additions and 93 deletions

View File

@@ -189,6 +189,7 @@ class MHTBlueToothController extends GetControllerEx<MHTBlueToothModel> {
return res; return res;
} else { } else {
model.deviceDataStatus = []; model.deviceDataStatus = [];
return ApiResponse(code:1, msg: "".tr);
} }
} catch (e) { } catch (e) {
print("获取设备状态异常: $e"); print("获取设备状态异常: $e");
@@ -218,10 +219,10 @@ class MHTBlueToothController extends GetControllerEx<MHTBlueToothModel> {
"macA": bleDevice.macA, "macA": bleDevice.macA,
if (bleDevice.macB != null && bleDevice.macB!.isNotEmpty) if (bleDevice.macB != null && bleDevice.macB!.isNotEmpty)
"macB": bleDevice.macB, "macB": bleDevice.macB,
if (bleDevice.name!=null && bleDevice.name!.isNotEmpty) if (bleDevice.name != null && bleDevice.name!.isNotEmpty)
'param':{ 'param': {
'name':bleDevice.name, 'name': bleDevice.name,
}, },
}; };
var response = var response =
@@ -237,8 +238,8 @@ class MHTBlueToothController extends GetControllerEx<MHTBlueToothModel> {
// personController.currentPersonId.value = res.data['id']; // personController.currentPersonId.value = res.data['id'];
//todo 绑定成功需要返回传感器id //todo 绑定成功需要返回传感器id
currentDeviceMac.value = ""; currentDeviceMac.value = "";
if(res.data!=null){ if (res.data != null) {
if(currentFullDevice!=null){ if (currentFullDevice != null) {
currentFullDevice!.macAID = res.data['macA']; currentFullDevice!.macAID = res.data['macA'];
currentFullDevice!.macBID = res.data['macB']; currentFullDevice!.macBID = res.data['macB'];
} }

View File

@@ -112,97 +112,105 @@ class _MHTBlueteethDevicePageState extends State<MHTBlueteethDevicePage> {
} }
void _startScanning() async { void _startScanning() async {
if (!mounted || isScanning || !mhtBlueToothController.shouldScan.value) try {
return; if (!mounted || isScanning || !mhtBlueToothController.shouldScan.value)
return;
_scanSubscription?.cancel(); _scanSubscription?.cancel();
var bluetoothState = await FlutterBluePlus.isOn; var bluetoothState = await FlutterBluePlus.isOn;
mhtBlueToothController.model.bluetooth = bluetoothState; mhtBlueToothController.model.bluetooth = bluetoothState;
mhtBlueToothController.updateAll();
if (!bluetoothState && !_isDialogShowing) {
_isDialogShowing = true;
mhtBlueToothController.model.blueRawData = [];
mhtBlueToothController.model.deviceDataStatus = [];
mhtBlueToothController.updateAll(); mhtBlueToothController.updateAll();
await _showBluetoothNotEnabledDialog();
_isDialogShowing = false;
return;
}
if (!isScanning) { if (!bluetoothState && !_isDialogShowing) {
setState(() { _isDialogShowing = true;
isScanning = true; mhtBlueToothController.model.blueRawData = [];
}); mhtBlueToothController.model.deviceDataStatus = [];
mhtBlueToothController.updateAll();
await FlutterBluePlus.startScan(timeout: Duration(seconds: 10)); await _showBluetoothNotEnabledDialog();
_isDialogShowing = false;
_scanSubscription = FlutterBluePlus.scanResults.listen((results) { return;
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<String>());
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 = <BlueToothDataModel>[];
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 (!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<String>());
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 = <BlueToothDataModel>[];
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;
});
} }
} }