diff --git a/example/lib/main.dart b/example/lib/main.dart index b7b8677..fd0d563 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -5,6 +5,7 @@ import 'package:ble_ex_example/samples/cases/bleex_communication_case.dart'; import 'package:ble_ex_example/samples/cases/bleex_reqeust_case.dart'; import 'package:ble_ex_example/samples/cases/connect_by_dist_case.dart'; import 'package:ble_ex_example/samples/cases/reconnect_case.dart'; +import 'package:ble_ex_example/samples/cases/scan_case.dart'; import 'package:ble_ex_example/samples/cases/verify_central_case.dart'; import 'package:flutter/material.dart'; @@ -20,11 +21,12 @@ void main() async { void runSampleCase(BleManager bleManager) { bleLog(tag, "Creating sample case"); // CaseBase sampleCase = VerifyCentralCase(); - CaseBase sampleCase = ReconnectCase(); + // CaseBase sampleCase = ReconnectCase(); // CaseBase sampleCase = BleCommunicationCase(); // CaseBase sampleCase = BleexRequestCase(); // CaseBase sampleCase = BleexCommunicationCase(); // CaseBase sampleCase = ConnectByDistCase(); + CaseBase sampleCase = ScanCase(); sampleCase.init(bleManager); bleLog(tag, "Sample case created"); diff --git a/example/lib/samples/cases/scan_case.dart b/example/lib/samples/cases/scan_case.dart new file mode 100644 index 0000000..d4b7d44 --- /dev/null +++ b/example/lib/samples/cases/scan_case.dart @@ -0,0 +1,18 @@ +import 'package:ble_ex/ble_ex.dart'; +import 'package:ble_ex_example/samples/cases/base_case.dart'; +import 'package:flutter_reactive_ble/flutter_reactive_ble.dart'; + +class ScanCase extends CaseBase { + static const String tag = "ScanCase"; + + @override + Future start() async { + bleManager.listenScanAddDevice(deviceScanHandler); + bleManager.listenScanUpdateDevice(deviceScanHandler); + bleManager.scanDevices(); + } + + void deviceScanHandler(DiscoveredDevice device) { + bleLog(tag, '$device'); + } +} diff --git a/example/pubspec.lock b/example/pubspec.lock index e317038..f040a5a 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -14,7 +14,7 @@ packages: path: ".." relative: true source: path - version: "0.9.2" + version: "0.9.3" boolean_selector: dependency: transitive description: diff --git a/lib/src/ble_manager.dart b/lib/src/ble_manager.dart index e26f106..a42d92f 100644 --- a/lib/src/ble_manager.dart +++ b/lib/src/ble_manager.dart @@ -68,6 +68,7 @@ class BleManager extends Object { Uint8List? _manufacturerFilter; void scanDevices({Uint8List? manufacturerFilter}) async { + await stopScanDevices(); _doScanDevices(true, manufacturerFilter: manufacturerFilter); } @@ -163,7 +164,7 @@ class BleManager extends Object { _updateDeviceMap[device.id] = device; } - void stopScanDevices() { + Future stopScanDevices() async { fireScanEvent = false; _targetServiceUuid = null; _targetManufacturerData = null; @@ -177,14 +178,18 @@ class BleManager extends Object { if (_statusIniter != null) { _statusIniter!.cancel(); + _statusIniter = null; } _statusIniter = null; if (_scanTimer != null) { _scanTimer!.cancel(); + _scanTimer = null; } _scanTimer = null; if (_subscription != null) { - _subscription!.cancel(); + await _subscription!.cancel(); + _subscription = null; + await Future.delayed(const Duration(milliseconds: 200)); } } @@ -289,14 +294,14 @@ class BleManager extends Object { /// 得到目标蓝牙服务 Future scanForDevice(Uuid serviceUuid, - {Uint8List? manufacturerFilter}) { - stopScanDevices(); + {Uint8List? manufacturerFilter}) async { + await stopScanDevices(); fireScanEvent = false; Completer _completer = Completer(); _targetServiceUuid = serviceUuid; _targetManufacturerData = manufacturerFilter; - _findTargetDeviceFunc = (device) { - stopScanDevices(); + _findTargetDeviceFunc = (device) async { + await stopScanDevices(); _completer.complete(device); }; _doScanDevices(false); diff --git a/pubspec.yaml b/pubspec.yaml index c6499ff..f5552c0 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: 0.9.2 +version: 0.9.3 homepage: https://github.com/featherJ/ble_ex environment: