From 58677246b3500430c269570bae99c23ab520b34f Mon Sep 17 00:00:00 2001 From: Foti Dim Date: Thu, 13 Jun 2024 22:35:54 +0200 Subject: [PATCH] Improve queue naming --- lib/src/ble_command_queue.dart | 8 ++++---- lib/src/universal_ble.dart | 32 ++++++++++++++++---------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/src/ble_command_queue.dart b/lib/src/ble_command_queue.dart index bd7fdae..4bd3861 100644 --- a/lib/src/ble_command_queue.dart +++ b/lib/src/ble_command_queue.dart @@ -1,7 +1,7 @@ import 'package:universal_ble/src/queue.dart'; import 'package:universal_ble/universal_ble.dart'; -/// Execute commands in queue and manage queue per device +/// Queue commands and manage queue per device class BleCommandQueue { QueueType queueType = QueueType.global; Duration? timeout = const Duration(seconds: 10); @@ -15,15 +15,13 @@ class BleCommandQueue { }; } - Future executeCommand( + Future queueCommand( Future Function() command, { bool withTimeout = true, String? deviceId, }) { Duration? duration = withTimeout ? timeout : null; switch (queueType) { - case QueueType.none: - return duration != null ? command().timeout(duration) : command(); case QueueType.global: return _globalQueue.add(command, timeout: duration); case QueueType.perDevice: @@ -33,6 +31,8 @@ class BleCommandQueue { } else { return _globalQueue.add(command, timeout: duration); } + case QueueType.none: + return duration != null ? command().timeout(duration) : command(); } } diff --git a/lib/src/universal_ble.dart b/lib/src/universal_ble.dart index 590cfda..74d2056 100644 --- a/lib/src/universal_ble.dart +++ b/lib/src/universal_ble.dart @@ -37,7 +37,7 @@ class UniversalBle { /// Get Bluetooth availability state /// To be notified of updates, set [onAvailabilityChange] listener static Future getBluetoothAvailabilityState() async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.getBluetoothAvailabilityState(), ); } @@ -49,7 +49,7 @@ class UniversalBle { static Future startScan({ ScanFilter? scanFilter, }) async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.startScan(scanFilter: scanFilter), withTimeout: false, ); @@ -59,7 +59,7 @@ class UniversalBle { /// Set [onScanResult] listener to `null` if you don't need it anymore /// It might throw errors if Bluetooth is not available static Future stopScan() async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.stopScan(), withTimeout: false, ); @@ -74,7 +74,7 @@ class UniversalBle { String deviceId, { Duration? connectionTimeout, }) async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.connect(deviceId, connectionTimeout: connectionTimeout), deviceId: deviceId, ); @@ -83,7 +83,7 @@ class UniversalBle { /// Disconnect from a device. /// Get notified of connection state changes in [onConnectionChanged] listener static Future disconnect(String deviceId) async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.disconnect(deviceId), deviceId: deviceId, ); @@ -91,7 +91,7 @@ class UniversalBle { /// Discover services of a device static Future> discoverServices(String deviceId) async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.discoverServices(deviceId), deviceId: deviceId, ); @@ -107,7 +107,7 @@ class UniversalBle { String characteristic, BleInputProperty bleInputProperty, ) async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.setNotifiable( deviceId, service, @@ -125,7 +125,7 @@ class UniversalBle { String service, String characteristic, ) async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.readValue(deviceId, service, characteristic), deviceId: deviceId, ); @@ -140,7 +140,7 @@ class UniversalBle { Uint8List value, BleOutputProperty bleOutputProperty, ) async { - await _bleCommandQueue.executeCommand( + await _bleCommandQueue.queueCommand( () => _platform.writeValue( deviceId, service, @@ -155,7 +155,7 @@ class UniversalBle { /// Request MTU value /// `requestMtu` is not supported on `Linux` and `Web static Future requestMtu(String deviceId, int expectedMtu) async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.requestMtu(deviceId, expectedMtu), deviceId: deviceId, ); @@ -165,7 +165,7 @@ class UniversalBle { /// Returns null on `Apple` and `Web` static Future isPaired(String deviceId) async { if (kIsWeb || Platform.isIOS || Platform.isMacOS) return null; - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.isPaired(deviceId), deviceId: deviceId, ); @@ -174,7 +174,7 @@ class UniversalBle { /// Trigger pair request /// It might throw an error if device is already paired static Future pair(String deviceId) async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.pair(deviceId), deviceId: deviceId, ); @@ -183,7 +183,7 @@ class UniversalBle { /// Unpair a device /// It might throw an error if device is not paired static Future unPair(String deviceId) async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.unPair(deviceId), deviceId: deviceId, ); @@ -197,14 +197,14 @@ class UniversalBle { static Future> getSystemDevices({ List? withServices, }) async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.getSystemDevices(withServices), ); } /// Returns true if device is connected to the app static Future isConnected(String deviceId) async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.isConnected(deviceId), ); } @@ -213,7 +213,7 @@ class UniversalBle { /// It might throw errors if Bluetooth is not available /// Not supported on `Web` and `Apple` static Future enableBluetooth() async { - return await _bleCommandQueue.executeCommand( + return await _bleCommandQueue.queueCommand( () => _platform.enableBluetooth(), ); }