public class UsbDiscoverer extends Object
package test.zebra.sdk.printer.discovery.examples;
import java.util.LinkedList;
import java.util.List;
import android.R;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.printer.discovery.DiscoveredPrinter;
import com.zebra.sdk.printer.discovery.DiscoveredPrinterUsb;
import com.zebra.sdk.printer.discovery.DiscoveryHandler;
import com.zebra.sdk.printer.discovery.UsbDiscoverer;
public class UsbDiscoveryAndPrintExample extends Activity {
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
PendingIntent mPermissionIntent;
boolean hasPermissionToCommunicate = false;
UsbManager mUsbManager;
Button buttonRequestPermission;
Button buttonPrint;
DiscoveredPrinterUsb discoveredPrinterUsb;
// Catches intent indicating if the user grants permission to use the USB device
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
hasPermissionToCommunicate = true;
}
}
}
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
// Register broadcast receiver that catches USB permission intent
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
buttonRequestPermission = (Button) findViewById(R.id.button1);
buttonPrint = (Button) findViewById(R.id.button2);
// Request Permission button click
buttonRequestPermission.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
// Find connected printers
UsbDiscoveryHandler handler = new UsbDiscoveryHandler();
UsbDiscoverer.findPrinters(getApplicationContext(), handler);
try {
if (handler.printers != null && handler.printers.size() > 0)
{
discoveredPrinterUsb = handler.printers.get(0);
mUsbManager.requestPermission(discoveredPrinterUsb.device, mPermissionIntent);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage() + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
}
}).start();
}
});
// Print button click
buttonPrint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (hasPermissionToCommunicate)
{
try {
Connection conn = discoveredPrinterUsb.getConnection();
conn.open();
conn.write("~WC".getBytes());
conn.close();
} catch (ConnectionException e) {
Toast.makeText(getApplicationContext(), e.getMessage() + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "No permission to communicate", Toast.LENGTH_LONG).show();
}
}
});
}
// Handles USB device discovery
class UsbDiscoveryHandler implements DiscoveryHandler {
public List<DiscoveredPrinterUsb> printers;
public UsbDiscoveryHandler() {
printers = new LinkedList<DiscoveredPrinterUsb>();
}
public void foundPrinter(final DiscoveredPrinter printer) {
printers.add((DiscoveredPrinterUsb) printer);
}
public void discoveryFinished() {
}
public void discoveryError(String message) {
}
}
}
Constructor and Description |
---|
UsbDiscoverer() |
Modifier and Type | Method and Description |
---|---|
static void |
findPrinters(android.content.Context context,
DiscoveryHandler discoveryHandler)
Initiate discovery
|
static void |
findPrinters(android.hardware.usb.UsbManager usbService,
DiscoveryHandler discoveryHandler) |
static boolean |
isZebraUsbDevice(android.hardware.usb.UsbDevice device) |
public static void findPrinters(android.content.Context context, DiscoveryHandler discoveryHandler)
context
- discoveryHandler
- public static void findPrinters(android.hardware.usb.UsbManager usbService, DiscoveryHandler discoveryHandler)
usbService
- discoveryHandler
- public static boolean isZebraUsbDevice(android.hardware.usb.UsbDevice device)
device
-
© 2015 ZIH Corp. All Rights Reserved.