2017-06-15 189 views
5

我遵循Zebra Android Link_OS SDK示例代码,通过蓝牙在ZQ510上打印测试标签,但不会以ZPL格式打印。Zebra打印机不会打印ZPL格式

这里是我运行打印标签的代码:

private void sendZplOverBluetooth(final String theBtMacAddress) { 

    new Thread(new Runnable() { 
     public void run() { 
      try { 
       // Instantiate connection for given Bluetooth® MAC Address. 
       Connection thePrinterConn = new BluetoothConnection(theBtMacAddress); 

       // Initialize 
       Looper.prepare(); 

       // Open the connection - physical connection is established here. 
       thePrinterConn.open(); 

       // This example prints "This is a ZPL test." near the top of the label. 
       String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ"; 

       // Send the data to printer as a byte array. 
       thePrinterConn.write(zplData.getBytes()); 

       // Make sure the data got to the printer before closing the connection 
       Thread.sleep(500); 

       // Close the connection to release resources. 
       thePrinterConn.close(); 

       Looper.myLooper().quit(); 
      } catch (Exception e) { 
       // Handle communications error here. 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 
} 

这里是打印的结果。 (我跑了两次,这就是为什么有两个测试打印)。

Example print 然后我读到它是如何处于不同的模式,因为由于某种原因,斑马无法检测到他们自己的专有语言。所以我试图获取设置并查看Android应用。再次使用给定链路-OS SDK示例代码:

private static void displaySettings(Connection c) throws ConnectionException, ZebraPrinterLanguageUnknownException, SettingsException, ZebraIllegalArgumentException { 
    ZebraPrinter genericPrinter = ZebraPrinterFactory.getInstance(c); 
    ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.createLinkOsPrinter(genericPrinter); 

    if (linkOsPrinter != null) { 

     System.out.println("Available Settings for myDevice"); 
     Set<String> availableSettings = linkOsPrinter.getAvailableSettings(); 
     for (String setting : availableSettings) { 
      System.out.println(setting + ": Range = (" + linkOsPrinter.getSettingRange(setting) + ")"); 
     } 

     System.out.println("\nCurrent Setting Values for myDevice"); 
     Map<String, String> allSettingValues = linkOsPrinter.getAllSettingValues(); 
     for (String settingName : allSettingValues.keySet()) { 
      System.out.println(settingName + ":" + allSettingValues.get(settingName)); 
     } 

     String darknessSettingId = "print.tone"; 
     String newDarknessValue = "10.0"; 
     if (availableSettings.contains(darknessSettingId) && 
       linkOsPrinter.isSettingValid(darknessSettingId, newDarknessValue) && 
       linkOsPrinter.isSettingReadOnly(darknessSettingId) == false) { 
      linkOsPrinter.setSetting(darknessSettingId, newDarknessValue); 
     } 

     System.out.println("\nNew " + darknessSettingId + " Value = " + linkOsPrinter.getSettingValue(darknessSettingId)); 
    } 
} 

这一次,我得到一个SettingsExceptionOperation cannot be performed on raw channel with a printer set to line print mode

我如何能够使用Mac和正确地开发Android打印ZPL文字说明?我阅读了关于使用一些Zebra Utility应用程序来改变模式,但它仅适用于Windows,并且他们的Android应用程序不起作用。无论如何,如果有人在不正确的模式下使用打印机的应用程序,他们将不得不经历所有这些不必要的设置,这对任何人都不是直观的。

感谢您的帮助和欣赏任何反馈。

+0

我建议您添加一个较小的问题图片,我认为这个帖子对于帖子太大了。 – Dayan

回答

4

您可以将打印模式编程设置为ZPL,这是目前在line-mode

要做到这一点:

BluetoothConnection printerIns= new BluetoothConnection(theBtMacAddress); 
ZebraPrinter zPrinterIns = ZebraPrinterFactory.getInstance(printerIns); 
//Set printer to ZPL mode 
zPrinterIns.sendCommand("! U1 setvar \"device.languages\" \"zpl\"\r\n"); 
//Feed and calibrate to the media 
zPrinterIns.sendCommand("~jc^xa^jus^xz"); 

在您的示例代码,您都建立蓝牙连接,并试图发送原始数据,利用斑马从代替com.zebra.sdk.printer命名空间提供的ZebraPrinterBluetoothConnection类。

我更正了你的代码,它现在应该可以工作。

new Thread(new Runnable() { 
     public void run() { 
      try { 
       // Instantiate connection for given Bluetooth&reg; MAC Address. 
       BluetoothConnection thePrinterConn = new BluetoothConnection(theBtMacAddress); 

       // Initialize 
       Looper.prepare(); 

       // Open the connection - physical connection is established here. 
       ZebraPrinter zPrinterIns = ZebraPrinterFactory.getInstance(thePrinterConn); 
       zPrinterIns.sendCommand("! U1 setvar \"device.languages\" \"zpl\"\r\n"); 
       zPrinterIns.sendCommand("~jc^xa^jus^xz"); 
       Thread.sleep(500); 

       // Send the data to printer as a byte array. 
       zPrinterIns.sendCommand("^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ"); 

       // Make sure the data got to the printer before closing the connection 
       Thread.sleep(500); 

       // Close the connection to release resources. 
       thePrinterConn.close(); 

       Looper.myLooper().quit(); 
      } catch (Exception e) { 
       // Handle communications error here. 
       e.printStackTrace(); 
      } 
     } 
}).start(); 
+1

完美的解决方案。谢谢您的帮助! – jacks205

+0

随时!我和你一样经历了同样的问题。有一个好的 – Dayan

+0

@Dayan我有一个.txt文件,其中包含ZPL代码,如:^ XA ^ FO50,50^A0,60,60^FDTest^FS ^ XZ但我无法打印它的输出在斑马ZTC gc420t(EPL)打印机。我该怎么办? – ArgaPK

0

如果您不想像Dayan答案那样以编程方式执行此步骤,并且您可以访问Windows机器(或模拟其中一个),请安装Zebra Setup Utilities。然后按照指示在这里https://km.zebra.com/kb/index?page=content&id=SO7296打印模式切换到ZPL用命令

! U1 setvar "device.languages" "zpl" 
+0

我想这是我不得不接受的最可能的解决方案。感谢你的回答。 – jacks205