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 = try 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.
        printerDiscovery.stopPrinterDiscovery()
    }
}
//Connect to the found printer.
printerDetails = try 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 newlyDiscoveredPrinter == model.lastConnectedPrinterName {
    connected = try await model.connectToDiscoveredPrinter(printerSelected: printerToConnectTo!, listeners: listeners)
}

Calling printerDetails.disconnect() will make the app "forget" the current printer internally and will not connect to it automatically afterwards. Alternatively, calling printerDetails.disconnectWithoutForget() will disconnect from the printer temporarily, but getLastConnectedPrinterName() will still return the name of the printer you just disconnected from.


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 try await printerDetails?.disconnect()
  • After disconnecting, it is recommended to start discovering for printers again.

Performing Other Printer Operations:

  • Feed a label:

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

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

    • let result = try await printerDetails?.setAutomaticShutoffTime(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 nil.

  • 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 connected printer. 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. However if you call printerDetails.disconnectWithoutForget(), the previously connected printer will be remembered still. Therefore, this is a perfect situation to use reconnect().