Skip to content

Commit

Permalink
Add timeout limit while searching for device.
Browse files Browse the repository at this point in the history
  • Loading branch information
featherJ committed Apr 11, 2023
1 parent 9c4cedf commit 60e3eb2
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 字段中增加如下依赖
Expand All @@ -25,7 +25,7 @@ dependencies:
ble_ex:
git:
url: https://github.com/featherJ/ble_ex.git
ref: ^1.0.0
ref: ^1.0.1
```
然后执行命令
```
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
11 changes: 7 additions & 4 deletions example/lib/samples/cases/base_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0"
version: "1.0.1"
boolean_selector:
dependency: transitive
description:
Expand Down
5 changes: 3 additions & 2 deletions lib/src/ble_ex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ class BleEx extends Object {
}

/// 搜索指定设备
Future<DiscoveredDevice> searchForDevice(List<DevicesFilter> filters) async {
Future<DiscoveredDevice?> searchForDevice(List<DevicesFilter> filters,
{Duration timeout = const Duration(milliseconds: 5000)}) async {
BleSearchingTask task = BleSearchingTask._(_scannerHelper);
return task.searchForDevice(filters);
return task.searchForDevice(filters, timeout);
}

/// 创建一个外围设备
Expand Down
8 changes: 6 additions & 2 deletions lib/src/scan/commons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ class _BleScannerHelper {
scanMode: ScanMode.lowLatency,
).listen(
(device) {
for (var deviceUpdateCallback in deviceUpdateCallbacks) {
deviceUpdateCallback(device);
List<ScanningListener> tempCallbacks = [];
for (var func in deviceUpdateCallbacks) {
tempCallbacks.add(func);
}
for (var callback in tempCallbacks) {
callback(device);
}
},
onError: (error) {
Expand Down
16 changes: 14 additions & 2 deletions lib/src/scan/searching_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ class BleSearchingTask {

bool _disposed = false;
bool get disposed => _disposed;
Completer<DiscoveredDevice> _completer = Completer();
Completer<DiscoveredDevice?> _completer = Completer();
Timer? _searchTimer;

/// 搜索指定设备
Future<DiscoveredDevice> searchForDevice(List<DevicesFilter> filters) async {
Future<DiscoveredDevice?> searchForDevice(
List<DevicesFilter> filters, Duration timeout) async {
if (_disposed) {
throw Exception("Searching task can not start after disposed");
}
Expand All @@ -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;
}

Expand All @@ -37,6 +47,8 @@ class BleSearchingTask {

/// 停止扫描设备
void stopSearching() {
_searchTimer?.cancel();
_searchTimer = null;
_scannerHelper?.removeDeviceUpdateListener(_deviceUpdateHandler);
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 60e3eb2

Please sign in to comment.