From 0d2dd5f58500b368efb631e2f5edff673f66d7b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AD=8F=E5=B0=91=E9=98=B3?= <786612630@qq.com> Date: Thu, 1 Aug 2024 11:10:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=93=9D=E7=89=99=E6=8E=A7=E5=88=B6=E7=B1=BB?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=B9=E6=B3=95=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=A6=96=E6=AC=A1=E6=B7=BB=E5=8A=A0=E5=AE=8C=E9=94=81=E4=B9=8B?= =?UTF-8?q?=E5=90=8E=EF=BC=8C=E9=83=A8=E5=88=86=E6=9C=BA=E5=9E=8B=E5=BC=80?= =?UTF-8?q?=E9=94=81=E6=8F=90=E7=A4=BA=E9=94=81=E5=B7=B2=E8=A2=AB=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/blue/blue_manage.dart | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/blue/blue_manage.dart b/lib/blue/blue_manage.dart index 6943a336..0c2409ee 100755 --- a/lib/blue/blue_manage.dart +++ b/lib/blue/blue_manage.dart @@ -355,9 +355,14 @@ class BlueManage { return isExistDevice; } - Future _connectDevice(List devicesList, String deviceName, + Future _connectDevice( + List devicesList, + String deviceName, ConnectStateCallBack connectStateCallBack, - {bool isAddEquipment = false}) async { + { + bool isAddEquipment = false,// 是否是添加设备之前 + bool isReconnect = true,// 是否是重连 + }) async { // 判断数组列表里面是否有这个设备 // AppLog.log("devicesList:$devicesList"); final int knownDeviceIndex = devicesList.indexWhere((ScanResult d) => @@ -385,16 +390,18 @@ class BlueManage { AppLog.log('调用了停止扫描的方法'); await stopScan(); - if ((scanResult.advertisementData.serviceUuids[0].toString()[31] == '0') && - isAddEquipment == false) { - connectStateCallBack(BluetoothConnectionState.disconnected); - EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds); - scanDevices.clear(); - - startScanSingle(deviceName, 15, (List scanDevices) { - _connectDevice(scanDevices, deviceName, connectStateCallBack, - isAddEquipment: isAddEquipment); - }); + if ((scanResult.advertisementData.serviceUuids[0].toString()[31] == '0') && isAddEquipment == false) { + // 添加这个判断是因为有些苹果设备或者安卓等性能比较好的设备时,添加完锁之后,锁板未改变为已添加状态之前,就进行了蓝牙连接,导致添加完锁就失败,这里进行了判断,如果第一次连接失败,就清除缓存重新扫描连接 + if(isReconnect == true){ + AppLog.log('该锁已被重置, 重新发送扫描命令'); + startScanSingle(deviceName, 15, (List scanDevices) { + _connectDevice(scanDevices, deviceName, connectStateCallBack, isAddEquipment: isAddEquipment, isReconnect: false); + }); + }else{ + connectStateCallBack(BluetoothConnectionState.disconnected); + EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds); + scanDevices.clear(); + } return; } //尝试连接设备