feat:增加兼容32bit广播信号

This commit is contained in:
liyi 2025-03-03 18:29:37 +08:00
parent 0d77d3aff4
commit dc37b42977
4 changed files with 179 additions and 38 deletions

View File

@ -253,14 +253,20 @@ class BlueManage {
FlutterBluePlus.scanResults.listen((List<ScanResult> results) { FlutterBluePlus.scanResults.listen((List<ScanResult> results) {
scanDevices.clear(); scanDevices.clear();
for (final ScanResult scanResult in results) { for (final ScanResult scanResult in results) {
// AppLog.log('扫描到的设备:${scanResult.device.platformName} ${scanResult.advertisementData.advName} ${scanResult.rssi}'); if (scanResult.advertisementData.serviceUuids.isNotEmpty) {
// AppLog.log(
// '扫描到的设备:${scanResult.advertisementData.serviceUuids[0].toString()}');
} else {
continue;
}
final isMatch = _isMatch(
scanResult.advertisementData.serviceUuids
.map((e) => e.uuid)
.toList(),
deviceType);
// //
if (((scanResult.advertisementData.serviceUuids.isNotEmpty if (isMatch && (scanResult.rssi >= -100)) {
? scanResult.advertisementData.serviceUuids[0]
: '')
.toString()
.contains(getDeviceType(deviceType))) &&
(scanResult.rssi >= -100)) {
// id相同的元素 // id相同的元素
final int knownDeviceIndex = scanDevices.indexWhere( final int knownDeviceIndex = scanDevices.indexWhere(
(ScanResult d) => (ScanResult d) =>
@ -300,6 +306,23 @@ class BlueManage {
}); });
} }
/// uuid
bool _isMatch(List<String> serviceUuids, DeviceType deviceType) {
//
List<String> deviceTypeList = getDeviceType(deviceType);
// serviceUuids deviceTypeList
if (serviceUuids != null && serviceUuids.isNotEmpty) {
return serviceUuids.any((uuid) {
return deviceTypeList
.any((type) => uuid.toLowerCase().contains(type.toLowerCase()));
});
}
// serviceUuids false
return false;
}
/// List senderData, /// List senderData,
Future<void> blueSendData( Future<void> blueSendData(
String deviceName, ConnectStateCallBack stateCallBack, String deviceName, ConnectStateCallBack stateCallBack,
@ -499,8 +522,39 @@ class BlueManage {
} }
AppLog.log('调用了停止扫描的方法'); AppLog.log('调用了停止扫描的方法');
await stopScan(); await stopScan();
if (scanResult.advertisementData.serviceUuids[0].toString().length >= 5 &&
(scanResult.advertisementData.serviceUuids[0].toString()[5] == '0') &&
isAddEquipment == false) {
//
if (isReconnect == true) {
AppLog.log('该锁已被重置, 重新发送扫描命令');
if ((scanResult.advertisementData.serviceUuids[0].toString()[31] == '0') && BuglyTool.uploadException(
message: '该锁已被重置, 重新发送扫描命令startScanSingle 上传记录当前方法是_connectDevice',
detail:
'添加这个判断是因为有些苹果设备或者安卓等性能比较好的设备时,添加完锁之后,锁板未改变为已添加状态之前,就进行了蓝牙连接,导致添加完锁就失败,这里进行了判断,如果第一次连接失败,就清除缓存重新扫描连接 该锁已被重置, 重新发送扫描命令 serviceUuids:${scanResult.advertisementData.serviceUuids[0].toString()}',
upload: false);
scanDevices.clear();
startScanSingle(deviceName, 15, (List<ScanResult> scanDevices) {
_connectDevice(scanDevices, deviceName, connectStateCallBack,
isAddEquipment: isAddEquipment, isReconnect: false);
});
} else {
connectStateCallBack(BluetoothConnectionState.disconnected);
EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds);
scanDevices.clear();
BuglyTool.uploadException(
message: '提示该锁已被重置, 回调断开连接, 清除缓存上传记录当前方法是_connectDevice',
detail:
'isReconnect:$isReconnect serviceUuids:${scanResult.advertisementData.serviceUuids[0].toString()}',
upload: false);
}
return;
}
if (scanResult.advertisementData.serviceUuids[0].toString().length >= 30 &&
(scanResult.advertisementData.serviceUuids[0].toString()[31] == '0') &&
isAddEquipment == false) { isAddEquipment == false) {
// //
if (isReconnect == true) { if (isReconnect == true) {

View File

@ -5,14 +5,14 @@ enum DeviceType {
gateway // 758825 gateway // 758825
} }
String getDeviceType(DeviceType deviceType) { List<String> getDeviceType(DeviceType deviceType) {
String t = '758824'; List<String> t = ['758824'];
switch (deviceType) { switch (deviceType) {
case DeviceType.blue: case DeviceType.blue:
t = '758824'; t = ['758824', '75'];
break; break;
case DeviceType.gateway: case DeviceType.gateway:
t = '758825'; t = ['758825'];
break; break;
} }
return t; return t;

View File

@ -51,9 +51,9 @@ class NearbyLockLogic extends BaseGetXController {
}); });
BlueManage().blueSendData(deviceName, BlueManage().blueSendData(deviceName,
(BluetoothConnectionState state) async { (BluetoothConnectionState state) async {
// AppLog.log('点击要添加的设备了'); AppLog.log('点击要添加的设备了');
if (state == BluetoothConnectionState.connected) { if (state == BluetoothConnectionState.connected) {
// AppLog.log('开始获取公钥'); AppLog.log('开始获取公钥');
IoSenderManage.getPublicKey(lockId: deviceName); IoSenderManage.getPublicKey(lockId: deviceName);
} else if (state == BluetoothConnectionState.disconnected) { } else if (state == BluetoothConnectionState.disconnected) {
dismissEasyLoading(); dismissEasyLoading();
@ -395,19 +395,104 @@ class NearbyLockLogic extends BaseGetXController {
void startScanBlueList() { void startScanBlueList() {
BlueManage().startScan(2000, DeviceType.blue, (List<ScanResult> list) { BlueManage().startScan(2000, DeviceType.blue, (List<ScanResult> list) {
state.devices.clear(); state.devices.clear();
for (int i = 0; i < list.length; i++) {
final ScanResult device = list[i]; for (final device in list) {
if ((device.advertisementData.serviceUuids.isNotEmpty final String? serviceUuid =
? device.advertisementData.serviceUuids[0] device.advertisementData.serviceUuids.isNotEmpty
: '') ? device.advertisementData.serviceUuids[0].toString()
.toString()[31] != : null;
'1') {
state.devices.add(list[i]); if (serviceUuid != null && !isPaired(serviceUuid)) {
state.devices.add(device);
} }
} }
}); });
} }
/// 128-bit 32-bit
bool isPaired(String serviceUuid) {
if (serviceUuid.length < 6) return false; // 6
try {
if (serviceUuid.length >= 32) {
// 128-bit UUID 31 32
String status = serviceUuid.substring(30, 32);
return status == '01'; // '01'
} else if (serviceUuid.length >= 5) {
// 32-bit UUID 4 5
String status = serviceUuid.substring(4, 6);
return status == '01'; // '01'
}
return false; // false
} catch (e) {
return false; // false
}
}
/// 128-bit 32-bit
bool isSleeping(String serviceUuid) {
if (serviceUuid.length < 8) return false; // 8
try {
if (serviceUuid.length >= 34) {
// 128-bit UUID 33 34
String status = serviceUuid.substring(32, 34);
return status == '00'; // '00'
} else if (serviceUuid.length >= 7) {
// 32-bit UUID 6 7
String status = serviceUuid.substring(6, 8);
return status == '00'; // '00'
}
return false; // false
} catch (e) {
return false; // false
}
}
/// 128-bit UUID
bool isPaired128Bit(String serviceUuid) {
if (serviceUuid.length != 36) return false; // 128-bit UUID
try {
String status = serviceUuid.substring(30, 32); // 31 32
return status == '01'; // '01'
} catch (e) {
return false; // false
}
}
/// 128-bit UUID
bool isSleeping128Bit(String serviceUuid) {
if (serviceUuid.length != 36) return false; // 128-bit UUID
try {
String status = serviceUuid.substring(32, 34); // 33 34
return status == '00'; // '00'
} catch (e) {
return false; // false
}
}
/// 32-bit UUID
bool isPaired32Bit(String serviceUuid) {
if (serviceUuid.length != 8) return false; // 32-bit UUID
try {
String status = serviceUuid.substring(3, 5); // 4 5
return status == '01'; // '01'
} catch (e) {
return false; // false
}
}
/// 32-bit UUID
bool isSleeping32Bit(String serviceUuid) {
if (serviceUuid.length != 8) return false; // 32-bit UUID
try {
String status = serviceUuid.substring(5, 7); // 6 7
return status == '00'; // '00'
} catch (e) {
return false; // false
}
}
void stopScanBlueList() { void stopScanBlueList() {
BlueManage().disconnect(); BlueManage().disconnect();
BlueManage().stopScan(); BlueManage().stopScan();

View File

@ -106,13 +106,18 @@ class _NearbyLockPageState extends State<NearbyLockPage> with RouteAware {
Widget nearbyLockItem( Widget nearbyLockItem(
String lockTypeIcon, ScanResult scanResult, Function() action) { String lockTypeIcon, ScanResult scanResult, Function() action) {
return GestureDetector( return GestureDetector(
onTap: ((scanResult.advertisementData.serviceUuids.isNotEmpty onTap: () {
? scanResult.advertisementData.serviceUuids[0] final String? serviceUuid =
: '') scanResult.advertisementData.serviceUuids.isNotEmpty
.toString()[33] == ? scanResult.advertisementData.serviceUuids[0].toString()
'1') : null;
? action
: null, // serviceUuid 34 '1'
final isPaired = !logic.isPaired(serviceUuid!);
if (serviceUuid != null && isPaired) {
action();
}
},
child: Column( child: Column(
children: <Widget>[ children: <Widget>[
Container( Container(
@ -136,15 +141,12 @@ class _NearbyLockPageState extends State<NearbyLockPage> with RouteAware {
Text(scanResult.advertisementData.advName, Text(scanResult.advertisementData.advName,
style: TextStyle( style: TextStyle(
fontSize: 20.sp, fontSize: 20.sp,
color: ((scanResult.advertisementData.serviceUuids color: logic.isSleeping(
.isNotEmpty scanResult.advertisementData.serviceUuids[0]
? scanResult.advertisementData .toString(),
.serviceUuids[0] )
: '') ? Colors.grey
.toString()[33] == : AppColors.blackColor)),
'1')
? AppColors.blackColor
: Colors.grey)),
], ],
), ),
SizedBox( SizedBox(