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) {
scanDevices.clear();
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
? scanResult.advertisementData.serviceUuids[0]
: '')
.toString()
.contains(getDeviceType(deviceType))) &&
(scanResult.rssi >= -100)) {
if (isMatch && (scanResult.rssi >= -100)) {
// id相同的元素
final int knownDeviceIndex = scanDevices.indexWhere(
(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,
Future<void> blueSendData(
String deviceName, ConnectStateCallBack stateCallBack,
@ -499,8 +522,39 @@ class BlueManage {
}
AppLog.log('调用了停止扫描的方法');
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) {
//
if (isReconnect == true) {

View File

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

View File

@ -51,9 +51,9 @@ class NearbyLockLogic extends BaseGetXController {
});
BlueManage().blueSendData(deviceName,
(BluetoothConnectionState state) async {
// AppLog.log('点击要添加的设备了');
AppLog.log('点击要添加的设备了');
if (state == BluetoothConnectionState.connected) {
// AppLog.log('开始获取公钥');
AppLog.log('开始获取公钥');
IoSenderManage.getPublicKey(lockId: deviceName);
} else if (state == BluetoothConnectionState.disconnected) {
dismissEasyLoading();
@ -395,19 +395,104 @@ class NearbyLockLogic extends BaseGetXController {
void startScanBlueList() {
BlueManage().startScan(2000, DeviceType.blue, (List<ScanResult> list) {
state.devices.clear();
for (int i = 0; i < list.length; i++) {
final ScanResult device = list[i];
if ((device.advertisementData.serviceUuids.isNotEmpty
? device.advertisementData.serviceUuids[0]
: '')
.toString()[31] !=
'1') {
state.devices.add(list[i]);
for (final device in list) {
final String? serviceUuid =
device.advertisementData.serviceUuids.isNotEmpty
? device.advertisementData.serviceUuids[0].toString()
: null;
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() {
BlueManage().disconnect();
BlueManage().stopScan();

View File

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