gpt4 book ai didi

ios - Brother SDK如何在IOS上查找打印机?

转载 作者:行者123 更新时间:2023-11-28 13:31:32 24 4
gpt4 key购买 nike

我从 iPad 中查找打印机设置打印可以找到我的 Brother 打印机。

但是当我尝试代码时,我得到的设备列表是空的,我不知道为什么。

我不熟悉 Swift。我只是尝试官方文档中的示例代码。 https://support.brother.com/g/s/es/htmldoc/mobilesdk/guide/discover-printer.html

这是我的代码:

func getPrinter() {

let printerManager = BRPtouchNetworkManager()

printerManager.setPrinterName("Brother QL-720NW")
printerManager.startSearch(5)

printerManager.getPrinterNetInfo()

print("start")
let testFind = YourClass()

print("1")
testFind.startSearchWiFiPrinter()
testFind.didFinishSearch(printerManager)
print("2")
}

class YourClass: NSObject, BRPtouchNetworkDelegate {
private var networkManager: BRPtouchNetworkManager?

func startSearchWiFiPrinter() {
print("3")
let manager = BRPtouchNetworkManager()
manager.delegate = self
manager.startSearch(5)
self.networkManager = manager
}

// BRPtouchNetworkDelegate
func didFinishSearch(_ sender: Any!) {
print("4")
guard let manager = sender as? BRPtouchNetworkManager else {
print("5")
return
}
guard let devices = manager.getPrinterNetInfo() else {
print("6")
return
}
print(devices)
print("7")
for deviceInfo in devices {
print("8")
if let deviceInfo = deviceInfo as? BRPtouchDeviceInfo {
print("Model: \(deviceInfo.strModelName), IP Address: \(deviceInfo.strIPAddress)")
}
}
}
}

我调用函数 getPrinter(),这是我的打印日志:

enter image description here

最佳答案

SDK 文档为您提供了两种方法的示例实现:

func startSearchWiFiPrinter() {}

func didFinishSearch(_ sender: Any!) {}

在您要执行搜索的类中,您必须实现这些。您还需要声明该类以参与协议(protocol) BRPtouchNetworkDelegate。最后一件事是拥有一个能够容纳网络管理器的属性(在以下行中完成:private var networkManager: BRPtouchNetworkManager?)

但是,您不应该自己调用“didFinishSearch”方法。当您调用 startSearchWiFiPrinter 时,搜索开始,BRPtouchNetworkManager 实例自身 调用didFinishSearch 方法。它之所以能够这样做,是因为您在以下行中设置了委托(delegate):manager.delegate = self

你不应该为此需要 2 个类。您也不应该使用 2 个 BRPtouchNetworkManager 实例。

试试这个。请记住,您作为参数放入 startSearchWiFiPrinter 的数字表示搜索将持续多长时间(以秒为单位)。

class EXAMPLEClass: NSObject, BRPtouchNetworkDelegate {
private var networkManager: BRPtouchNetworkManager?
func getPrinter() {
self.startSearchWiFiPrinter()
}

func startSearchWiFiPrinter() {
let manager = BRPtouchNetworkManager()
manager.delegate = self
manager.setPrinterName("Brother QL-720NW")
manager.startSearch(5)
self.networkManager = manager
}

// BRPtouchNetworkDelegate
func didFinishSearch(_ sender: Any!) {
print("4")
guard let manager = sender as? BRPtouchNetworkManager else {
print("5")
return
}
guard let devices = manager.getPrinterNetInfo() else {
print("6")
return
}
print(devices)
print("7")
for deviceInfo in devices {
print("8")
if let deviceInfo = deviceInfo as? BRPtouchDeviceInfo {
print("Model: \(deviceInfo.strModelName), IP Address: \(deviceInfo.strIPAddress)")
}
}
}
}

关于ios - Brother SDK如何在IOS上查找打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57250005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com