Android USB Host开发之manager.getDeviceList()获取不到设备列表

来源:互联网 发布:淘宝网商城女装秋装 编辑:程序博客网 时间:2024/06/07 07:42

同样遇到这样的问题,我的Android设备是原道N12C,官方的4.0.3系统,遇到这个问题,后来找了半天找到的,现在汇总一下吧:

1、创建 android.hardware.usb.host.xml,内容为:

[html] view plaincopyprint?
  1. <permissions>  
  2.  <feature name="android.hardware.usb.host"/>  
  3. </permissions>  
将该文件push到/system/etc/permissions目录下

2、在/system/etc/permissions下的handheld_core_hardware.xml或者tablet_core_hardware.xml文件的<permissions>段中添加:

[html] view plaincopyprint?
  1. <feature name="android.hardware.usb.host" />  
重启设备


3、修改AndroidManifest.xml文件,添加以下权限(很关键):

[html] view plaincopyprint?
  1. <uses-permission android:name="android.hardware.usb.host" />  
  2. <uses-permission android:name="android.hardware.usb.accessory" />  



4、枚举设备的代码例子:
[html] view plaincopyprint?
  1. UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);  
  2. HashMap<String, UsbDevice> deviceList = manager.getDeviceList();  
  3. Log.e(TAG, "get device list  = " + deviceList.size());  
  4. Toast.makeText(this, "get device list  = " + deviceList.size(), 200).show();  
  5. Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();  
  6. while (deviceIterator.hasNext()) {  
  7.      UsbDevice device = deviceIterator.next();  
  8.      Log.e(TAG, "device name = " + device.getDeviceName());  
  9. }  

6、相关连接:
http://stackoverflow.com/questions/11183792/android-usb-host-and-hidden-devices
http://bbs.csdn.net/topics/390008074
http://developer.android.com/guide/topics/connectivity/usb/host.html
0 0