public class UrlPrinterDiscoverer
extends Object
Modifier and Type | Method and Description |
---|---|
static void |
findPrinters(String url,
DiscoveryHandler discoveryHandler)
Deprecated.
|
static void |
findPrinters(String url,
DiscoveryHandler discoveryHandler,
android.content.Context context)
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
findPrinters(String, DiscoveryHandler, Context)
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 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;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.AsyncTask;
import android.os.Parcelable;
public class UrlPrinterDiscovererExample extends Activity {
protected void onResume() {
super.onResume();
processNfcScan(getIntent(), this.getApplicationContext());
}
private void processNfcScan(Intent intent, final Context context) {
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) {
}
}, context);
} 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)
).public static void findPrinters(String url, DiscoveryHandler discoveryHandler, android.content.Context context) 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 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;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.AsyncTask;
import android.os.Parcelable;
public class UrlPrinterDiscovererExample extends Activity {
protected void onResume() {
super.onResume();
processNfcScan(getIntent(), this.getApplicationContext());
}
private void processNfcScan(Intent intent, final Context context) {
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) {
}
}, context);
} 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).context
- Android application context.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.