startDiscovery method

  1. @override
Stream<BluetoothDiscoveryResult> startDiscovery()
override

Starts an inquiry and streams sightings. Cancelling the subscription (or calling stopDiscovery) stops the inquiry on every backend except Windows, where the inquiry runs to completion (~10s) on a worker isolate and its results are delivered in one batch; cancelling there only discards them.

Implementation

@override
Stream<BluetoothDiscoveryResult> startDiscovery() {
  late StreamController<BluetoothDiscoveryResult> controller;
  controller = StreamController<BluetoothDiscoveryResult>.broadcast(
    onListen: () {
      discoveryStarted = true;
      for (final r in discoveryResults) {
        controller.add(r);
      }
      if (discoveryError != null) controller.addError(discoveryError!);
    },
    onCancel: () {
      discoveryStopped = true;
    },
  );
  return controller.stream;
}