Discovering Printers:
Before starting this tutorial, it is important to note that the SDK was not designed to discover, connect, print, and disconnect all at once. The printer itself will still struggle to receive and send data this quickly.
Example: It is not recommended to design an app with a single button labeled "Print" where the app would discover, connect, print, and then disconnect on the button click.
Requesting Permissions:
In order to discover nearby printers, the user must approve the required permissions:
(Reference the Setup page for the iOS SDK)
- This should prompt the user only on the initial opening of the application.
- If the user does not allow permissions when prompted, they can always go into the settings app to allow them later. However, the user will never be able to discover printers without approving these permissions.
Implementing Discovery:
- Create a new PrinterDiscovery object:
var printerDiscovery: PrinterDiscovery = PrinterDiscoveryFactory.getPrinterDiscovery(listeners: [PrinterDiscoveryListener])
- Start discovering nearby printers:
- PrinterDiscoveryListener must be implemented in the UI's class where you will store the discovered printer objects.
- This will force you to add the printerDiscovered method that you can implement like:
- PrinterDiscoveryListener must be implemented in the UI's class where you will store the discovered printer objects.
public func printerDiscovered(discoveredPrinterInformation: DiscoveredPrinterInformation) {
if(!foundPrinters.contains(where: {discoveredPrinterInformation.getName() == $0.getName()})) {
foundPrinters.append(discoveredPrinterInformation)
}
}
-
To start discovering Bluetooth Low Energy devices, call:
printerDiscovery.startBlePrinterDiscovery()
printerDiscovery.startWifiPrinterDiscovery()
-
To stop the discovery scan for Bluetooth Low Energy devices, call:
printerDiscovery.stopPrinterDiscovery()
-
To find the most recent printer you successfully connected to, call:
let lastConnectedPrinter = printerDiscovery.getLastConnectedPrinterName()
Possible Solution For Displaying Printers:
Swift has specific tags that allow a list to automatically notify the UI if it has changed. These tags are called @Published and @ObservedObject.
In order for the UI to update immediately when a new printer is found, initialize the foundPrinters list as such:
@Published private var foundPrinters = [DiscoveredPrinterInformation]()
In order for our "Model" class to comply, we need to inherit ObservableObject. Your "Model" class header may look like this after fully implementing the Brady SDK:
public class Model: PrinterUpdateListener, PrinterDiscoveryListener, ObservableObject {
Lastly, initialize the "Model" object in the UI class. Initialize it as such:
@ObservedObject var model = Model()
Now, whenever we call a method on the variable "model", it will send the UI its updated state.
To summarize, now a user could initialize a NavigationView holding a list of items in their UI. In this case, the list will represent printer names. With this implemented, the UI will update whenever a new printer is found and the list of printers in our "Model" class is changed.