上篇文章,我們已經開始進行代碼編程,主要目的就是枚舉出連接到USBHost上的HID設備,但是在實際開發中,在使用manager.getDeviceList()總是獲取不到設備列表。經過一番查找資料,現在終于把問題解決了,在此記錄一下,以備不時之需。
1、創建 android.hardware.usb.host.xml,內容為:
<permissions>
<feature name="android.hardware.usb.host"/>
</permissions>
將該文件push到/system/etc/permissions目錄下。
2、在/system/etc/permissions下的handheld_core_hardware.xml或者tablet_core_hardware.xml文件的
[html] view plaincopyprint?
<feature name="android.hardware.usb.host" />
重啟設備
3、修改AndroidManifest.xml文件,添加以下權限(很關鍵):
[html] view plaincopyprint?
<uses-permission android:name="android.hardware.usb.host" />
<uses-permission android:name="android.hardware.usb.accessory" />
4、枚舉設備的代碼例子:
[html] view plaincopyprint?
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());
}
到此為止,終于看到連接到Android平板上的設備了。