Connection with printer in iOS Application.

// Expert user has replied.
O Oleh Podluzhnyi 3 years 7 months ago
7 1 0

Hello guys.
I have got some problem.
My App works with printer Zebra ZQ520. I connected with BLE. When I send some command like
private func writeZebraScript(zpl: String) {
guard let bytes = zpl.data(using: .utf8), let chars = self.discoverWriteCharacteristic
else {
let error = "Problem with data for zebra script.\nBytes =" +
"\(zpl.data(using: .utf8) ?? Data())\nCharacteristic = " +
"\(String(describing: self.discoverWriteCharacteristic))\n"
logMessage(error, in: #function, from: "\(Self.self)")
return
}
self.selectedPrinter?.writeValue(bytes, for: chars, type: .withResponse)
}
func sendToPrint(text: String) {
let str = "^XA^FO20,20^BY3^B3N,N,150,Y,N^FD\(text)^FS^XZ"
let zpl: String = str + "\r\n"
self.writeZebraScript(zpl: zpl)
}
func calibrate() {
let zpl = "~jc^xa^jus^xz +\r\n"
self.writeZebraScript(zpl: zpl)
}
func initialSettings() {
let zpl = "! U1 setvar \"media.type\" \"label\"" +
"! U1 setvar \"media.sense_mode\" \"gap\"" + "~jc^xa^jus^xz" + "~SD30"
self.writeZebraScript(zpl: zpl)
}
It works amazing.
But when I try to do
guard let serialNumber = self.receivedDataFromDISCharacteristic[
ZebraKeysLib().zPrinteDisSerialNumber
] else {
logMessage("serialNumber is nil", in: #function, from: "\(Self.self)")
return
}
self.connection = MfiBtPrinterConnection(serialNumber: serialNumber)
self.connection?.open()
My self.connection?.isConnected is always false. And I`m not able to do my next work. Why do I have this problem?

Please Register or Login to post a reply

1 Replies

S Steven Si

Looks like you are using BLE to obtain the printer's serial number. Then switch to use Bluetooth Classic. Before you do the following, you would need to make sure that the printer's Bluetooth is paired with the iOS device first.

self.connection = MfiBtPrinterConnection(serialNumber: serialNumber)
self.connection?.open()

You also need to make sure that you have the following in your info.plist, as the printer uses com.zebra.rawport as the protocol string to communicate with the iOS device.

Lastly, iOS uses the name of the Bluetooth accessory to establish the connection. By default, the Bluetooth name of Zebra printer is the serial number. User can change the Bluetooth name (a.k.a. friendly_name) of the printer. If you have ever changed the friendly_name (! U1 setvar "device.friendly_name" "NewName"), then you need to use the NewName instead of the serial number.

Check out the documentation about MfiBtPrinterConnection.

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