getPrinters method

void getPrinters({
  1. Duration refreshDuration = const Duration(seconds: 5),
  2. List<ConnectionType> connectionTypes = const [ConnectionType.BLE, ConnectionType.USB],
})

Implementation

void getPrinters({
  Duration refreshDuration = const Duration(seconds: 5),
  List<ConnectionType> connectionTypes = const [
    ConnectionType.BLE,
    ConnectionType.USB,
  ],
}) async {
  List<Printer> btlist = [];
  if (connectionTypes.contains(ConnectionType.BLE)) {
    await init();
    if (!isInitialized) {
      await init();
    }
    if (!isInitialized) {
      throw Exception(
        'WindowBluetoothManager is not initialized. Try starting the scan again',
      );
    }
    WinBle.stopScanning();
    WinBle.startScanning();
    subscription?.cancel();
    subscription = WinBle.scanStream.listen((device) async {
      btlist.add(Printer(
        address: device.address,
        name: device.name,
        connectionType: ConnectionType.BLE,
        isConnected: await WinBle.isPaired(device.address),
      ));
    });
  }
  List<Printer> list = [];
  if (connectionTypes.contains(ConnectionType.USB)) {
    _usbSubscription?.cancel();
    _usbSubscription = Stream.periodic(refreshDuration, (x) => x).listen((event) async {
      final devices = PrinterNames(PRINTER_ENUM_LOCAL);
      List<Printer> templist = [];
      for (var e in devices.all()) {
        final device = Printer(
          vendorId: e,
          productId: "N/A",
          name: e,
          connectionType: ConnectionType.USB,
          address: e,
          isConnected: true,
        );
        templist.add(device);
      }
      list = templist;
    });
  }
  Stream.periodic(refreshDuration, (x) => x).listen((event) {
    _devicesstream.add(list + btlist);
  });
}