public class NetworkDiscoverer
extends Object
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();
}
}
}
Modifier and Type | Method and Description |
---|---|
static void |
directedBroadcast(DiscoveryHandler discoveryHandler,
String ipAddress)
Sends a directed broadcast discovery packet to the subnet specified by
ipAddress . |
static void |
directedBroadcast(DiscoveryHandler discoveryHandler,
String ipAddress,
int waitForResponsesTimeout)
Sends a directed broadcast discovery packet to the subnet specified by
ipAddress . |
static void |
findPrinters(DiscoveryHandler discoveryHandler)
This method will search the network using a combination of discovery methods to find printers on the network.
|
static void |
findPrinters(DiscoveryHandler discoveryHandler,
java.util.List<String> printersToFind)
Sends a discovery request to the list of printer DNS names or IPs in
printersToFind . |
static void |
findPrinters(DiscoveryHandler discoveryHandler,
java.util.List<String> printersToFind,
int waitForResponsesTimeout)
Sends a discovery request to the list of printer DNS names or IPs in
printersToFind . |
static void |
localBroadcast(DiscoveryHandler discoveryHandler)
Sends a local broadcast packet.
|
static void |
localBroadcast(DiscoveryHandler discoveryHandler,
int waitForResponsesTimeout)
Sends a local broadcast packet.
|
static void |
multicast(DiscoveryHandler discoveryHandler,
int hops)
Sends a multicast discovery packet.
|
static void |
multicast(DiscoveryHandler discoveryHandler,
int hops,
int waitForResponsesTimeout)
Sends a multicast discovery packet.
|
static void |
subnetSearch(DiscoveryHandler discoveryHandler,
String subnetRange)
Sends a discovery packet to the IPs specified in the
subnetRange . |
static void |
subnetSearch(DiscoveryHandler discoveryHandler,
String subnetRange,
int waitForResponsesTimeout)
Sends a discovery packet to the IPs specified in the
subnetRange . |
public static void subnetSearch(DiscoveryHandler discoveryHandler, String subnetRange) throws DiscoveryException
subnetRange
. 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:
DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method for each printer that
is found during discovery. DiscoveryHandler.discoveryFinished()
will be invoked when the discovery is
finished and DiscoveryHandler.discoveryError(String)
will be invoked when any errors are encountered
during discovery. When DiscoveryHandler.discoveryError(String)
is invoked, the discovery will be canceled
and DiscoveryHandler.discoveryFinished()
will not be invoked. DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method will only be invoked on the first occurrence.discoveryHandler
- a DiscoveryHandler
instance that is used to handle discovery events (e.g. found a
printer, errors, discovery finished).subnetRange
- the subnet search range.DiscoveryException
- if an error occurs while starting the discovery (errors during discovery will be sent
via DiscoveryHandler.discoveryError(String)
).public static void subnetSearch(DiscoveryHandler discoveryHandler, String subnetRange, int waitForResponsesTimeout) throws DiscoveryException
subnetRange
. This method will wait up to
waitForResponsesTimeout
milliseconds 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:
DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method for each printer that
is found during discovery. DiscoveryHandler.discoveryFinished()
will be invoked when the discovery is
finished and DiscoveryHandler.discoveryError(String)
will be invoked when any errors are encountered
during discovery. When DiscoveryHandler.discoveryError(String)
is invoked, the discovery will be canceled
and DiscoveryHandler.discoveryFinished()
will not be invoked.DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method will only be invoked on the first occurrence.discoveryHandler
- a DiscoveryHandler
instance 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.DiscoveryException
- if an error occurs while starting the discovery (errors during discovery will be sent
via DiscoveryHandler.discoveryError(String)
).public static void directedBroadcast(DiscoveryHandler discoveryHandler, String ipAddress) throws DiscoveryException
ipAddress
. 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:
DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method for each printer that
is found during discovery. DiscoveryHandler.discoveryFinished()
will be invoked when the discovery is
finished and DiscoveryHandler.discoveryError(String)
will be invoked when any errors are encountered
during discovery. When DiscoveryHandler.discoveryError(String)
is invoked, the discovery will be canceled
and DiscoveryHandler.discoveryFinished()
will not be invoked.DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method will only be invoked on the first occurrence.discoveryHandler
- a DiscoveryHandler
instance that is used to handle discovery events (e.g. found a
printer, errors, discovery finished).ipAddress
- the IP address of the subnet.DiscoveryException
- if an error occurs while starting the discovery (errors during discovery will be sent
via DiscoveryHandler.discoveryError(String)
).public static void directedBroadcast(DiscoveryHandler discoveryHandler, String ipAddress, int waitForResponsesTimeout) throws DiscoveryException
ipAddress
. This method will
wait up to waitForResponsesTimeout
milliseconds 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:
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 and DiscoveryHandler.discoveryError(String)
when any errors are encountered during
discovery. When DiscoveryHandler.discoveryError(String)
is invoked, the discovery will be canceled and
DiscoveryHandler.discoveryFinished()
will not be invoked.DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method will only be invoked on the first occurrence.discoveryHandler
- a DiscoveryHandler
instance 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.DiscoveryException
- if an error occurs while starting the discovery (errors during discovery will be sent
via DiscoveryHandler.discoveryError(String)
).public static void localBroadcast(DiscoveryHandler discoveryHandler) throws DiscoveryException
DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method for each printer that is found during discovery.
DiscoveryHandler.discoveryFinished()
will be invoked when the discovery is finished and
DiscoveryHandler.discoveryError(String)
will be invoked when any errors are encountered during discovery.
When DiscoveryHandler.discoveryError(String)
is invoked, the discovery will be canceled and
DiscoveryHandler.discoveryFinished()
will not be invoked.DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method will only be invoked on the first occurrence.discoveryHandler
- a DiscoveryHandler
instance that is used to handle discovery events (e.g. found a
printer, errors, discovery finished).DiscoveryException
- if an error occurs while starting the discovery (errors during discovery will be sent
via DiscoveryHandler.discoveryError(String)
).public static void localBroadcast(DiscoveryHandler discoveryHandler, int waitForResponsesTimeout) throws DiscoveryException
waitForResponsesTimeout
milliseconds
determining that there are no more discovery responses. DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method for each printer that
is found during discovery. DiscoveryHandler.discoveryFinished()
will be invoked when the discovery is
finished and DiscoveryHandler.discoveryError(String)
will be invoked when any errors are encountered
during discovery. When DiscoveryHandler.discoveryError(String)
is invoked, the discovery will be canceled
and DiscoveryHandler.discoveryFinished()
will not be invoked.DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method will only be invoked on the first occurrence.discoveryHandler
- a DiscoveryHandler
instance 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.DiscoveryException
- if an error occurs while starting the discovery (errors during discovery will be sent
via DiscoveryHandler.discoveryError(String)
).public static void multicast(DiscoveryHandler discoveryHandler, int hops) throws DiscoveryException
DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method for each printer that is found during discovery.
DiscoveryHandler.discoveryFinished()
will be invoked when the discovery is finished and
DiscoveryHandler.discoveryError(String)
will be invoked when any errors are encountered during discovery.
When DiscoveryHandler.discoveryError(String)
is invoked, the discovery will be canceled and
DiscoveryHandler.discoveryFinished()
will not be invoked.DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method will only be invoked on the first occurrence. multicast(DiscoveryHandler, int, int)
:discoveryHandler
- a DiscoveryHandler
instance that is used to handle discovery events (e.g. found a
printer, errors, discovery finished).hops
- number of hops.DiscoveryException
- if an error occurs while starting the discovery (errors during discovery will be sent
via DiscoveryHandler.discoveryError(String)
).public static void multicast(DiscoveryHandler discoveryHandler, int hops, int waitForResponsesTimeout) throws DiscoveryException
waitForResponsesTimeout
milliseconds
determining that there are no more discovery responses. DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method for each printer that
is found during discovery. DiscoveryHandler.discoveryFinished()
will be invoked when the discovery is
finished and DiscoveryHandler.discoveryError(String)
will be invoked when any errors are encountered
during discovery. When DiscoveryHandler.discoveryError(String)
is invoked, the discovery will be canceled
and DiscoveryHandler.discoveryFinished()
will not be invoked.DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method will only be invoked on the first occurrence.
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();
}
}
discoveryHandler
- a DiscoveryHandler
instance 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.DiscoveryException
- if an error occurs while starting the discovery (errors during discovery will be sent
via DiscoveryHandler.discoveryError(String)
).public static void findPrinters(DiscoveryHandler discoveryHandler) throws DiscoveryException
DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method for each printer that
is found during discovery. DiscoveryHandler.discoveryFinished()
will be invoked when the discovery is
finished and DiscoveryHandler.discoveryError(String)
will be invoked when any errors are encountered
during discovery. When DiscoveryHandler.discoveryError(String)
is invoked, the discovery will be canceled
and DiscoveryHandler.discoveryFinished()
will not be invoked.DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method will only be invoked on the first occurrence. discoveryHandler
- a DiscoveryHandler
instance that is used to handle discovery events (e.g. found a
printer, errors, discovery finished).DiscoveryException
- if an error occurs while starting the discovery (errors during discovery will be sent
via DiscoveryHandler.discoveryError(String)
).public static void findPrinters(DiscoveryHandler discoveryHandler, java.util.List<String> printersToFind) throws DiscoveryException
printersToFind
. This method
will invoke the DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method for each printer that is found
during discovery. DiscoveryHandler.discoveryFinished()
will be invoked when the discovery is finished and
DiscoveryHandler.discoveryError(String)
will be invoked when any errors are encountered during discovery. DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method will only be invoked on the first occurrence.discoveryHandler
- a DiscoveryHandler
instance 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.DiscoveryException
- if an error occurs while starting the discovery (errors during discovery will be sent
via DiscoveryHandler.discoveryError(String)
).public static void findPrinters(DiscoveryHandler discoveryHandler, java.util.List<String> printersToFind, int waitForResponsesTimeout) throws DiscoveryException
printersToFind
. This method
will invoke the DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method for each printer that is found
during discovery. DiscoveryHandler.discoveryFinished()
will be invoked when the discovery is finished and
DiscoveryHandler.discoveryError(String)
will be invoked when any errors are encountered during discovery. DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method will only be invoked on the first occurrence.discoveryHandler
- a DiscoveryHandler
instance 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.DiscoveryException
- if an error occurs while starting the discovery (errors during discovery will be sent
via DiscoveryHandler.discoveryError(String)
).
© 2017 ZIH Corp. All Rights Reserved.