From 60e3eb287945ab7bb11ec5ab855e16820146b158 Mon Sep 17 00:00:00 2001 From: "Agua.L" <438517073@qq.com> Date: Tue, 11 Apr 2023 18:05:33 +0800 Subject: [PATCH] Add timeout limit while searching for device. --- CHANGELOG.md | 3 +++ README.md | 4 ++-- example/lib/main.dart | 2 +- example/lib/samples/cases/base_case.dart | 11 +++++++---- example/pubspec.lock | 2 +- lib/src/ble_ex.dart | 5 +++-- lib/src/scan/commons.dart | 8 ++++++-- lib/src/scan/searching_task.dart | 16 ++++++++++++++-- pubspec.yaml | 2 +- 9 files changed, 38 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49b01ff..f27fb71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 1.0.1 +* Add timeout limit while searching for device. + ## 1.0.0 * Add multi task support for device scanning and searching. diff --git a/README.md b/README.md index 73bf689..e9bc1bb 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ 你可以通过命令 `flutter pub add ble_ex` 直接安装 `ble_ex` 插件,这将自动为你项目内的 `pubspec.yaml` 文件的 `dependencies` 字段中增加如下依赖 ```yaml dependencies: - ble_ex: ^1.0.0 + ble_ex: ^1.0.1 ``` #### 从 github 安装 需要你手动在 `pubspec.yaml` 文件的 `dependencies` 字段中增加如下依赖 @@ -25,7 +25,7 @@ dependencies: ble_ex: git: url: https://github.com/featherJ/ble_ex.git - ref: ^1.0.0 + ref: ^1.0.1 ``` 然后执行命令 ``` diff --git a/example/lib/main.dart b/example/lib/main.dart index 4c9ebd2..971375c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -31,7 +31,7 @@ void runSampleCase(BleEx bleex) { // CaseBase sampleCase = BleexCommunicationCase(); // CaseBase sampleCase = BleexRequestHighFrequencyCase(); // CaseBase sampleCase = ConnectByDistCase(); - CaseBase sampleCase = ScanCase(); + CaseBase sampleCase = CaseBase(); sampleCase.init(bleex); bleLog(tag, "Sample case created"); diff --git a/example/lib/samples/cases/base_case.dart b/example/lib/samples/cases/base_case.dart index 91983a5..7e307ab 100644 --- a/example/lib/samples/cases/base_case.dart +++ b/example/lib/samples/cases/base_case.dart @@ -15,10 +15,13 @@ class CaseBase { ServiceSampleFilter(BleUUIDs.service1).filter, ManufacturerSampleFilter(Constants.serviceManufacturerTag).filter ]); - - bleLog(tag, 'Find device: ' + device.toString()); - peripheral = createPeripheral(device); - peripheral.connect(); + if (device != null) { + bleLog(tag, 'Find device: ' + device.toString()); + peripheral = createPeripheral(device); + peripheral.connect(); + } else { + bleLog(tag, 'Find device timeout'); + } } BlePeripheral createPeripheral(DiscoveredDevice device) { diff --git a/example/pubspec.lock b/example/pubspec.lock index e085a86..58cd6ff 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -15,7 +15,7 @@ packages: path: ".." relative: true source: path - version: "1.0.0" + version: "1.0.1" boolean_selector: dependency: transitive description: diff --git a/lib/src/ble_ex.dart b/lib/src/ble_ex.dart index 25101b9..d2326ce 100644 --- a/lib/src/ble_ex.dart +++ b/lib/src/ble_ex.dart @@ -166,9 +166,10 @@ class BleEx extends Object { } /// 搜索指定设备 - Future searchForDevice(List filters) async { + Future searchForDevice(List filters, + {Duration timeout = const Duration(milliseconds: 5000)}) async { BleSearchingTask task = BleSearchingTask._(_scannerHelper); - return task.searchForDevice(filters); + return task.searchForDevice(filters, timeout); } /// 创建一个外围设备 diff --git a/lib/src/scan/commons.dart b/lib/src/scan/commons.dart index 774ee53..f65c098 100644 --- a/lib/src/scan/commons.dart +++ b/lib/src/scan/commons.dart @@ -32,8 +32,12 @@ class _BleScannerHelper { scanMode: ScanMode.lowLatency, ).listen( (device) { - for (var deviceUpdateCallback in deviceUpdateCallbacks) { - deviceUpdateCallback(device); + List tempCallbacks = []; + for (var func in deviceUpdateCallbacks) { + tempCallbacks.add(func); + } + for (var callback in tempCallbacks) { + callback(device); } }, onError: (error) { diff --git a/lib/src/scan/searching_task.dart b/lib/src/scan/searching_task.dart index 38e0d06..fb84154 100644 --- a/lib/src/scan/searching_task.dart +++ b/lib/src/scan/searching_task.dart @@ -8,10 +8,12 @@ class BleSearchingTask { bool _disposed = false; bool get disposed => _disposed; - Completer _completer = Completer(); + Completer _completer = Completer(); + Timer? _searchTimer; /// 搜索指定设备 - Future searchForDevice(List filters) async { + Future searchForDevice( + List filters, Duration timeout) async { if (_disposed) { throw Exception("Searching task can not start after disposed"); } @@ -21,6 +23,14 @@ class BleSearchingTask { } _completer = Completer(); _scannerHelper!.addDeviceUpdateListener(_deviceUpdateHandler); + _searchTimer = Timer(timeout, () { + _searchTimer?.cancel(); + _searchTimer = null; + if (!_completer.isCompleted) { + _completer.complete(null); + dispose(); + } + }); return _completer.future; } @@ -37,6 +47,8 @@ class BleSearchingTask { /// 停止扫描设备 void stopSearching() { + _searchTimer?.cancel(); + _searchTimer = null; _scannerHelper?.removeDeviceUpdateListener(_deviceUpdateHandler); } diff --git a/pubspec.yaml b/pubspec.yaml index 944ab8f..e013185 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: ble_ex description: A Flutter library based on flutter_reactive_ble. Added more operations for BLE communication. -version: 1.0.0 +version: 1.0.1 homepage: https://github.com/featherJ/ble_ex environment: