在 Samsung S4 设备上工作,但在 MotoG、Samsung A5(2016) 上不工作
下面的代码总是返回零,我尝试在 list 文件中授予权限但它仍然返回 null.. 谁能给我任何建议
在 Activity 代码中:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UsbManager usbManager = (UsbManager) getSystemService(USB_SERVICE);
HashMap<String, UsbDevice> devicelist = usbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = devicelist.values().iterator();
Log.i("List", "size =" + devicelist.size());
Log.i("List", "List" + devicelist);
while(deviceIterator.hasNext()) {
UsbDevice usbDevice = deviceIterator.next();
Log.i("List", "Model : " +usbDevice.getDeviceName());
Log.i("List", "Id : " +usbDevice.getDeviceId());
}
}
}
list 代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pathtech.usbapp" >
<permission android:name="android.hardware.usb.host"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
</activity>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</application>
</manifest>
manager.getDeviceList() 总是返回零 谁能给我任何建议? 日志信息如下:
09-12 23:26:28.692 5053-5053/com.example.usbapp I/List﹕ size =0
09-12 23:26:28.692 5053-5053/com.example.usbapp I/List﹕ List{ }
请给我建议.. 提前致谢
最佳答案
感谢@Gurupad Mamadapur,它对我来说很好(对于三星设备)
要启用 USB 主机 API 支持,您应该添加一个文件(xml 文件)并包含以下行:
<permissions>
<feature name="android.hardware.usb.host"/>
</permissions>
进入文件夹(文件位置)
/system/etc/permissions
在该文件夹中找到名为 handheld_core_hardware.xml 或 tablet_core_hardware.xml
并添加
<feature name="android.hardware.usb.host" />
和 重新启动您的设备。 USB 主机 api 应该可以工作。
来源:Android USB host and hidden devices
但是对于Motog设备还是检测不到
关于android - getDeviceList() 在 Android 中始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39456279/