2

Samsung S4 デバイスでは動作するが、MotoG、Samsung 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 に感謝します (Samsung デバイスの場合)。

USB ホスト API サポートを有効にするには、次の行を含むファイル (xml ファイル) を追加する必要があります。

<permissions>
 <feature name="android.hardware.usb.host"/>
</permissions>

into folder(ファイルの場所)

/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

Android ドキュメント:

getDeviceList()

現在接続されているすべての 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 に答える