getDeviceList() 总是在 Android 中给出空值

getDeviceList() always gives empty in Android

在 Samsung S4 设备上工作,但在 MotoG、Samsung A5(2016) 上不工作

下面的代码总是 return 零,我尝试在清单文件中授予权限,但它仍然 return 无效。任何人都可以给我任何建议吗

在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());

        }
    }

}

舱单代码:

<?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() 总是 return 零 任何人都可以给我任何建议吗? 日志信息如下:

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{ }

请给我建议.. 提前致谢

Android Docs:

getDeviceList()

Returns a HashMap containing all USB devices currently attached. USB device name is the key for the returned HashMap. The result will be empty if no devices are attached, or if USB host mode is inactive or unsupported.

尝试将此添加到您的清单文件中。

<uses-feature
    android:name="android.hardware.usb.host"
    android:required="true"/>

提供了有关如何检查设备的 usbhost 功能的详细信息。

它对我来说很好(对于三星设备)感谢@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 设备仍然没有检测到