From f41aceaa0ecdad91ced6febdb068b51f2352f21e Mon Sep 17 00:00:00 2001 From: Foti Dim Date: Wed, 25 Sep 2024 16:42:29 +0200 Subject: [PATCH] Revert "Add default timeout in Connect, Pair and IsPaired apis" This reverts commit b978b81cb12bcfd616ab2673380dba76b5969de3. --- lib/src/universal_ble.dart | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/lib/src/universal_ble.dart b/lib/src/universal_ble.dart index 67337b0..300664e 100644 --- a/lib/src/universal_ble.dart +++ b/lib/src/universal_ble.dart @@ -76,14 +76,12 @@ class UniversalBle { /// Connect to a device. /// It is advised to stop scanning before connecting. /// It might throw errors if device is not connectable. - /// Default connection timeout is 60 sec + /// `connectionTimeout` is supported on Web only. static Future connect( String deviceId, { Duration? connectionTimeout, }) async { - connectionTimeout ??= const Duration(seconds: 60); StreamSubscription? connectionSubscription; - try { Completer completer = Completer(); @@ -106,7 +104,10 @@ class UniversalBle { }, ); - return await completer.future.timeout(connectionTimeout); + if (connectionTimeout != null) { + return await completer.future.timeout(connectionTimeout); + } + return await completer.future; } finally { connectionSubscription?.cancel(); } @@ -210,7 +211,6 @@ class UniversalBle { static Future isPaired( String deviceId, { BleCommand? pairingCommand, - Duration? connectionTimeout, }) async { if (BleCapabilities.hasSystemPairingApi) { return _bleCommandQueue.queueCommand( @@ -218,12 +218,8 @@ class UniversalBle { deviceId: deviceId, ); } else if (pairingCommand != null) { - return _connectAndExecuteBleCommand( - deviceId, - pairingCommand, - connectionTimeout: connectionTimeout, - updateCallbackValue: false, - ); + return _connectAndExecuteBleCommand(deviceId, pairingCommand, + updateCallbackValue: false); } return null; } @@ -240,16 +236,11 @@ class UniversalBle { static Future pair( String deviceId, { BleCommand? pairingCommand, - Duration? connectionTimeout, }) async { if (BleCapabilities.hasSystemPairingApi) { return _platform.pair(deviceId); } - return _connectAndExecuteBleCommand( - deviceId, - pairingCommand, - connectionTimeout: connectionTimeout, - ); + return _connectAndExecuteBleCommand(deviceId, pairingCommand); } /// Unpair a device. @@ -315,20 +306,12 @@ class UniversalBle { static Future _connectAndExecuteBleCommand( String deviceId, BleCommand? bleCommand, { - Duration? connectionTimeout, bool updateCallbackValue = false, }) async { try { - bool isConnected = - await getConnectionState(deviceId) != BleConnectionState.connected; - - if (!isConnected) { - isConnected = await connect( - deviceId, - connectionTimeout: connectionTimeout, - ); + if (await getConnectionState(deviceId) != BleConnectionState.connected) { + await connect(deviceId); } - if (!isConnected) return null; List services = await discoverServices(deviceId);