2017-06-15 349 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