8
votes

I am following the Zebra Android Link_OS SDK sample code for printing a test label on a ZQ510 over Bluetooth, but it won't print in ZPL format.

Here is the code I'm running to print the label:

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();
 }

And here is the result of the print. (I ran it twice, that's why there are two test prints).

Example print Then I read about how it might be in a different mode because for some reason Zebra can't detect their own proprietary language. So I tried to get the settings and see through the Android app. Again using the given Link-OS SDK example code:

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));
     }
 }

This time, I get a SettingsException with the description of Operation cannot be performed on raw channel with a printer set to line print mode

How am I able to print ZPL text using a Mac and developing Android correctly? I read about using some Zebra Utility app for changing the mode, but it's only available for Windows, and their Android app doesn't work.

Regardless, if someone was to use the app with a printer in the incorrect mode, they would have to go through all this unnecessary setup that wouldn't be intuitive for just anybody.

Thanks for the help and appreciate any feedback.

2
I recommend adding a smaller image of the issue you had, I think this one is way too big for the post.Dayan

2 Answers

7
votes

You can programmatically set the print mode to ZPL, it's currently in line-mode.

To do so:

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");

In your example code, You are establishing a Bluetooth connection and attempting to send raw data, make use of the ZebraPrinterand BluetoothConnection classes provided by Zebra instead from the com.zebra.sdk.printer namespace.

I corrected your code, it should work now.

 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
votes

If you don't want to perform this step programmatically like in the Dayan answer and you have acces to a Windows machine (or emulating one), install the Zebra Setup Utilities. Then following the instructions here https://km.zebra.com/kb/index?page=content&id=SO7296 to switch the print mode to ZPL with the command

! U1 setvar "device.languages" "zpl"