Skip to content

Instantly share code, notes, and snippets.

View EricADockery's full-sized avatar
🦕

Eric Arlan Dockery EricADockery

🦕
  • Versailles, KY
View GitHub Profile
var manager: EAAccessoryManager!
var isConnected: Bool = false
private var printerConnection: MfiBtPrinterConnection?
private var serialNumber: String?
private var disconnectNotificationObserver: NSObjectProtocol?
private var connectedNotificationObserver: NSObjectProtocol?
static let sharedInstance = PrintManager()
class ViewController: UIViewController {
@IBOutlet var printerConnectionStatus: UILabel!
var printManager = PrintManager.sharedInstance
override func viewDidLoad() {
super.viewDidLoad()
printManager.connectionDelegate = self
if printManager.isConnected {
printerConnectionStatus.text = “Connected”
} else {
printerConnectionStatus.text = “Not Connected”
private func printerTextField(font:Int, size: Int, x:Int, y: Int, content: String) -> String {
return “TEXT \(font) \(size) \(x) \(y) \(content)”
}
private func printMultiLineTextField(linesHeight: Int, font:Int, size: Int, x:Int, y: Int, content: String) -> String {
return “ML \(linesHeight)\nTEXT \(font) \(size) \(x) \(y) \n\(content)\nENDML\nENDML”
}
private func printerBarCodeFormat(width: Int, ratio: Int, height: Int, x: Int, y:Int, content: String) -> String {
return “BARCODE 128 \(width) \(ratio) \(height) \(x) \(y) \(content)”
}
func findConnectedPrinter(completion: (Bool) -> Void) {
let connectedDevices = manager.connectedAccessories
for device in connectedDevices {
if device.protocolStrings.contains(“com.zebra.rawport”) {
serialNumber = device.serialNumber
connectToPrinter(completion: { completed in
completion(completed)
})
}
}
deinit {
if let disconnectNotificationObserver = disconnectNotificationObserver {
NotificationCenter.default.removeObserver(disconnectNotificationObserver)
}
if let connectedNotificationObserver = connectedNotificationObserver {
NotificationCenter.default.removeObserver(connectedNotificationObserver)
}
}
private override init() {
super.init()
manager = EAAccessoryManager.shared()
findConnectedPrinter { [weak self] bool in
if let strongSelf = self {
strongSelf.isConnected = bool
}
}
//Notifications
disconnectNotificationObserver = NotificationCenter.default.addObserver(forName: Notification.Name.EAAccessoryDidDisconnect, object: nil, queue: nil, using: didDisconnect)
var manager: EAAccessoryManager!
var isConnected: Bool = false
//new content from step1
var connectionDelegate: EAAccessoryManagerConnectionStatusDelegate?
// end new content
private var printerConnection: MfiBtPrinterConnection?
private var serialNumber: String?
private var disconnectNotificationObserver: NSObjectProtocol?
private var connectedNotificationObserver: NSObjectProtocol?
static let sharedInstance = PrintManager()
protocol EAAccessoryManagerConnectionStatusDelegate {
func connectionStatusChanged() -> Void
}
private func didDisconnect(notification: Notification) {
isConnected = false
connectionDelegate?.connectionStatusChanged()
}
private func didConnect(notification: Notification) {
isConnected = true
connectionDelegate?.connectionStatusChanged()
}
enum CommonPrintingFormat: String {
case start = “! 0 200 200 150 1”
case end = “\nFORM\nPRINT “
}