startScan method

  1. @override
Future<Stream<BleDevice>> startScan({
  1. List<ScanFilter>? filters,
  2. ScanSettings? settings,
})
override

Starts a scan for nearby BLE devices and returns a Stream of BleDevice instances representing the BLE devices that were discovered. On the Flutter side, listeners can be added to this stream so they can respond to Bluetooth devices being discovered, for example by presenting the list in the user interface or enabling controllers to find and connect to specific devices.

Implementation

@override
Future<Stream<BleDevice>> startScan({List<ScanFilter>? filters, ScanSettings? settings}) async {
  // Simulate a scan by adding fake devices after a short delay.
  Future.delayed(const Duration(milliseconds: 100), () {
    for (final BleDevice device in _fakeDevices) {
      // Check that the device matches the filters. The filtering logic is the same as the logic used in
      // `CentralMethodChannel`. However, note that the filtering applied by the native side is not used here so it is
      // not a perfect match.
      if (filters.deviceMatchesFilters(device)) {
        _scanResultsController.add(device);
      }
      // If the device does not match the filters, it is ignored.
    }
  });

  return _scanResultsController.stream;
}