Skip to content

Commit

Permalink
Add: Stop current scanning before 'scanDevices'
Browse files Browse the repository at this point in the history
  • Loading branch information
featherJ committed Aug 10, 2022
1 parent 2fa0b30 commit c54baca
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
4 changes: 3 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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");
Expand Down
18 changes: 18 additions & 0 deletions example/lib/samples/cases/scan_case.dart
Original file line number Diff line number Diff line change
@@ -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<void> start() async {
bleManager.listenScanAddDevice(deviceScanHandler);
bleManager.listenScanUpdateDevice(deviceScanHandler);
bleManager.scanDevices();
}

void deviceScanHandler(DiscoveredDevice device) {
bleLog(tag, '$device');
}
}
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.9.2"
version: "0.9.3"
boolean_selector:
dependency: transitive
description:
Expand Down
17 changes: 11 additions & 6 deletions lib/src/ble_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class BleManager extends Object {
Uint8List? _manufacturerFilter;

void scanDevices({Uint8List? manufacturerFilter}) async {
await stopScanDevices();
_doScanDevices(true, manufacturerFilter: manufacturerFilter);
}

Expand Down Expand Up @@ -163,7 +164,7 @@ class BleManager extends Object {
_updateDeviceMap[device.id] = device;
}

void stopScanDevices() {
Future<void> stopScanDevices() async {
fireScanEvent = false;
_targetServiceUuid = null;
_targetManufacturerData = null;
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -289,14 +294,14 @@ class BleManager extends Object {

/// 得到目标蓝牙服务
Future<DiscoveredDevice> scanForDevice(Uuid serviceUuid,
{Uint8List? manufacturerFilter}) {
stopScanDevices();
{Uint8List? manufacturerFilter}) async {
await stopScanDevices();
fireScanEvent = false;
Completer<DiscoveredDevice> _completer = Completer();
_targetServiceUuid = serviceUuid;
_targetManufacturerData = manufacturerFilter;
_findTargetDeviceFunc = (device) {
stopScanDevices();
_findTargetDeviceFunc = (device) async {
await stopScanDevices();
_completer.complete(device);
};
_doScanDevices(false);
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: 0.9.2
version: 0.9.3
homepage: https://github.com/featherJ/ble_ex

environment:
Expand Down

0 comments on commit c54baca

Please sign in to comment.