discoverStream static method

Stream<NetWorkPrinter> discoverStream({
  1. int port = 9100,
  2. Duration timeout = const Duration(milliseconds: 5000),
})

discoverStream returns a Stream that yields printers as they are found on the network, instead of waiting for the full scan to finish.

port defaults to 9100 (standard RAW/JetDirect port).

Implementation

static Stream<NetWorkPrinter> discoverStream({
  int port = 9100,
  Duration timeout = const Duration(milliseconds: 5000),
}) async* {
  String? currentIpAddress = await getDeviceIpAddress();
  if (currentIpAddress == null) return;

  final subnet =
      currentIpAddress.substring(0, currentIpAddress.lastIndexOf('.'));
  final stream = NetworkAnalyzer.discover2(
    subnet,
    port,
    timeout: timeout,
  );

  await for (final entry in stream) {
    if (entry.exists) {
      yield NetWorkPrinter(
        id: entry.ip,
        name: entry.ip,
        address: entry.ip,
        type: 0,
      );
    }
  }
}