更新
This commit is contained in:
@@ -8,6 +8,7 @@ import 'package:flutterflow_ui/flutterflow_ui.dart';
|
||||
import 'package:permission_handler/permission_handler.dart'; // 引入permission_handler
|
||||
import 'package:vbvs_app/common/util/FitTool.dart';
|
||||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||||
import 'package:vbvs_app/component/tool/ClickableContainer.dart';
|
||||
import 'package:vbvs_app/controller/device/blueteeth_bind_controller.dart';
|
||||
import 'package:vbvs_app/controller/main_bottom/global_controller.dart';
|
||||
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
|
||||
@@ -51,6 +52,8 @@ class _BlueteethDevicePageState extends State<BlueteethDevicePage> {
|
||||
flutterBlue = FlutterBluePlus(); // 初始化flutterBlue实例
|
||||
_checkBluetoothPermission(); // 检查蓝牙权限
|
||||
Get.find<BlueteethBindController>().startStatusPolling();
|
||||
blueteethBindController.search.value = "";
|
||||
blueteethBindController.currentDeviceMac?.value = "";
|
||||
}
|
||||
|
||||
// 检查蓝牙权限
|
||||
@@ -131,15 +134,94 @@ class _BlueteethDevicePageState extends State<BlueteethDevicePage> {
|
||||
await FlutterBluePlus.startScan(timeout: Duration(seconds: 10));
|
||||
// await FlutterBluePlus.startScan(timeout: Duration(minutes: 30));
|
||||
|
||||
// _scanSubscription = FlutterBluePlus.scanResults.listen((results) {
|
||||
// if (!mounted) return; // 确保页面未销毁
|
||||
// final signalThreshold = blueteethBindController.model.singal!;
|
||||
// final filteredResults = results
|
||||
// .where((r) =>
|
||||
// r.rssi > signalThreshold &&
|
||||
// r.advertisementData.localName == "AITH-V2" &&
|
||||
// r.advertisementData.manufacturerData.containsKey(0xFFED))
|
||||
// .toList();
|
||||
|
||||
// // 解析数据
|
||||
// final parsedDeviceList = <BleDeviceData>[];
|
||||
|
||||
// for (var r in filteredResults) {
|
||||
// try {
|
||||
// List<int> rawData = r.advertisementData.manufacturerData[0xFFED]!;
|
||||
// BleDeviceData deviceData = parseBleData(rawData);
|
||||
// deviceData.name = r.advertisementData.advName;
|
||||
// deviceData.rssi = r.rssi;
|
||||
// deviceData.mac = deviceData.deviceId.replaceAll(':', '');
|
||||
// parsedDeviceList.add(deviceData);
|
||||
// if (deviceData.mac!.toLowerCase() == 'b43a45c3dfa0') {
|
||||
// print('匹配设备数据: ${deviceData.mac}-->sn:${deviceData.sn}');
|
||||
// }
|
||||
// } catch (e) {
|
||||
// print("设备数据解析失败: $e");
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 使用一个临时变量 lastDeviceList 来比较是否有变化
|
||||
// setState(() {
|
||||
// bool hasChanges = false;
|
||||
|
||||
// // 如果 devicelist 长度不同或内容有差异,认为有变化
|
||||
// if (blueteethBindController.model.devicelist?.length !=
|
||||
// parsedDeviceList.length) {
|
||||
// hasChanges = true;
|
||||
// } else {
|
||||
// // 深度比较每个设备的属性(比如 mac, rssi)
|
||||
// for (int i = 0;
|
||||
// i < blueteethBindController.model.devicelist!.length;
|
||||
// i++) {
|
||||
// if (blueteethBindController.model.devicelist![i].mac !=
|
||||
// parsedDeviceList[i].mac ||
|
||||
// blueteethBindController.model.devicelist![i].rssi !=
|
||||
// parsedDeviceList[i].rssi) {
|
||||
// hasChanges = true;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 如果有变化,更新 devicelist 和 blelist
|
||||
// if (hasChanges) {
|
||||
// blueteethBindController.model.devicelist = parsedDeviceList;
|
||||
// blueteethBindController.model.blelist = filteredResults;
|
||||
// // blueteethBindController.updateDeviceStatus();
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
|
||||
_scanSubscription = FlutterBluePlus.scanResults.listen((results) {
|
||||
if (!mounted) return; // 确保页面未销毁
|
||||
if (!mounted) return;
|
||||
|
||||
final signalThreshold = blueteethBindController.model.singal!;
|
||||
final filteredResults = results
|
||||
.where((r) =>
|
||||
r.rssi > signalThreshold &&
|
||||
r.advertisementData.localName == "AITH-V2" &&
|
||||
r.advertisementData.manufacturerData.containsKey(0xFFED))
|
||||
.toList();
|
||||
final searchKey =
|
||||
blueteethBindController.search.value.trim().toLowerCase();
|
||||
|
||||
final filteredResults = results.where((r) {
|
||||
final isTarget = r.rssi > signalThreshold &&
|
||||
r.advertisementData.localName == "AITH-V2" &&
|
||||
r.advertisementData.manufacturerData.containsKey(0xFFED);
|
||||
|
||||
if (!isTarget) return false;
|
||||
|
||||
// 搜索关键字过滤(根据名称和 MAC 地址模糊匹配)
|
||||
final name = r.advertisementData.advName.toLowerCase();
|
||||
final mac = r.device.remoteId.str.replaceAll(':', '').toLowerCase();
|
||||
final search = searchKey.trim().replaceAll(':', '').toLowerCase();
|
||||
|
||||
if (search.isNotEmpty &&
|
||||
!name.contains(search) &&
|
||||
!mac.contains(search)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}).toList();
|
||||
|
||||
// 解析数据
|
||||
final parsedDeviceList = <BleDeviceData>[];
|
||||
@@ -152,24 +234,23 @@ class _BlueteethDevicePageState extends State<BlueteethDevicePage> {
|
||||
deviceData.rssi = r.rssi;
|
||||
deviceData.mac = deviceData.deviceId.replaceAll(':', '');
|
||||
parsedDeviceList.add(deviceData);
|
||||
if (deviceData.mac!.toLowerCase() == 'b43a45c3dfa0') {
|
||||
print('匹配设备数据: ${deviceData.mac}-->sn:${deviceData.sn}');
|
||||
}
|
||||
|
||||
// if (deviceData.mac!.toLowerCase() == 'b43a45c3dfa0') {
|
||||
// print('匹配设备数据: ${deviceData.mac}-->sn:${deviceData.sn}');
|
||||
// }
|
||||
} catch (e) {
|
||||
print("设备数据解析失败: $e");
|
||||
}
|
||||
}
|
||||
|
||||
// 使用一个临时变量 lastDeviceList 来比较是否有变化
|
||||
// 判断是否更新
|
||||
setState(() {
|
||||
bool hasChanges = false;
|
||||
|
||||
// 如果 devicelist 长度不同或内容有差异,认为有变化
|
||||
if (blueteethBindController.model.devicelist?.length !=
|
||||
parsedDeviceList.length) {
|
||||
hasChanges = true;
|
||||
} else {
|
||||
// 深度比较每个设备的属性(比如 mac, rssi)
|
||||
for (int i = 0;
|
||||
i < blueteethBindController.model.devicelist!.length;
|
||||
i++) {
|
||||
@@ -183,11 +264,9 @@ class _BlueteethDevicePageState extends State<BlueteethDevicePage> {
|
||||
}
|
||||
}
|
||||
|
||||
// 如果有变化,更新 devicelist 和 blelist
|
||||
if (hasChanges) {
|
||||
blueteethBindController.model.devicelist = parsedDeviceList;
|
||||
blueteethBindController.model.blelist = filteredResults;
|
||||
// blueteethBindController.updateDeviceStatus();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -360,6 +439,7 @@ class _BlueteethDevicePageState extends State<BlueteethDevicePage> {
|
||||
newValue.toStringAsFixed(0));
|
||||
blueteethBindController.model.singal =
|
||||
newValue;
|
||||
_startScanning();
|
||||
blueteethBindController.updateAll();
|
||||
},
|
||||
);
|
||||
@@ -425,6 +505,12 @@ class _BlueteethDevicePageState extends State<BlueteethDevicePage> {
|
||||
child: Align(
|
||||
alignment: AlignmentDirectional(-1, 0),
|
||||
child: TextFormField(
|
||||
initialValue: blueteethBindController
|
||||
.search.value,
|
||||
onChanged: (Value) {
|
||||
blueteethBindController
|
||||
.search.value = Value;
|
||||
},
|
||||
autofocus: false,
|
||||
obscureText: false,
|
||||
decoration: InputDecoration(
|
||||
@@ -494,11 +580,8 @@ class _BlueteethDevicePageState extends State<BlueteethDevicePage> {
|
||||
fontSize: 26.rpx,
|
||||
letterSpacing: 0.0,
|
||||
),
|
||||
cursorColor: themeController
|
||||
.currentColor.sc3,
|
||||
// validator: _model
|
||||
// .textControllerValidator
|
||||
// .asValidator(context),
|
||||
cursorColor:
|
||||
themeController.currentColor.sc3,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -519,16 +602,60 @@ class _BlueteethDevicePageState extends State<BlueteethDevicePage> {
|
||||
color: stringToColor("#333333"), //固定
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'搜索'.tr,
|
||||
style: FlutterFlowTheme.of(context)
|
||||
.bodyMedium
|
||||
.override(
|
||||
fontFamily: 'Inter',
|
||||
fontSize: 30.rpx,
|
||||
letterSpacing: 0.0,
|
||||
color: stringToColor("#333333"), //固定
|
||||
),
|
||||
ClickableContainer(
|
||||
backgroundColor: Colors.transparent,
|
||||
highlightColor:
|
||||
themeController.currentColor.sc4,
|
||||
borderRadius: 6.rpx,
|
||||
padding: EdgeInsets.zero,
|
||||
// onTap: () async {
|
||||
|
||||
// blueteethBindController
|
||||
// .model.betDevicelist;
|
||||
// blueteethBindController.search.value;
|
||||
// blueteethBindController.updateAll();
|
||||
// },
|
||||
onTap: () async {
|
||||
// final searchKey = blueteethBindController
|
||||
// .search.value
|
||||
// .trim();
|
||||
// if (searchKey.isNotEmpty) {
|
||||
// final filtered = blueteethBindController
|
||||
// .model.betDevicelist!
|
||||
// .where((device) {
|
||||
// final name =
|
||||
// device.name?.toLowerCase() ?? '';
|
||||
// final mac =
|
||||
// device.mac?.toLowerCase() ?? '';
|
||||
// return name.contains(
|
||||
// searchKey.toLowerCase()) ||
|
||||
// mac.contains(
|
||||
// searchKey.toLowerCase());
|
||||
// }).toList();
|
||||
|
||||
// // 替换原始列表
|
||||
// blueteethBindController
|
||||
// .model.betDevicelist!
|
||||
// ..clear()
|
||||
// ..addAll(filtered);
|
||||
// }
|
||||
|
||||
// blueteethBindController.updateAll();
|
||||
_startScanning();
|
||||
},
|
||||
|
||||
child: Text(
|
||||
'搜索'.tr,
|
||||
style: FlutterFlowTheme.of(context)
|
||||
.bodyMedium
|
||||
.override(
|
||||
fontFamily: 'Inter',
|
||||
fontSize: 30.rpx,
|
||||
letterSpacing: 0.0,
|
||||
color:
|
||||
stringToColor("#333333"), //固定
|
||||
),
|
||||
),
|
||||
),
|
||||
].divide(SizedBox(width: 26.rpx)),
|
||||
),
|
||||
@@ -548,8 +675,10 @@ class _BlueteethDevicePageState extends State<BlueteethDevicePage> {
|
||||
EdgeInsetsDirectional.fromSTEB(19.rpx, 0, 0, 0),
|
||||
child: Obx(() {
|
||||
return Text(
|
||||
// '匹配出的外围设备'.tr +
|
||||
// "(${blueteethBindController.model.betDevicelist!.length})",
|
||||
'匹配出的外围设备'.tr +
|
||||
"(${blueteethBindController.model.betDevicelist!.length})",
|
||||
"(${blueteethBindController.model.blelist!.length})",
|
||||
style: FlutterFlowTheme.of(context)
|
||||
.bodyMedium
|
||||
.override(
|
||||
|
||||
Reference in New Issue
Block a user