From 7678b8989ce18a55ad2c5766e7da5040ecee01c7 Mon Sep 17 00:00:00 2001 From: anfe <448468458@qq.com> Date: Wed, 29 May 2024 15:29:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=20android=20=E8=93=9D?= =?UTF-8?q?=E7=89=99=E4=B8=8D=E7=A8=B3=E5=AE=9A=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/blue/blue_manage.dart | 70 ++++++++++++--------------------------- 1 file changed, 22 insertions(+), 48 deletions(-) diff --git a/lib/blue/blue_manage.dart b/lib/blue/blue_manage.dart index dcbced19..30e39212 100755 --- a/lib/blue/blue_manage.dart +++ b/lib/blue/blue_manage.dart @@ -110,7 +110,7 @@ class BlueManage { _connectionStateSubscription = bluetoothConnectDevice!.connectionState .listen((BluetoothConnectionState state) async { bluetoothConnectionState = state; - // AppLog.log("蓝牙连接状态:$state"); + AppLog.log('蓝牙连接回调状态:$state'); }); } @@ -386,13 +386,33 @@ class BlueManage { EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds); return; } + //尝试连接设备 + await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack); + } + //直接给蓝牙设备写入 + Future doNotSearchBLE( + String masAdds, ConnectStateCallBack connectStateCallBack) async { + await FlutterBluePlus.stopScan(); + if (bluetoothConnectDevice == null || + bluetoothConnectDevice?.remoteId.str != masAdds) { + bluetoothConnectDevice = BluetoothDevice.fromId(masAdds); + _initGetMtuSubscription(); + _initListenConnectionState(); + } + //尝试连接设备 + await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack); + } + + //设备连接 + Future bluetoothDeviceConnect(BluetoothDevice bluetoothConnectDevice, + ConnectStateCallBack connectStateCallBack) async { // 重连三次 const int maxAttempts = 3; int attempt = 0; while (attempt < maxAttempts) { try { - await bluetoothConnectDevice!.connect(timeout: 5.seconds); + await bluetoothConnectDevice.connect(timeout: 5.seconds); break; // If the connection is successful, break the loop } catch (e) { AppLog.log('连接失败 重连了: $e'); @@ -437,52 +457,6 @@ class BlueManage { } } - //直接给蓝牙设备写入 - Future doNotSearchBLE( - String masAdds, ConnectStateCallBack connectStateCallBack) async { - await FlutterBluePlus.stopScan(); - final BluetoothDevice device = BluetoothDevice.fromId(masAdds); - // 重连三次 - const int maxAttempts = 3; - bool connected = false; - int attempt = 0; - while (attempt < maxAttempts && !connected) { - try { - await device.connect(); - connected = true; - } catch (e, s) { - AppLog.log('连接失败 重连了: ${e.toString()} ${s.toString()}'); - attempt++; // Increase the attempt count - if (attempt < maxAttempts) { - AppLog.log('重新尝试连接...'); - } - } - } - - if (!connected) { - connectStateCallBack(BluetoothConnectionState.disconnected); - return; - } - - bluetoothConnectDevice = device; - bluetoothConnectionState = BluetoothConnectionState.connected; - 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); - } - } - } - } - connectStateCallBack(BluetoothConnectionState.connected); - // writeCharacteristicWithResponse(value); - } - Future _subScribeToCharacteristic( BluetoothCharacteristic characteristic) async { final StreamSubscription> subscription =