getPrinters method

Future<void> getPrinters({
  1. Duration refreshDuration = const Duration(seconds: 2),
  2. List<ConnectionType> connectionTypes = const [ConnectionType.BLE, ConnectionType.USB, ConnectionType.NETWORK],
  3. bool androidUsesFineLocation = false,
})

Discover BLE and USB printers, plus installed network queues on macOS and Windows. Network discovery elsewhere still needs an explicit IP.

Implementation

Future<void> getPrinters({
  Duration refreshDuration = const Duration(seconds: 2),
  List<ConnectionType> connectionTypes = const [
    ConnectionType.BLE,
    ConnectionType.USB,
    ConnectionType.NETWORK,
  ],
  bool androidUsesFineLocation = false,
}) async {
  if (connectionTypes.isEmpty) {
    throw Exception('No connection type provided');
  }
  _scanConnectionTypes = connectionTypes.toSet();
  _queueScanGeneration++;
  await _usbSubscription?.cancel();
  _usbSubscription = null;
  sortDevices();

  final wantsQueues =
      connectionTypes.contains(ConnectionType.USB) ||
      connectionTypes.contains(ConnectionType.NETWORK);
  if (Platform.isMacOS && wantsQueues) {
    await _pollPrinters(refreshDuration, _macOSQueues);
  } else if (Platform.isWindows && wantsQueues) {
    await _pollPrinters(
      refreshDuration,
      () async => PrinterNames(PRINTER_ENUM_LOCAL).all(),
    );
  } else if (Platform.isAndroid &&
      connectionTypes.contains(ConnectionType.USB)) {
    await _pollPrinters(refreshDuration, _androidUsbPrinters);
  }

  if (connectionTypes.contains(ConnectionType.BLE)) {
    await _getBLEPrinters(androidUsesFineLocation);
  }
}