startDiscovery method
Starts a real radio inquiry and streams sightings of nearby devices, paired or not. Cancelling the subscription (or calling stopDiscovery) aborts the inquiry on every backend.
Implementation
@override
Stream<BluetoothDiscoveryResult> startDiscovery() {
late StreamController<BluetoothDiscoveryResult> controller;
controller = StreamController<BluetoothDiscoveryResult>.broadcast(
onListen: () {
discoveryStarted = true;
startDiscoveryCount++;
_liveDiscoveries.add(controller);
for (final r in discoveryResults) {
controller.add(r);
}
if (discoveryError != null) controller.addError(discoveryError!);
if (discoveryCompletes && !controller.isClosed) {
unawaited(controller.close());
}
},
onCancel: () {
discoveryStopped = true;
stopDiscoveryCount++;
_liveDiscoveries.remove(controller);
},
);
return controller.stream;
}