2

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

下面的代码总是返回零,我尝试在清单文件中授予权限,但它仍然返回 null .. 任何人都可以给我任何建议

在活动代码中:

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() 总是返回零 谁能给我任何建议?日志信息如下所示:

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

请给我建议..提前谢谢

4

2 回答 2

1

感谢@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 主机和隐藏设备

但是对于 Motog 设备仍然没有检测到

于 2016-09-13T10:23:31.457 回答
0

安卓文档

获取设备列表()

返回包含当前连接的所有 USB 设备的 HashMap。USB 设备名称是返回的 HashMap 的键。结果将是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"/>

https://stackoverflow.com/a/26728839/3225001提供了有关如何检查设备的 usbhost 功能的详细信息。

于 2016-09-12T18:24:51.767 回答