FaISalBLiNK
FaISalBLiNK

Reputation: 711

Android Printing App for Zebra RW420 printer

I mean to start developing a simple android application for printing labels using zebra RW420 printer. They have SDK for android but I cannot figure out where to start from.

I invision an app with a single screen, it asks for the label or purchase order, how many copies we'd like to print, and a print button. Can someone help me get started. Any help will be appreciated.

How I am a going to search for the printer will the SDK will do it for me ?? or I have to use Androids BluetoothAdapter class ... about the label format do I have to create myself or I can use the existing file in the SDK ??

Can I set the printer to print multiple copies ??

Upvotes: 4

Views: 5180

Answers (2)

user3866498
user3866498

Reputation: 3

1) You have to print from a thread like:

new Thread (new Runnable () {
  public void run () {
    enablePrintButton (false);
    Looper.prepare ();
    doConnectionTest ();
    Looper.loop ();
    Looper.myLooper ().quit ();
  }
}).start ();

2) Then:

private void doConnectionTest () {
    printer = connect ();

    if (printer != null) {
      //print
    } else {
      disconnect ();
    }
  }

3) Print method:

private void sendTestLabel () {
    try {
      ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.createLinkOsPrinter (printer);

      PrinterStatus printerStatus =
          (linkOsPrinter != null) ? linkOsPrinter.getCurrentStatus () : printer.getCurrentStatus ();

      if (printerStatus.isReadyToPrint) {
        byte[] configLabel = getConfigLabel ();
        connection.write (configLabel);
        setStatus ("Sending Data", Color.BLUE);
      } else if (printerStatus.isHeadOpen) {
        setStatus ("Printer Head Open", Color.RED);
      } else if (printerStatus.isPaused) {
        setStatus ("Printer is Paused", Color.RED);
      } else if (printerStatus.isPaperOut) {
        setStatus ("Printer Media Out", Color.RED);
      }
      DemoSleeper.sleep (1500);
      if (connection instanceof BluetoothConnection) {
        String friendlyName = ((BluetoothConnection) connection).getFriendlyName ();
        setStatus (friendlyName, Color.MAGENTA);
        DemoSleeper.sleep (500);
      }
    } catch (ConnectionException e) {
      setStatus (e.getMessage (), Color.RED);
    } finally {
      disconnect ();
    }
  }

4) Label configuration method:

private byte[] getConfigLabel () {
     byte[] configLabel = null;

     try {
       PrinterLanguage printerLanguage = printer.getPrinterControlLanguage ();
       SGD.SET ("device.languages", "zpl", connection);

       if (printerLanguage == PrinterLanguage.ZPL) {
         configLabel = "^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ".getBytes();
       } else if (printerLanguage == PrinterLanguage.CPCL) {

         String msg = setPopratnica ();

         msg = format (msg);

         configLabel = msg.getBytes ("windows-1250");
       }
     } catch (Exception e) {
     }
     return configLabel;
   }

5) Connect method:

public ZebraPrinter connect () {
     setStatus ("Connecting...", Color.YELLOW);
     connection = null;
     connection = new BluetoothConnection (getBluetoothAddressKey ());
    try {
       connection.open ();
       setStatus ("Connected", Color.GREEN);
         } catch (ConnectionException e) {
           setStatus ("Comm Error! Disconnecting", Color.RED);
           DemoSleeper.sleep (1000);
           disconnect ();
         }

         ZebraPrinter printer = null;

         if (connection.isConnected ()) {
           try {
             printer = ZebraPrinterFactory.getInstance (connection);
             setStatus ("Determining Printer Language", Color.YELLOW);
             String pl = SGD.GET ("device.languages", connection);
             setStatus ("Printer Language " + pl, Color.BLUE);
          } catch (ConnectionException e) {
             setStatus ("Unknown Printer Language", Color.RED);
             printer = null;
             DemoSleeper.sleep (1000);
             disconnect ();
           } catch (ZebraPrinterLanguageUnknownException e) {
             setStatus ("Unknown Printer Language", Color.RED);
             printer = null;
             DemoSleeper.sleep (1000);
             disconnect ();
           }
         }
         return printer;
}

6) Disconect method:

public void disconnect () {
    try {
      setStatus ("Disconnecting", Color.RED);
      if (connection != null) {
        connection.close ();
      }
      setStatus ("Not Connected", Color.RED);
    } catch (ConnectionException e) {
      setStatus ("COMM Error! Disconnected", Color.RED);
    } finally {
      enablePrintButton (true);
    }
  }

Plus you have to have a sleeper class :

public class DemoSleeper {
    private DemoSleeper () {
    }

    public static void sleep(int ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Ovi Tisler
Ovi Tisler

Reputation: 6473

The SDK provides a bluetooth discovery class. Look at the documentation and the examples. Check out the BluetoothDiscoverer class. It provides a method to find all bluetooth devices and notifies you in a handler. There is a Developer Demo which comes with the SDK which has an example of how to do a discovery:

    try {
        BluetoothDiscoverer.findPrinters(this, new DiscoveryHandler() {

            public void foundPrinter(DiscoveredPrinter printer) {
                String macAddress = printer.address;
                //I found a printer! I can use the properties of a Discovered printer (address) to make a Bluetooth Connection
            }

            public void discoveryFinished() {
                //Discovery is done
            }

            public void discoveryError(String message) {
                //Error during discovery
            }
        });
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

Upvotes: 8

Related Questions