Morton
Morton

Reputation: 5770

How to find printers on IOS from Brother SDK?

I find printers from iPad setting print can find my Brother printer.

But when I try the code I get empty device list and I don't know why.

I am not familiar with Swift. I just try the sample code from Official documentation. https://support.brother.com/g/s/es/htmldoc/mobilesdk/guide/discover-printer.html

Here is my code:

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)")
        }
      }
    }
  }

I call my function getPrinter() and here is my print log:

enter image description here

Upvotes: 0

Views: 1394

Answers (1)

Naslausky
Naslausky

Reputation: 3768

The SDK documentation gives you an example implementation of two methods:

func startSearchWiFiPrinter() {}

and

func didFinishSearch(_ sender: Any!) {}

In the class you want to execute the search you must implement these. You also need to declare the class to attend the protocol BRPtouchNetworkDelegate. The last thing is to have a property to be able to hold the Network manager (Which is done in the line: private var networkManager: BRPtouchNetworkManager?)

However, you are not supposed to call the "didFinishSearch" method by yourself. When you call startSearchWiFiPrinter, the search begins, and the BRPtouchNetworkManager instance itself calls the didFinishSearch method. It is capable of doing so because you set the delegate in the line: manager.delegate = self.

You should not need 2 classes for this. You should not use 2 instances of BRPtouchNetworkManager either.

Try this. Remember the number you put as an argument to startSearchWiFiPrinter means how long in seconds the search will be.

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)")
        }
      }
    }
  }

Upvotes: 1

Related Questions