startDiscovery method

Stream<BluetoothDiscoveryResult> startDiscovery()

Starts discovery and provides stream of BluetoothDiscoveryResults.

Implementation

Stream<BluetoothDiscoveryResult> startDiscovery() async* {
  late StreamSubscription subscription;
  StreamController controller;

  controller = new StreamController(
    onCancel: () {
      // `cancelDiscovery` happens automaticly by platform code when closing event sink
      subscription.cancel();
    },
  );

  await _methodChannel.invokeMethod('startDiscovery');

  subscription = _discoveryChannel.receiveBroadcastStream().listen(
        controller.add,
        onError: controller.addError,
        onDone: controller.close,
      );

  yield* controller.stream
      .map((map) => BluetoothDiscoveryResult.fromMap(map));
}