From 7f0087d1531888a6f5670eef59ff4706ad24394a Mon Sep 17 00:00:00 2001 From: anfe <448468458@qq.com> Date: Thu, 6 Jun 2024 11:28:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=90=9C=E7=B4=A2=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E5=88=A4=E6=96=AD=E8=AE=BE=E5=A4=87=E8=A2=AB=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E7=9A=84=E6=97=B6=E5=80=99=E9=87=8D=E7=BD=AE=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E7=BC=93=E5=AD=98=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/blue/blue_manage.dart | 40 +++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/blue/blue_manage.dart b/lib/blue/blue_manage.dart index 30e39212..6a26d88e 100755 --- a/lib/blue/blue_manage.dart +++ b/lib/blue/blue_manage.dart @@ -325,7 +325,8 @@ class BlueManage { mac != null) { scanSingleTimer?.cancel(); //兼容android 的低配手机 - await doNotSearchBLE(mac, connectStateCallBack); + await doNotSearchBLE(mac, connectStateCallBack, + isAddEquipment: isAddEquipment); scanSingleTimer = Timer(3.seconds, () { scanSingleTimer?.cancel(); startScanSingle(deviceName, 15, (List scanDevices) => null); @@ -384,6 +385,7 @@ class BlueManage { isAddEquipment == false) { connectStateCallBack(BluetoothConnectionState.disconnected); EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds); + scanDevices.clear(); return; } //尝试连接设备 @@ -392,7 +394,8 @@ class BlueManage { //直接给蓝牙设备写入 Future doNotSearchBLE( - String masAdds, ConnectStateCallBack connectStateCallBack) async { + String masAdds, ConnectStateCallBack connectStateCallBack, + {bool isAddEquipment = false}) async { await FlutterBluePlus.stopScan(); if (bluetoothConnectDevice == null || bluetoothConnectDevice?.remoteId.str != masAdds) { @@ -401,12 +404,14 @@ class BlueManage { _initListenConnectionState(); } //尝试连接设备 - await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack); + await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack, + isAddEquipment: isAddEquipment); } //设备连接 Future bluetoothDeviceConnect(BluetoothDevice bluetoothConnectDevice, - ConnectStateCallBack connectStateCallBack) async { + ConnectStateCallBack connectStateCallBack, + {bool isAddEquipment = false}) async { // 重连三次 const int maxAttempts = 3; int attempt = 0; @@ -430,23 +435,22 @@ class BlueManage { if (bluetoothConnectionState == BluetoothConnectionState.connected) { try { - bluetoothConnectDevice! - .discoverServices() - .then((List services) { - for (final BluetoothService service in services) { - if (service.uuid == _serviceIdConnect) { - for (final BluetoothCharacteristic characteristic - in service.characteristics) { - if (characteristic.characteristicUuid == - _characteristicIdSubscription) { - _subScribeToCharacteristic(characteristic); - bluetoothConnectionState = BluetoothConnectionState.connected; - connectStateCallBack(bluetoothConnectionState!); - } + final List services = + await bluetoothConnectDevice.discoverServices(); + //循环判断服务 + for (final BluetoothService service in services) { + if (service.uuid == _serviceIdConnect) { + for (final BluetoothCharacteristic characteristic + in service.characteristics) { + if (characteristic.characteristicUuid == + _characteristicIdSubscription) { + _subScribeToCharacteristic(characteristic); + bluetoothConnectionState = BluetoothConnectionState.connected; + connectStateCallBack(bluetoothConnectionState!); } } } - }); + } } on Exception catch (e) { bluetoothConnectionState = BluetoothConnectionState.disconnected; connectStateCallBack(bluetoothConnectionState!);