- 获取权限
<uses-permission android:name="android.permission.USB_PERMISSION" />
<uses-feature android:name="android.hardware.usb.host" />
<uses-feature
android:name="android.hardware.usb.accessory"
android:required="false" />
- 注册广播
private BroadcastReceiver initUsbReceiver() {
return new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION2.equals(action)) {
synchronized (this) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
}
}
}
}
}
};
}
- 通用方法
private static UsbInterface usbInterface = null;
private Activity mContext;
private static UsbDeviceConnection connection;
public List<UsbDevice> getUsbDeviceList() {
if (usbManager == null) {
intManage();
}
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
if (deviceList.size() == 0) {
CustomToast.showCustomToast(mContext, "暂无可连接USB设备");
return null;
}
List<UsbDevice> usbDeviceList = new ArrayList<>();
for (UsbDevice device : deviceList.values()) {
if (device != null) {
usbDeviceList.add(device);
}
}
return usbDeviceList;
}
**
* 连接usbDevice
*
* @param usbDevice
*/
public void connectDevice(UsbDevice usbDevice, CompleteLisenter lisenter) {
if (connection != null) {
close(false);
}
if (usbDevice == null) {
Log.i(TAG, "connectDevice:该USB设备不可用 ");
CustomToast.showCustomToast(mContext, "该USB设备不可用");
return;
}
showProgress("正在连接...");
connection = usbManager.openDevice(usbDevice);
if (connection == null) {
lisenter.onError("连接失败");
return;
}
UsbPrintFactory.setCurUsbDevice(usbDevice);
connection = usbManager.openDevice(usbDevice);
usbInterface = usbDevice.getInterface(0);
UsbEndpoint endpoint = usbInterface.getEndpoint(0);
Log.i(TAG, "connectDevice: 已连接USB设备,设备vid: " + usbDevice.getVendorId() + ", pid: " + usbDevice.getProductId());
dismissProgress();
CustomToast.showCustomToast(mContext, "连接成功");
lisenter.onComplete("连接成功");
}
- 打印
public void printData(List<String> infos, CompleteLisenter lisenter) {
UsbDevice usbDevice = UsbPrintFactory.getCurUsbDevice();
List<UsbDevice> list = getUsbDeviceList();
if (usbDevice == null) {
CustomToast.showCustomToast(mContext, "还未连接设备,请先连接设备");
return;
}
if (!list.contains(usbDevice)) {
CustomToast.showCustomToast(mContext, "还未连接设备,请先连接设备");
UsbPrintFactory.setCurUsbDevice(null);
return;
}
if(connection == null){
connection = usbManager.openDevice(usbDevice);
usbInterface = usbDevice.getInterface(0);
UsbEndpoint endpoint = usbInterface.getEndpoint(0);
}
connection.claimInterface(usbInterface, true);
for (int i = 0; i < infos.size(); i++) {
String str = "^XA\r\n" +
"^PW1000\r\n" +
"^LL800\r\n" +
"^LH0,0\r\n" +
"^MD10\r\n" +
"^FO50,50^GB700,200,9^FS\r\n" +
"^XZ";
byte[] data = str.getBytes();
connection.bulkTransfer(endpoint, data, data.length, 60000);
}
dismissProgress();
lisenter.onComplete("");
CustomToast.showCustomToast(mContext, "打印成功");
}
- 关闭连接
public void close(Boolean showResult) {
if (connection != null) {
connection.releaseInterface(usbInterface);
connection.close();
connection = null;
UsbPrintFactory.setCurUsbDevice(null);
if (showResult) {
CustomToast.showCustomToast(mContext, "已断开连接");
}
}
}