Printer Connection:

Connecting to a Printer:

  • The connectToDiscoveredPrinter() method uses the intialized PrinterDiscovery and attemps to connect to the desired printer and returns a PrinterDetails object.
printerDiscovery.connectToDiscoveredPrinter(Context context, DiscoveredPrinterInformation printerSelected, List<PrinterUpdateListener> pul)
  • context is the UI context of the activity
  • printerSelected is the DiscoveredPrinterInformation object of the printer to connect to.
  • pul is a list of listener objects. (This works similar to a "Context" object because it can be initialized as "MainActivity.this".)

It is recommended to call the connectToDiscoveredPrinter method inside another thread, so that it can run asynchronously and we can wait for it to finish by initializing a loading wheel or another similar view.

Runnable r = () -> {
    printerDetails = printerDiscovery.connectToDiscoveredPrinter(context, printerSelected, Collections.singletonList(MainActivity.this));
};
Thread connectThread = new Thread(r);
connectThread.start();

After the connection has successfully finished, it will return a PrinterDetails object populated with details about the printer, such as name and model number, battery life, and installed part/supply information.

  • To show a progress bar and then hide it upon completing a successful connection, see the code below:
public void connectToPrinter(String printerSelected, PrinterUpdateListener pul, boolean showDetailPage) {
    Runnable r = () -> {
        runOnUiThread(()->progressBar.setVisibility(View.VISIBLE));
        printerDetails = printerDiscovery.connectToDiscoveredPrinter(context, printerSelected, Collections.singletonList(pul));
        runOnUiThread(()->progressBar.setVisibility(View.INVISIBLE));
    };
    Thread connectThread = new Thread(r);
    connectThread.start();
}

Automatic Connection:

Once your application has connected to a printer for the first time, it will store the printer object internally. You can add a feature to your app that grabs this object and connects to it automatically without having the user manually select a printer from the UI:

//Use this PrinterDiscovery method to find the name of the printer available to automatically connect to.
String lastConnectedPrinter = printerDiscovery.getLastConnectedPrinterName();
//Now, whenever a printer is discovered, it'll compare it immediately to this printer name.
//If it matches, it'll attempt to connect right away when the app starts.
if (foundPrinterName == lastConnectedPrinter) {
    printerDetails = printerDiscovery.connectToDiscoveredPrinter(context, printerSelected, Collections.singletonList(pul));
} 

Calling printerDetails.disconnect() will make the app "forget" about this printer internally and will not connect automatically afterwards until another printer is connected.


OWNERSHIP NOTE: Using an M211 introduces the concept of "ownership". When connecting to an M211 with a mobile device for the first time, the blue light on the M211 should be blinking. If the light is solid blue, another mobile device "owns" the printer and only that device will be able to connect. In the event that a connection fails, a device can still have "ownership". Therefore, being connected and having ownership are not synonymous. To connect using a different mobile device, hold the power button for five seconds to release ownership.


Disconnecting from a Printer:

  • Simply call printerDetails.disconnect()
  • After disconnecting, it is recommended to start discovering for printers again.

Performing Other Printer Operations:

  • Feed a label:

    • printerDetails.feed()
  • Cut a label:

    • printerDetails.cutLabel()
  • Set the printer's automatic shutoff timer:

    • printerDetails.turnOff(int timeInMinutes)

Connect and Disconnect Permutations

Scenario #1: If you connect for the first time and the printer disconnects unexpectedly (turned off, taken out of range, etc.), the printer will send a "CurrentStatus" update to the PrinterUpdate override method.

  • Because of this scenario, you must have a way to notify your UI view that the printer is no longer connected.
@Override
public void PrinterUpdate(List<PrinterProperties> list) {
    for(PrinterProperties property : list) {
        if(property == PrinterProperties.CurrentStatus) {
            if(printerDetails.getPrinterStatusMessage().equals("PrinterStatus_Disconnected")) {
            //ENTER CODE HERE TO HANDLE THE DISCONNECT (start discovery again or disable the print button for example)
            }
        }
    }
}

Scenario #2: You must only use the reconnect() if you have previously connected once (since the app has been open)! If you connect for the first time, close the app, open the app, and call reconnect(), it will not work because the PrinterDetails will once again be null.

  • Because of this scenario, you must only use the reconnect() method if the printer has disconnected for an unknown reason and you are attempting to reconnect.
if (printerDetails != null) {
    printerDiscovery.connectToDiscoveredPrinter(context, printerSelected, listener);
}
else {
    printerDetails.reconnect()
}

Scenario #3: You call printerDetails.disconnect() to forget the printer to which you are connected. This forgets the printer internally. Therefore, reconnect() will not work until you connect with connectToDiscoveredPrinter and are successful again. Calling printerDetails.disconnect() also releases ownership (if you are using an M211) so the blue blinking light is a clear indication that you will not be able to reconnect anymore and that you are properly disconnected.