printBytes method

  1. @override
Future<void> printBytes({
  1. required List<int> bytes,
  2. required Printer printer,
})
override

Implementation

@override
Future<void> printBytes({required List<int> bytes, required Printer printer}) async {
  if (printer.type == PrinterType.usb) {
    try {
      final bool result = await methodChannel.invokeMethod<bool>(
            'printBytes',
            <String, dynamic>{
              'bytes': bytes,
              'printerName': printer.name,
            },
          ) ??
          false;
      if (!result) {
        log('Failed to print bytes', name: 'THERMAL_PRINTER_FLUTTER');
      }
    } catch (e) {
      log('Error printing: $e', name: 'THERMAL_PRINTER_FLUTTER');
      rethrow;
    }
  } else if (printer.type == PrinterType.bluethoot) {
    try {
      if (isWindows) {
        await WinBleManager.instance.printBytes(bytes: bytes, address: printer.bleAddress);
      } else if (isAndroid || isIOS || isMacOS) {
        await MobileBleManager.instance.printBytes(bytes: bytes, address: printer.bleAddress);
      } else {
        _logPlatformNotSuported();
      }
    } catch (e) {
      log('Error printing via Bluetooth: $e', name: 'THERMAL_PRINTER_FLUTTER');
      rethrow;
    }
  } else if (printer.type == PrinterType.network) {
    try {
      final networkPrinter = NetworkPrinter(
        host: printer.ip,
        port: int.tryParse(printer.port) ?? 9100,
      );
      final success = await networkPrinter.printBytes(bytes);
      if (!success) {
        log('Failed to print via network', name: 'THERMAL_PRINTER_FLUTTER');
      }
    } catch (e) {
      log('Error printing via network: $e', name: 'THERMAL_PRINTER_FLUTTER');
      rethrow;
    }
  }
}