discovery method

Stream<PrinterDevice> discovery({
  1. bool isBle = false,
  2. Duration? timeout = const Duration(seconds: 7),
})

Starts a scan for Bluetooth Low Energy devices Timeout closes the stream after a specified Duration this device is low energy isBle

Implementation

Stream<PrinterDevice> discovery({
  bool isBle = false,
  Duration? timeout = const Duration(seconds: 7),
}) async* {
  final killStreams = <Stream>[];
  killStreams.add(_stopScanPill);
  killStreams.add(Rx.timer(null, timeout!));
  // Clear scan results list
  _scanResults.add(<PrinterDevice>[]);

  if (Platform.isAndroid) {
    isBle ? flutterPrinterChannel.invokeMethod('getBluetoothLeList') : flutterPrinterChannel.invokeMethod('getBluetoothList');

    await for (dynamic data in _methodStream
        .where((m) => m.method == "ScanResult")
        .map((m) => m.arguments)
        .takeUntil(Rx.merge(killStreams))
        // .takeUntil(TimerStream(3, Duration(seconds: 5)))
        .doOnDone(stopScan)
        .map((message) => message)) {
      var device = PrinterDevice(name: data['name'] as String, address: data['address'] as String?);
      if (!_addDevice(device)) continue;
      yield device;
    }
  } else if (Platform.isIOS) {
    try {
      await iosChannel.invokeMethod('startScan');
    } catch (e) {
      print('Error starting scan.');
      _stopScanPill.add(null);
      _isScanning.add(false);
      throw e;
    }

    await for (dynamic data in _methodStream
        .where((m) => m.method == "ScanResult")
        .map((m) => m.arguments)
        .takeUntil(Rx.merge(killStreams))
        .doOnDone(stopScan)
        .map((message) => message)) {
      print('Scan result: $data');
      final device = PrinterDevice(name: data['name'] as String, address: data['address'] as String?);
      if (!_addDevice(device)) continue;
      yield device;
    }
  }
}