discovery method

Stream<PrinterDevice> discovery({
  1. TcpPrinterInput? model,
})

Starts a scan for network printers.

Implementation

Stream<PrinterDevice> discovery({TcpPrinterInput? model}) async* {
  final defaultPort = model?.port ?? 9100;

  String? deviceIp;
  if (Platform.isAndroid || Platform.isIOS) {
    deviceIp = await NetworkInfo().getWifiIP();
  } else if (model?.ipAddress != null) {
    deviceIp = model!.ipAddress;
  } else {
    return;
    // throw Exception('No IP address provided');
  }

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

  final stream = NetworkAnalyzer.discover2(subnet, defaultPort);

  await for (var data in stream.map((message) => message)) {
    if (data.exists) {
      yield PrinterDevice(name: "${data.ip}:$defaultPort", address: data.ip);
    }
  }
}