Hi Everyone,

I thought, now that you know why printing is important and needs to be thought about earlier in the development process, you might want to know how to actually print with a Zebra device.  It’s a lot like making a connection to other devices.  The main difference is that most other devices, you are usually attempting to get information (like a picture, or a scan), and printers you are usually sending information. 

 

If you have any recommendations for content you want to see, please let me know.  For my example, I’m using a Motorola ET1, a Zebra QLn220 printer and the Zebra Link-OS Multiplatform SDK.  I’m going to create a connection, make sure the printer is ready, and then print something.  And now for some code…

 

import android.os.Looper;

import com.zebra.sdk.comm.BluetoothConnectionInsecure;

import com.zebra.sdk.comm.Connection;

 

public class BluetoothConnectionInsecureExample {

    public static void main(String[] args) {

        BluetoothConnectionInsecureExample example = new BluetoothConnectionInsecureExample();

 

        // The Bluetooth MAC address can be discovered, scanned, or typed in

        String theBtMacAddress = "00:11:BB:DD:55:FF";

        example.sendZplOverBluetooth(theBtMacAddress);

    }

 

    private void sendZplOverBluetooth(final String theBtMacAddress) {

        new Thread(new Runnable() {

            public void run() {

                try {

                    // Instantiate insecure connection for given Bluetooth MAC Address.

                    Connection thePrinterConn = new BluetoothConnectionInsecure(theBtMacAddress);

 

 

                    // Verify the printer is ready to print

                    if (isPrinterReady(thePrinterConn)) {

 

 

                    // 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 insecure connection to release resources.

                    thePrinterConn.close();

 

                    Looper.myLooper().quit();

                }

            } catch (Exception e) {

                // Handle communications error here.

                e.printStackTrace();

            }

        }

    }).start();

 

 

    private bool isPrinterReady(Connection thePrinterConn) {

        bool isOK = false;

        try {

            connection.open();

            // Creates a ZebraPrinter object to use Zebra specific functionality like getCurrentStatus()

            ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);

 

            PrinterStatus printerStatus = printer.getCurrentStatus();

            if (printerStatus.isReadyToPrint) {

                isOK = true;

            } else if (printerStatus.isPaused) {

                System.out.println("Cannot Print because the printer is paused.");

            } else if (printerStatus.isHeadOpen) {

                System.out.println("Cannot Print because the printer media door is open.");

            } else if (printerStatus.isPaperOut) {

                System.out.println("Cannot Print because the paper is out.");

            } else {

                System.out.println("Cannot Print.");

            }

        } catch (ConnectionException e) {

            e.printStackTrace();

        } catch (ZebraPrinterLanguageUnknownException e) {

            e.printStackTrace();

        }

        return isOK;

    }

}