discoverPrinters static method

Future<List<PrinterDiscovered<TcpPrinterInfo>>> discoverPrinters({
  1. String? ipAddress,
  2. int? port,
  3. Duration? timeOut,
})

Implementation

static Future<List<PrinterDiscovered<TcpPrinterInfo>>> discoverPrinters({String? ipAddress, int? port, Duration? timeOut}) async {
  final List<PrinterDiscovered<TcpPrinterInfo>> result = [];
  final defaultPort = port ?? 9100;

  String? deviceIp;
  if (Platform.isAndroid || Platform.isIOS) {
    deviceIp = await NetworkInfo().getWifiIP();
  } else if (ipAddress != null) deviceIp = ipAddress;
  if (deviceIp == null) return result;

  final String subnet = deviceIp.substring(0, deviceIp.lastIndexOf('.'));
  // final List<String> ips = List.generate(255, (index) => '$subnet.$index');

  final stream = NetworkAnalyzer.discover2(
    subnet,
    defaultPort,
    timeout: timeOut ?? Duration(milliseconds: 4000),
  );

  await for (var addr in stream) {
    if (addr.exists) {
      result.add(PrinterDiscovered<TcpPrinterInfo>(name: "${addr.ip}:$defaultPort", detail: TcpPrinterInfo(address: addr.ip)));
    }
  }

  return result;
}