15 Replies Latest reply on Nov 5, 2015 1:50 PM by Beverly Vinson

    Connect to a printer that is offline

    Sonu Kapoor

      HI,

       

      I notice that when connecting to an printer that is either offline or not visible on the network by using a wrong IP the [printer conn] call takes a while to complete. Is there a way to prevent this long wait?

      Is there a timeout that I can set somewhere?

       

      Thanks

        • Re: Connect to a printer that is offline
          Robin West

          Hi Sonu,

          You can set the maxTimeoutForRead when you create a new connection.  Keep in mind, if you do this, it may miss your printer.  If the network is slow, or the printer is at the limit of it's range, or there are a lot of printers availible, all can cause it to take longer to find. 

          Hope this helps,

          Robin

          • Re: Connect to a printer that is offline
            Robin West

            Hi Sonu, I'm going to correct myself and tell you to use the NetworkDiscovery class in the SDK.  The findPrinters(DiscoveryHandler discoveryHandler, List<String> printersToFind, int waitForResponsesTimeout) function has a parameter for timeout (int waitForResponsesTimeout).  You can use this function even if you know the IP of the printer you want (List<String> printersToFind).  My same warnings will hold true though, if you make the timeout too low, it may miss printers.  It's a callback function as well, so you don't have to worry about freezing the app while you wait.

            Robin

              • Re: Connect to a printer that is offline
                Sonu Kapoor

                Thanks. I will try this out and report back.

                • Re: Connect to a printer that is offline
                  Sonu Kapoor

                  Any chance you can help out with some sample code?

                    • Re: Re: Connect to a printer that is offline
                      Robin West

                      This is modified from the SDK documentation.  There is also examples in the SDK install: android\v2.8.2148\demos\src\com\zebra\android\devdemo\discovery.


                      import java.util.ArrayList;

                      import java.util.List;

                       

                      import com.zebra.sdk.printer.discovery.DiscoveredPrinter;

                      import com.zebra.sdk.printer.discovery.DiscoveryException;

                      import com.zebra.sdk.printer.discovery.DiscoveryHandler;

                      import com.zebra.sdk.printer.discovery.NetworkDiscoverer;

                       

                      public class NetworkDiscovererExample {

                           public static void main(String[] args) {

                               DiscoveryHandler discoveryHandler = new DiscoveryHandler() {

                                   List<DiscoveredPrinter> printers = new ArrayList<DiscoveredPrinter>();

                       

                                   public void foundPrinter(DiscoveredPrinter printer) {

                                       printers.add(printer);

                                   }

                       

                                   public void discoveryFinished() {

                                       for (DiscoveredPrinter printer : printers) {

                                           System.out.println(printer);

                                       }

                                       System.out.println("Discovered " + printers.size() + " printers.");

                                   }

                       

                                   public void discoveryError(String message) {

                                       System.out.println("An error occurred during discovery : " + message);

                                   }

                               };

                               try {

                                   int timeout = 3000;

                                   List<Sring> ipList = new List<string>();

                                   ipList.add("10.80.127.80");

                                   System.out.println("Starting printer discovery.");

                                   NetworkDiscoverer.findPrinters(discoveryHandler, ipList, timeout);

                               } catch (DiscoveryException e) {

                                   e.printStackTrace();

                               }

                           }

                      }