0. 前言
在做 USB OTG 通信时,第一步就是要能够获取到 usb 的使用权限,因此特地在此处介绍一下两种我用过的获取 usb 权限方式。
1. 直接在 AndroidManifest.xml 中配置
这种配置方式是在 github 上 usb-serial-for-android 项目中看到的,大家如果有兴趣可以 clone 下来研究一下。
<activity
android:name=".DeviceListActivity"android:label="@string/app_name"android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"><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>
其中 device_filter.xml 中列出了可用 usb 设备,当usb 设备连接手机之后,app 会自动询问是否允许获取该 usb 的权限。
device_filter.xml 放置位置如下图所示
内容为
<?xml version="1.0" encoding="utf-8"?>
<resources><!-- 0x0403 / 0x6001: FTDI FT232R UART --><usb-device vendor-id="1027" product-id="24577" /><!-- 0x0403 / 0x6015: FTDI FT231X --><usb-device vendor-id="1027" product-id="24597" /><!-- 0x2341 / Arduino --><usb-device vendor-id="9025" /><!-- 0x16C0 / 0x0483: Teensyduino --><usb-device vendor-id="5824" product-id="1155" /><!-- 0x10C4 / 0xEA60: CP210x UART Bridge --><usb-device vendor-id="4292" product-id="60000" /><!-- 0x067B / 0x2303: Prolific PL2303 --><usb-device vendor-id="1659" product-id="8963" /><!-- 0x1a86 / 0x7523: Qinheng CH340 --><usb-device vendor-id="6790" product-id="29987" />
</resources>
每个 usb 设备通过 vendor-id(厂商 id) 和 product-id (产品 id)一起来定义的,这里有一个 linux 的 usb设备厂商 id 和产品 id 的汇总,可以作为 Android usb 设备的参考。
2. 在代码中获取
这代码是在其他博客中看到的,忘记是哪篇了,这里就不引用了。直接上代码。
/*** 获得 usb 权限*/private void openUsbDevice(){//before open usb device//should try to get usb permissiontryGetUsbPermission();}UsbManager mUsbManager;private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";private void tryGetUsbPermission(){mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);registerReceiver(mUsbPermissionActionReceiver, filter);PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);//here do emulation to ask all connected usb device for permissionfor (final UsbDevice usbDevice : mUsbManager.getDeviceList().values()) {//add some conditional check if necessary//if(isWeCaredUsbDevice(usbDevice)){if(mUsbManager.hasPermission(usbDevice)){//if has already got permission, just goto connect it//that means: user has choose yes for your previously popup window asking for grant perssion for this usb device//and also choose option: not ask againafterGetUsbPermission(usbDevice);}else{//this line will let android popup window, ask user whether to allow this app to have permission to operate this usb devicemUsbManager.requestPermission(usbDevice, mPermissionIntent);}//}}}private void afterGetUsbPermission(UsbDevice usbDevice){//call method to set up device communication//Toast.makeText(this, String.valueOf("Got permission for usb device: " + usbDevice), Toast.LENGTH_LONG).show();//Toast.makeText(this, String.valueOf("Found USB device: VID=" + usbDevice.getVendorId() + " PID=" + usbDevice.getProductId()), Toast.LENGTH_LONG).show();doYourOpenUsbDevice(usbDevice);}private void doYourOpenUsbDevice(UsbDevice usbDevice){//now follow line will NOT show: User has not given permission to device UsbDeviceUsbDeviceConnection connection = mUsbManager.openDevice(usbDevice);//add your operation code here}private final BroadcastReceiver mUsbPermissionActionReceiver = new BroadcastReceiver() {public void onReceive(Context context, Intent intent) {String action = intent.getAction();if (ACTION_USB_PERMISSION.equals(action)) {synchronized (this) {UsbDevice usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {//user choose YES for your previously popup window asking for grant perssion for this usb deviceif(null != usbDevice){afterGetUsbPermission(usbDevice);}}else {//user choose NO for your previously popup window asking for grant perssion for this usb deviceToast.makeText(context, String.valueOf("Permission denied for device" + usbDevice), Toast.LENGTH_LONG).show();}}}}};