Android USB Host開發之manager.getDeviceList()擷取不到裝置列表

來源:互聯網
上載者:User

同樣遇到這樣的問題,我的Android裝置是原道N12C,官方的4.0.3系統,遇到這個問題,後來找了半天找到的,現在匯總一下吧:

1、建立 android.hardware.usb.host.xml,內容為:


[html] 
<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>段中添加:


[html] 
<feature name="android.hardware.usb.host"> 

<feature name="android.hardware.usb.host">重啟裝置


3、修改AndroidManifest.xml檔案,添加以下許可權:


[html]  ="android.hardware.usb.host" /> 

<uses-permission android:name="android.hardware.usb.host" />

 

4、枚舉裝置的代碼例子:
[html] 
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());
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.