diff --git a/CHANGELOG.md b/CHANGELOG.md index 013415f..af7739e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.21.0 * BREAKING CHANGE: `connectionTimeout` argument from `connect`, `isPaired` and `pair` API renamed to `timeout` * Add `timeout` argument to all APIs +* Add `clearQueue()` API ## 0.20.2 * Fix `BleCharacteristic.onValueReceived` diff --git a/README.md b/README.md index ebb7cb4..32443e4 100644 --- a/README.md +++ b/README.md @@ -420,6 +420,14 @@ UniversalBle.onQueueUpdate = (String id, int remainingItems) { }; ``` +To clear the queue: +```dart + /// Use [BleCommandQueue.globalQueueId] to clear the global queue. + /// To clear the queue of a specific device, use `deviceId` as [id]. + /// If no [id] is provided, all queues will be cleared. + UniversalBle.clearQueue(BleCommandQueue.globalQueueId); +``` + ## Timeout By default, all commands have a global timeout of 10 seconds. diff --git a/lib/src/universal_ble.dart b/lib/src/universal_ble.dart index 944f8bf..7825e7d 100644 --- a/lib/src/universal_ble.dart +++ b/lib/src/universal_ble.dart @@ -414,6 +414,12 @@ class UniversalBle { ); } + /// Clear a queue. + /// Use [BleCommandQueue.globalQueueId] to clear the global queue. + /// To clear the queue of a specific device, use `deviceId` as [id]. + /// If no [id] is provided, all queues will be cleared. + static void clearQueue([String? id]) => _bleCommandQueue.clearQueue(id); + /// [receivesAdvertisements] returns true on web if the browser supports receiving advertisements from a certain `deviceId`. /// The rest of the platforms will always return true. /// If true, then you will be getting scanResult updates for this device. diff --git a/lib/src/utils/ble_command_queue.dart b/lib/src/utils/ble_command_queue.dart index 2fad9a1..b9bd767 100644 --- a/lib/src/utils/ble_command_queue.dart +++ b/lib/src/utils/ble_command_queue.dart @@ -51,4 +51,14 @@ class BleCommandQueue { _queueMap[id] = queue; return queue; } + + void clearQueue(String? id) { + if (id == null) { + _queueMap.forEach((k, v) => v.dispose()); + _queueMap.clear(); + } else { + _queueMap[id]?.dispose(); + _queueMap.remove(id); + } + } }