1.首先注册一个广播:用户三种操作会导致系统发广播,插或者拔USB设备,还有就是在USB授权框点击是或者否
private void initReceiver() {//授权对话框点击操作之后会发出系统广播receiver = new BroadcastReceiver() {public void onReceive(Context context, Intent intent) {LogUtils.w(TAG,"postRequestPermission");String action = intent.getAction();if (ACTION_USB_PERMISSION.equals(intent.getAction())) {synchronized (this){UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);boolean usbPremission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);if((usbPremission) && (device != null)){closeDevice();initUsb();openUsbDevice();}else{}}}else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)){LogUtils.w(TAG,"有设备插入");UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);if(usbDevice != null){if (!mUsbManager.hasPermission(usbDevice)) {mUsbManager.requestPermission(usbDevice, mPendingIntent);}}else{}}else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)){LogUtils.w(TAG,"有设备拔出");UsbDevice usb = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);closeDevice();}}};IntentFilter ifilter = new IntentFilter(ACTION_USB_PERMISSION);ifilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);ifilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);registerReceiver(receiver, ifilter);}
2.可能在你打开你的APP之前就已经有USB打印机连接在你的平板上了,这个时候需要给他授权并打开它:
private void openConnectedDevice() {mUsbManager = ((UsbManager) this.getSystemService(Context.USB_SERVICE));mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);if(requestPermission(this) == Common.ERROR_CODE.SUCCESS) {initUsb();openUsbDevice();}}public Common.ERROR_CODE requestPermission(Context context) {LogUtils.w(TAG,"requestPermission");UsbDevice usbdev = getUsbDevice(mUsbManager);if (usbdev != null) {// get requestPermissionif (!mUsbManager.hasPermission(usbdev)) {LogUtils.w(TAG,"requestPermission 没有权限");mUsbManager.requestPermission(usbdev, mPendingIntent);return Common.ERROR_CODE.ERROR_OR_NO_ACCESS_PERMISSION;}LogUtils.w(TAG,"requestPermission 获取到了权限");return Common.ERROR_CODE.SUCCESS;}return Common.ERROR_CODE.NO_USB_DEVICE_FOUND;}
static UsbDevice getUsbDevice(UsbManager um) {HashMap<String, UsbDevice> lst = um.getDeviceList();Iterator<UsbDevice> deviceIterator = lst.values().iterator();while (deviceIterator.hasNext()) {UsbDevice dev = (UsbDevice) deviceIterator.next();LogUtils.d(TAG, "usb device : " + String.format("%1$04X:%2$04X", dev.getVendorId(), dev.getProductId()));if (dev.getVendorId() == 0x0485 ) {return dev;}if (dev.getVendorId() == 0xB000 ) {return dev;}}return null;}
给某个USB口授权很简单:
mUsbManager.requestPermission(usbdev, mPendingIntent);
调用这句代码就行,调用之后系统会弹出对话框让用户来选择是或者否,用户点击之后,系统发出action为 ACTION_USB_PERMISSION 的广播,第一步刚好注册了此action的广播来处理此操作,如果点“是”,就是打开此USB设备,如下:
closeDevice();
initUsb();
openUsbDevice();
3.第二步是考虑到在打开APP之前已经有USB设备连上Android平板,如果在打开之后插上USB设备怎么连接上呢?第一步已经说过插拔设备,系统都会发出广播,恰好第一步都注册此action的广播来处理插拔USB的操作。比方如果有设备插上,系统会发出ACTION_USB_DEVICE_ATTACHED 的广播,APP收到此广播后要对权限进行判断,如果没有权限则进行授权操作,处理如下:
else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action))
{LogUtils.w(TAG,"有设备插入");UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);if(usbDevice != null){if (!mUsbManager.hasPermission(usbDevice)) {mUsbManager.requestPermission(usbDevice, mPendingIntent);}}
}
上面这段代码在第一步中已经存在,为了方便观看,所以在这里再贴一次。
4.上面是在activity中的代码,在androidmanifest中也需要添加相应代码如下:
<activity android:name=".ui.MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><intent-filter><action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /></intent-filter><meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"android:resource="@xml/device_filter" /></activity>
<?xml version="1.0" encoding="utf-8"?><resources><usb-device vendor-id="1137" /><usb-device vendor-id="7358" />
</resources>