public class UsbDiscoverer
extends Object
package test.zebra.sdk.printer.discovery.examples;
import java.util.*;
import com.zebra.sdk.comm.*;
import com.zebra.sdk.printer.discovery.*;
import android.R;
import android.app.*;
import android.content.*;
import android.hardware.usb.*;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class UsbDiscoveryAndPrintExample extends Activity {
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
private PendingIntent mPermissionIntent;
private boolean hasPermissionToCommunicate = false;
private UsbManager mUsbManager;
private Button buttonRequestPermission;
private Button buttonPrint;
private 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);
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 {
while (!handler.discoveryComplete) {
Thread.sleep(100);
}
if (handler.printers != null && handler.printers.size() > 0) {
discoveredPrinterUsb = handler.printers.get(0);
if (!mUsbManager.hasPermission(discoveredPrinterUsb.device)) {
mUsbManager.requestPermission(discoveredPrinterUsb.device, mPermissionIntent);
} else {
hasPermissionToCommunicate = true;
}
}
} 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) {
Connection conn = null;
try {
conn = discoveredPrinterUsb.getConnection();
conn.open();
conn.write("~WC".getBytes());
} catch (ConnectionException e) {
Toast.makeText(getApplicationContext(), e.getMessage() + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
} finally {
if (conn != null) {
try {
conn.close();
} catch (ConnectionException e) {
e.printStackTrace();
}
}
}
} else {
Toast.makeText(getApplicationContext(), "No permission to communicate", Toast.LENGTH_LONG).show();
}
}
});
}
protected void onPause() {
unregisterReceiver(mUsbReceiver);
super.onPause();
}
protected void onResume() {
super.onResume();
registerReceiver(mUsbReceiver, filter);
}
// Handles USB device discovery
class UsbDiscoveryHandler implements DiscoveryHandler {
public List<DiscoveredPrinterUsb> printers;
public boolean discoveryComplete = false;
public UsbDiscoveryHandler() {
printers = new LinkedList<DiscoveredPrinterUsb>();
}
public void foundPrinter(final DiscoveredPrinter printer) {
printers.add((DiscoveredPrinterUsb) printer);
}
public void discoveryFinished() {
discoveryComplete = true;
}
public void discoveryError(String message) {
discoveryComplete = true;
}
}
}
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
-
© 2017 ZIH Corp. All Rights Reserved.