feat:增加兼容32bit广播信号
This commit is contained in:
parent
0d77d3aff4
commit
dc37b42977
@ -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) {
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
@ -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();
|
||||||
|
|||||||
@ -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(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user