Printer Connection:

Connecting to a Printer:

  • The connectToDiscoveredPrinter method uses the intialized PrinterDiscovery object and attempts to connect to the desired printer and returns a PrinterDetails object.
printerDetails = await printerDiscovery.connectToDiscoveredPrinter(printerSelected: printerToConnectTo!, listeners: listeners)
  • printerSelected is the DiscoveredPrinterInformation object which represents the printer that will be connected.
  • List<PrinterUpdateListener> is a list of listener objects. (This works similar to a "Context" object because we can initialize it by adding "self" to a list.)

Example:

var listeners: Array<PrinterUpdateListener> = []
listeners.append(self)

//Iterate through all found printers.
var printerToConnectTo: DiscoveredPrinterInformation? = nil
for printer in foundPrinters {
    //If the user-selected printer name matches one of the found printer's, 
    //set this printer object as the one we want to connect to.
    if printer.getName() == printerSelected {
        printerToConnectTo = printer
        //Stop the scan since we found the printer we want to connect to.
        printerDiscoveryStopped()
    }
}
//Connect to the found printer.
printerDetails = await printerDiscovery.connectToDiscoveredPrinter(printerSelected: printerToConnectTo!, listeners: listeners)
  • 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.
  • If the connection has failed, it will return nil.
  • After the connection is finished, the PrinterDetail's haveOwernship() method can also be used.
    • This method will return a Boolean representing the ownership of the desired printer to the used mobile device. (Reference the "PrinterDetails" page under "Printer Connection" for more details.)

Automatic Connection:

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

//Use this PrinterDiscovery method to find the name of the printer available to automatically connect to.
lastConnectedPrinterName = 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 printer == model.lastConnectedPrinterName {
    connected = await model.ConnectTo(printerSelected: printer)
}

Calling printerDetails.disconnect() will make the app "forget" the current printer internally and will not connect to it automatically afterwards.


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 Printers:

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

Performing Other Printer Operations:

  • Feed a label:

    • let result = await printerDetails?.feed()
  • Cut a label:

    • let result = await printerDetails?.cutLabel()
  • Set the printer's automatic shutoff timer:

    • let result = printerDetails?.turnOff(timeInMinutes: Int)

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 PrinterUpdate(list: List<PrinterProperties>) {
    for property: PrinterProperties in list {
        if property == PrinterProperties.CurrentStatus {
            if printerDetails.getPrinterStatusMessage() == "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 reconnect() if you have previously connected since the app has been opened. 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 are properly disconnected.