Android USB Host开发之manager.getDeviceList()获取不到设备列表
同样遇到这样的问题,我的Android设备是原道N12C,官方的4.0.3系统,遇到这个问题,后来找了半天找到的,现在汇总一下吧:
1、创建 android.hardware.usb.host.xml,内容为:
- <permissions>
- <feature name="android.hardware.usb.host"/>
- </permissions>
<permissions>
<feature name="android.hardware.usb.host"/>
</permissions>
将该文件push到/system/etc/permissions目录下
2、在/system/etc/permissions下的handheld_core_hardware.xml或者tablet_core_hardware.xml文件的<permissions>段中添加:(platform.xml)
- <feature name="android.hardware.usb.host" />
<feature name="android.hardware.usb.host" />
重启设备
3、修改AndroidManifest.xml文件,添加以下权限:
- <uses-permission android:name="android.hardware.usb.host" />
<uses-permission android:name="android.hardware.usb.host" />
- UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
- HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
- Log.e(TAG, "get device list = " + deviceList.size());
- Toast.makeText(this, "get device list = " + deviceList.size(), 200).show();
- Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
- while (deviceIterator.hasNext()) {
- UsbDevice device = deviceIterator.next();
- Log.e(TAG, "device name = " + device.getDeviceName());
- }
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Log.e(TAG, "get device list = " + deviceList.size());
Toast.makeText(this, "get device list = " + deviceList.size(), 200).show();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext()) {
UsbDevice device = deviceIterator.next();
Log.e(TAG, "device name = " + device.getDeviceName());
}
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