public class UrlPrinterDiscoverer extends Object
Modifier and Type | Method and Description |
---|---|
static void |
findPrinters(String url,
DiscoveryHandler discoveryHandler)
This method will search using a combination of discovery methods to find the printer described by the specified
URL.
|
public static void findPrinters(String url, DiscoveryHandler discoveryHandler) throws DiscoveryException
DiscoveryHandler.foundPrinter(DiscoveredPrinter)
method for each
interface that the specified printer is found. 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.<uses-permission android:name="android.permission.NFC" />
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" android:host="zebra.com" android:path="/apps/r/nfc" />
<data android:scheme="http" android:host="www.zebra.com" android:path="/apps/r/nfc" />
</intent-filter>
</activity>
package test.zebra.sdk.printer.discovery.examples;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.AsyncTask;
import android.os.Parcelable;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.printer.ZebraPrinterFactory;
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.UrlPrinterDiscoverer;
public class UrlPrinterDiscovererExample extends Activity {
protected void onResume() {
super.onResume();
processNfcScan(getIntent());
}
private void processNfcScan(Intent intent) {
Parcelable[] scannedTags = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (scannedTags != null && scannedTags.length > 0) {
NdefMessage msg = (NdefMessage) scannedTags[0];
final String payload = new String(msg.getRecords()[0].getPayload());
new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... params) {
try {
UrlPrinterDiscoverer.findPrinters(payload, new DiscoveryHandler() {
public void foundPrinter(DiscoveredPrinter printer) {
try {
Connection conn = printer.getConnection();
conn.open();
ZebraPrinterFactory.getInstance(conn).printConfigurationLabel();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void discoveryFinished() {
}
public void discoveryError(String message) {
}
});
} catch (DiscoveryException e) {
e.printStackTrace();
}
return null;
}
}.execute((Void) null);
intent.removeExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
}
}
}
url
- The URL describing the targeted printer (Typically, this information is encoded on an NFC tag attached
to the printer)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)
).
© 2015 ZIH Corp. All Rights Reserved.