Package com.zebra.sdk.printer.discovery
Class NetworkDiscoverer
Object
com.zebra.sdk.printer.discovery.NetworkDiscoverer
A class used to discover printers on an IP Network.
package test.zebra.sdk.discovery.examples;
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 {
System.out.println("Starting printer discovery.");
NetworkDiscoverer.findPrinters(discoveryHandler);
} catch (DiscoveryException e) {
e.printStackTrace();
}
}
}
-
Method Summary
Modifier and TypeMethodDescriptionstatic voiddirectedBroadcast(DiscoveryHandler discoveryHandler, String ipAddress) Sends a directed broadcast discovery packet to the subnet specified byipAddress.static voiddirectedBroadcast(DiscoveryHandler discoveryHandler, String ipAddress, int waitForResponsesTimeout) Sends a directed broadcast discovery packet to the subnet specified byipAddress.static voidfindPrinters(DiscoveryHandler discoveryHandler) This method will search the network using a combination of discovery methods to find printers on the network.static voidfindPrinters(DiscoveryHandler discoveryHandler, List<String> printersToFind) Sends a discovery request to the list of printer DNS names or IPs inprintersToFind.static voidfindPrinters(DiscoveryHandler discoveryHandler, List<String> printersToFind, int waitForResponsesTimeout) Sends a discovery request to the list of printer DNS names or IPs inprintersToFind.static voidipv6Multicast(DiscoveryHandler discoveryHandler, int hops) Sends an IPv6 multicast discovery packet.static voidipv6Multicast(DiscoveryHandler discoveryHandler, int hops, int waitForResponsesTimeout) Sends an IPv6 multicast discovery packet.static voidlocalBroadcast(DiscoveryHandler discoveryHandler) Sends a local broadcast packet.static voidlocalBroadcast(DiscoveryHandler discoveryHandler, int waitForResponsesTimeout) Sends a local broadcast packet.static voidmulticast(DiscoveryHandler discoveryHandler, int hops) Sends a multicast discovery packet.static voidmulticast(DiscoveryHandler discoveryHandler, int hops, int waitForResponsesTimeout) Sends a multicast discovery packet.static voidsubnetSearch(DiscoveryHandler discoveryHandler, String subnetRange) Sends a discovery packet to the IPs specified in thesubnetRange.static voidsubnetSearch(DiscoveryHandler discoveryHandler, String subnetRange, int waitForResponsesTimeout) Sends a discovery packet to the IPs specified in thesubnetRange.
-
Method Details
-
subnetSearch
public static void subnetSearch(DiscoveryHandler discoveryHandler, String subnetRange) throws DiscoveryException Sends a discovery packet to the IPs specified in thesubnetRange. Subnet searches are defined by the first three subnet octets, followed by a range, such as 192.168.2. This method accepts IP addresses of the form, assuming a subnet of 192.168.2:- 192.168.2.254 (will send a discovery packet to 192.168.2.254)
- 192.168.2.* (will send a discovery packet for the range 192.168.2.1 - 192.168.2.254)
- 192.168.2.8-* (will send a discovery packet for the range 192.168.2.8 - 192.168.2.254)
- 192.168.2.37-42 (will send a discovery packet for the range 192.168.2.37 - 192.168.2.42)
This overload uses a default timeout of 6000 milliseconds (6 seconds).
This method will invoke theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery. WhenDiscoveryHandler.discoveryError(String)is invoked, the discovery will be canceled andDiscoveryHandler.discoveryFinished()will not be invoked.
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).subnetRange- the subnet search range.- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
subnetSearch
public static void subnetSearch(DiscoveryHandler discoveryHandler, String subnetRange, int waitForResponsesTimeout) throws DiscoveryException Sends a discovery packet to the IPs specified in thesubnetRange. This method will wait up towaitForResponsesTimeoutmilliseconds determining that there are no more discovery responses. Subnet searches are defined by the first three subnet octets, followed by a range, such as 192.168.2. This method accepts IP addresses of the form, assuming a subnet of 192.168.2:- 192.168.2.254 (will send a discovery packet to 192.168.2.254)
- 192.168.2.* (will send a discovery packet for the range 192.168.2.1 - 192.168.2.254)
- 192.168.2.8-* (will send a discovery packet for the range 192.168.2.8 - 192.168.2.254)
- 192.168.2.37-42 (will send a discovery packet for the range 192.168.2.37 - 192.168.2.42)
DiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery. WhenDiscoveryHandler.discoveryError(String)is invoked, the discovery will be canceled andDiscoveryHandler.discoveryFinished()will not be invoked.
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).subnetRange- the subnet search range.waitForResponsesTimeout- time to wait, in milliseconds, before determining that there are no more discovery responses.- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
directedBroadcast
public static void directedBroadcast(DiscoveryHandler discoveryHandler, String ipAddress) throws DiscoveryException Sends a directed broadcast discovery packet to the subnet specified byipAddress. Directed broadcasts are defined by the first three subnet octets, followed by 255, such as 192.168.2.255. This method accepts IP addresses of the form, assuming a subnet of 192.168.2:- 192.168.2.255
- 192.168.2.1 (last octet will be replaced with 255)
- 192.168.2 (will append 255 for the last octet)
- 192.168.2. (will append 255 for the last octet)
This overload uses a default timeout of 6000 milliseconds (6 seconds).
This method will invoke theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery. WhenDiscoveryHandler.discoveryError(String)is invoked, the discovery will be canceled andDiscoveryHandler.discoveryFinished()will not be invoked.
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).ipAddress- the IP address of the subnet.- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
directedBroadcast
public static void directedBroadcast(DiscoveryHandler discoveryHandler, String ipAddress, int waitForResponsesTimeout) throws DiscoveryException Sends a directed broadcast discovery packet to the subnet specified byipAddress. This method will wait up towaitForResponsesTimeoutmilliseconds determining that there are no more discovery responses. Directed broadcasts are defined by the first three subnet octets, followed by 255, such as 192.168.2.255. This method accepts IP addresses of the form, assuming a subnet of 192.168.2:- 192.168.2.255
- 192.168.2.1 (last octet will be replaced with 255)
- 192.168.2 (will append 255 for the last octet)
- 192.168.2. (will append 255 for the last octet)
DiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)when any errors are encountered during discovery. WhenDiscoveryHandler.discoveryError(String)is invoked, the discovery will be canceled andDiscoveryHandler.discoveryFinished()will not be invoked.
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).ipAddress- the IP address of the subnet.waitForResponsesTimeout- time to wait, in milliseconds, before determining that there are no more discovery responses.- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
localBroadcast
Sends a local broadcast packet. This method will invoke theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery. WhenDiscoveryHandler.discoveryError(String)is invoked, the discovery will be canceled andDiscoveryHandler.discoveryFinished()will not be invoked.
This overload uses a default timeout of 6000 milliseconds (6 seconds).
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
localBroadcast
public static void localBroadcast(DiscoveryHandler discoveryHandler, int waitForResponsesTimeout) throws DiscoveryException Sends a local broadcast packet. This method will wait up towaitForResponsesTimeoutmilliseconds determining that there are no more discovery responses.
This method will invoke theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery. WhenDiscoveryHandler.discoveryError(String)is invoked, the discovery will be canceled andDiscoveryHandler.discoveryFinished()will not be invoked.
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).waitForResponsesTimeout- time to wait, in milliseconds, before determining that there are no more discovery responses.- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
multicast
Sends a multicast discovery packet. This method will invoke theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery. WhenDiscoveryHandler.discoveryError(String)is invoked, the discovery will be canceled andDiscoveryHandler.discoveryFinished()will not be invoked.
This overload uses a default timeout of 6000 milliseconds (6 seconds).
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.
Note: For Android users : some extra code is required to obtain a multicast lock. See the example inmulticast(DiscoveryHandler, int, int):- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).hops- number of hops.- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
multicast
public static void multicast(DiscoveryHandler discoveryHandler, int hops, int waitForResponsesTimeout) throws DiscoveryException Sends a multicast discovery packet. This method will wait up towaitForResponsesTimeoutmilliseconds determining that there are no more discovery responses.
This method will invoke theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery. WhenDiscoveryHandler.discoveryError(String)is invoked, the discovery will be canceled andDiscoveryHandler.discoveryFinished()will not be invoked.
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.
Note: For Android users : some extra code is required to obtain a multicast lock. See following example:package test.zebra.sdk.printer.discovery.examples; import android.app.Activity; import android.content.Context; import android.net.wifi.WifiManager; import android.net.wifi.WifiManager.MulticastLock; import android.os.Bundle; 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 NetworkDiscovererAndroidMulticastExample extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int multicastHops = 5; WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); MulticastLock lock = wifi.createMulticastLock("wifi_multicast_lock"); lock.setReferenceCounted(true); lock.acquire(); try { NetworkDiscoverer.multicast(new DiscoveryHandler() { public void foundPrinter(DiscoveredPrinter printer) { System.out.println("Found a printer with address : " + printer.address); } public void discoveryFinished() { System.out.println("Discovery finished"); } public void discoveryError(String message) { System.out.println("Discovery error! - " + message); } }, multicastHops); } catch (DiscoveryException e) { e.printStackTrace(); } lock.release(); } }- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).hops- number of hops.waitForResponsesTimeout- time to wait, in milliseconds, before determining that there are no more discovery responses.- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
ipv6Multicast
public static void ipv6Multicast(DiscoveryHandler discoveryHandler, int hops) throws DiscoveryException Sends an IPv6 multicast discovery packet. This method will invoke theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished, andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery. WhenDiscoveryHandler.discoveryError(String)is invoked, the discovery will be canceled andDiscoveryHandler.discoveryFinished()will not be invoked.
This overload uses a default timeout of 6000 milliseconds (6 seconds).
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.
- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g., found a printer, errors, discovery finished).hops- number of hops.- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
ipv6Multicast
public static void ipv6Multicast(DiscoveryHandler discoveryHandler, int hops, int waitForResponsesTimeout) throws DiscoveryException Sends an IPv6 multicast discovery packet. This method will wait up towaitForResponsesTimeoutmilliseconds determining that there are no more discovery responses.
This method will invoke theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery. WhenDiscoveryHandler.discoveryError(String)is invoked, the discovery will be canceled andDiscoveryHandler.discoveryFinished()will not be invoked.
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.
- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).hops- number of hops.waitForResponsesTimeout- time to wait, in milliseconds, before determining that there are no more discovery responses.- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
findPrinters
This method will search the network using a combination of discovery methods to find printers on the network. This is a convenience method that can be used as an alternative to the other discovery methods (e.g. multicast)
This method will invoke theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery. WhenDiscoveryHandler.discoveryError(String)is invoked, the discovery will be canceled andDiscoveryHandler.discoveryFinished()will not be invoked.
This overload uses a default timeout of 6000 milliseconds (6 seconds).
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.
Since findPrinters() sends multicast request in the combination of discovery methods MulticastLock is required on Android before calling this API..
Sample Code as follows:import android.content.Context; import android.net.wifi.WifiManager; import android.net.wifi.WifiManager.MulticastLock; import android.os.Bundle; import com.zebra.android.devdemo.util.UIHelper; import com.zebra.sdk.printer.discovery.DiscoveryException; import com.zebra.sdk.printer.discovery.NetworkDiscoverer; public class FindPrintersDiscoveryResultList extends DiscoveryResultList { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); MulticastLock lock = wifi.createMulticastLock("wifi_multicast_lock"); lock.setReferenceCounted(true); lock.acquire(); NetworkDiscoverer.findPrinters(this); lock.release(); } catch (DiscoveryException e) { new UIHelper(this).showErrorDialog(e.getMessage()); } } }- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
findPrinters
public static void findPrinters(DiscoveryHandler discoveryHandler, List<String> printersToFind) throws DiscoveryException Sends a discovery request to the list of printer DNS names or IPs inprintersToFind. This method will invoke theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery.
This overload uses a default timeout of 6000 milliseconds (6 seconds).
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).printersToFind- A list of IP addresses or DNS names for the printers to be discovered.- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-
findPrinters
public static void findPrinters(DiscoveryHandler discoveryHandler, List<String> printersToFind, int waitForResponsesTimeout) throws DiscoveryException Sends a discovery request to the list of printer DNS names or IPs inprintersToFind. This method will invoke theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method for each printer that is found during discovery.DiscoveryHandler.discoveryFinished()will be invoked when the discovery is finished andDiscoveryHandler.discoveryError(String)will be invoked when any errors are encountered during discovery.
If a printer responds to the discovery request more than once, theDiscoveryHandler.foundPrinter(DiscoveredPrinter)method will only be invoked on the first occurrence.- Parameters:
discoveryHandler- aDiscoveryHandlerinstance that is used to handle discovery events (e.g. found a printer, errors, discovery finished).printersToFind- A list of IP addresses or DNS names for the printers to be discovered.waitForResponsesTimeout- time to wait, in milliseconds, before determining that there are no more discovery responses.- Throws:
DiscoveryException- if an error occurs while starting the discovery (errors during discovery will be sent viaDiscoveryHandler.discoveryError(String)).
-