Keep open connections to printers with ZSDK

// Expert user has replied.
D Daniel Gato 2 weeks 4 days ago
17 1 0

We are trying to do something simple in essence but we keep running into ZSDK / Switch crashing.

We would like on our global AppViewModel to open 2 connections to 2 different printers connected via bluetooth. 

Then, we would like to display the status of the printers on our App header and on demand, open a connection, send a command, and finally close the connection.

We are working with this code:

        DispatchQueue.global(qos: .userInitiated).async {
            EAAccessoryManager.shared().registerForLocalNotifications()
            
            let connectedAccessories = EAAccessoryManager.shared().connectedAccessories
            connectedAccessories.forEach { accessory in
                autoreleasepool {
                    var printerConnection: MfiBtPrinterConnection?;
                    
                    if accessory.name.starts(with: "KR L") && type == .label {
                        print("KR L")
                        printerConnection = MfiBtPrinterConnection(serialNumber: accessory.serialNumber)
                    } else if accessory.name.starts(with: "KR T") && type == .tag {
                        print("KR T")
                        printerConnection = MfiBtPrinterConnection(serialNumber: accessory.serialNumber)
                    } else {
                        print("looking for \(type), got \(accessory.name)")
                        return
                    }
                    
                    if let printerConnection = printerConnection {
                        
                        let open = printerConnection.open()
                        if open {
                            do {
                                let printer = ZebraPrinterFactory.getInstance(printerConnection, with: PRINTER_LANGUAGE_ZPL)
                                if let tool = printer?.getToolsUtil() {
                                    try tool.sendCommand(command)
                                }
                            } catch {
                                MessageManager.shared.handleError(error)
                            }
                            printerConnection.close()
                        } else {
                            MessageManager.shared.handleError("Failed to open printer connection")
                        }
                    } else {
                        return
                    }
                }
            }

Is it possible to your knowledge to store printerConnection in a global state and then call ZebraPrinterFactory.getInstance(printerConnection, with: PRINTER_LANGUAGE_ZPL) on a need bases or is this the wrong approach?

Currently, if we queue too many printings in a serie the whole thing crashes and the iPad looses the connection

Please register or login to post a reply

1 Replies

S Steven Si

For best practice, we would not recommended keeping the Bluetooth connection open all the time, because the connection object (i.e., MfiBtPrinterConnection) can become invalid if the printer is out of the Bluetooth range of the iPad. It's ok to show the printer's Bluetooth connection status based on the return of EAAccessoryManager.shared().connectedAccessories. Keep in mind that the connection status may change at any given time, because of the distance dynamics between the printer and the iPad. It's wise to subscribe to the notifications for the connect/disconnect of the printer Bluetooth via the EAAccessoryDelegate, so that we don't have to poll the result from the EAAccessoryManager.shared().connectedAccessories periodically.

Once the labels are ready for print, we call the MfiBtPrinterConnection() to create a connection. After the labels are printed, we close the connection. Please keep in mind that there is a limit of the Bluetooth buffer size. The buffer may overflow if we keep sending the data (labels) over the connection. Depending on the data size of each label, we may need to pause for 1 or 2 seconds between labels. If there is a lot of data in a ZPL label (such as a label that contains a lot of graphics), we may have to break the data into chunks and send one chunk of data at a time.

CONTACT
Can’t find what you’re looking for?