dotintent / FlutterBleLib

Bluetooth Low Energy library for Flutter with support for simulating peripherals
Apache License 2.0
535 stars 197 forks source link

Stopscan called twice - preventing startperipheralscan from returning any result #525

Open xelons opened 3 years ago

xelons commented 3 years ago

Hi, Using the example provided, when the devices list is refreshed, it seems like the stopscan function is called twice, making the startperipheralscan not return any result 50% of the time.

Verbose log is :


2020-09-17 02:03:16.767845-0500 Runner[33751:11817821] Metal API Validation Enabled
2020-09-17 02:03:16.902531-0500 Runner[33751:11818033] flutter: Observatory listening on http://127.0.0.1:53708/kThvAbeFOis=/
2020-09-17 02:03:17.460105-0500 Runner[33751:11818022] flutter: 2020-09-17T02:03:17.458277  D   DeviceListScreenState.didChangeDependencies:     DeviceListScreenState didChangeDependencies
2020-09-17 02:03:17.464507-0500 Runner[33751:11818022] flutter: 2020-09-17T02:03:17.464484  D   DeviceListScreenState._onResume:     onResume
2020-09-17 02:03:17.468754-0500 Runner[33751:11818022] flutter: 2020-09-17T02:03:17.468734  D   DevicesBloc.init:    Init devices bloc
2020-09-17 02:03:17.481399-0500 Runner[33751:11818022] flutter: 2020-09-17T02:03:17.481321  D   DevicesBloc.init:     listen to _devicePickerController.stream
2020-09-17 02:03:17.491402-0500 Runner[33751:11818022] flutter: 2020-09-17T02:03:17.491326  D   DeviceListScreenState.build:     build DeviceListScreenState
2020-09-17 02:03:17.690304-0500 Runner[33751:11818022] flutter: set log level to verbose
2020-09-17 02:03:17.700219-0500 Runner[33751:11818022] flutter: 2020-09-17T02:03:17.700115  D   DevicesBloc._startScan:  Ble client created
[RxBLEKit|DEBG|07:03:17.703]: CentralManager(10769084560) scanForPeripherals(
withServices: Optional("[0EF881BC--RETRACTED]"),
options: Optional([:]))
[RxBLEKit|VERB|07:03:17.746]: CentralManager(10769084560) didDiscover(peripheral: Peripheral(uuid: 57FD66C2-RETRACTED, name: Optional("BLEDEVICE")),
rssi: -61)
2020-09-17 02:03:17.781052-0500 Runner[33751:11818022] flutter: 2020-09-17T02:03:17.780945  D   DevicesBloc._startScan.<ac>:     discovered device, checking advertisement data
2020-09-17 02:03:17.781412-0500 Runner[33751:11818022] flutter: 2020-09-17T02:03:17.781392  D   DevicesBloc._startScan.<ac>:     found new device BLEDEVICE 57FD66C2-RETRACTED
2020-09-17 02:03:17.787176-0500 Runner[33751:11818022] flutter: 2020-09-17T02:03:17.787072  D   new DevicesList.<ac>:    Build row for 0
**2020-09-17 02:03:22.434238-0500 Runner[33751:11818022] flutter: 2020-09-17T02:03:22.434027    D   DevicesBloc.refresh:     refresh started
[RxBLEKit|DEBG|07:03:22.437]: CentralManager(10769084560) stopScan()
2020-09-17 02:03:22.449692-0500 Runner[33751:11818022] flutter: 2020-09-17T02:03:22.449599  D   DevicesBloc._startScan:  Ble client created
[RxBLEKit|DEBG|07:03:22.450]: CentralManager(10769084560) scanForPeripherals(
withServices: Optional("[0EF881BC-RETRACTED]"),
options: Optional([:]))
[RxBLEKit|DEBG|07:03:22.452]: CentralManager(10769084560) stopScan()**```

    void _startScan() {
    Fimber.d("Ble client created");
    _scanSubscription =
        _bleManager.startPeripheralScan(scanMode: ScanMode.balanced, uuids: [DeviceUuids.Service]).listen((ScanResult scanResult) {
      var bleDevice = BleDevice(scanResult);
      Fimber.d("discovered device, checking advertisement data");
      if (scanResult.advertisementData.localName != null &&
          !bleDevices.contains(bleDevice)) {
        Fimber.d(
            'found new device ${scanResult.advertisementData.localName} ${scanResult.peripheral.identifier}');
        bleDevices.add(bleDevice);
        _visibleDevicesController.add(bleDevices.sublist(0));
      }
    });
    }

    Future<void> refresh() async {
     Fimber.d("refresh started");
    _scanSubscription.cancel();
    _timeoutTimer?.cancel();
    await _bleManager.stopPeripheralScan();
    bleDevices.clear();
    _visibleDevicesController.add(bleDevices.sublist(0));
    await _checkPermissions()
        .then((_) => _startScan())
        .catchError((e) => Fimber.d("Couldn't refresh", ex: e));
    }
    }
xelons commented 3 years ago

Update: Even if I comment out the stopperipheralscan, it seems stop scan is still called right after startperipheralscan.

[RxBLEKit|DEBG|08:59:59.685]: CentralManager(10783802240) scanForPeripherals(
withServices: Optional("[0EF881BC-RETRACTED]"),
options: Optional([:]))
[RxBLEKit|DEBG|08:59:59.712]: CentralManager(10783802240) stopScan()
xelons commented 3 years ago

Commenting out _scanSubscription.cancel(); solves this issue. Canceling the subscription stops the scan, and sometimes the stop comes after the scan was initiated, even when using await.