dotintent / FlutterBleLib

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

startPeripheralScan not working as expected #601

Open dajacmatt opened 3 years ago

dajacmatt commented 3 years ago

Hi,

I'm not totally sure if this is a ble_lib issue or a blemulator issue. I have two simulated peripherals, one with a service SERVICE_STATUS and one without. When I try to use bleManager.startPeripheralScan(uuids: [SERVICE_STATUS]) it returns both peripherals rather than just one. However, if I manually check each scan result's advertisementData then I get the desired results. I'm not super familiar with Dart/Flutter so there could just be some other dumb mistake in the relevant code:

Stream<ScanResult> timeoutScan( {int sec = 5}) {
    Future.delayed(Duration(seconds:sec), () {
      print("++++++ delayed scan stopping");
      bleManager.stopPeripheralScan();
    } );

    return bleManager.startPeripheralScan(uuids: [SERVICE_STATUS]); //this parameter makes no difference to the results
  }

...

Future<void> updateScan({timeout = 5}) async {
  Peripheral p;
  foundDevices.clear();

  if (!scanInProgress)
  {
    scanInProgress = true;
    await timeoutScan(sec: timeout).listen((scanResult) async {
      p = scanResult.peripheral;
      if (!foundDevices.contains(p) && !connectedDevices.contains(p) && scanResult.advertisementData.serviceUuids.contains(SERVICE_STATUS)) {
        foundDevices.addPeripheral(p);
      }

       ...

    }).asFuture();
    scanInProgress = false;
  }
}