简体   繁体   中英

Unable to get USB device list using usb host in Android

I want to retrieve a USB device list on my tablet.

As per the code it must launch an activity when USB is attached and even I'm checking that the device are available but not getting any list.

manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testusb1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/usbdevice" />
        </activity>
    </application>

</manifest>

java code:

package com.example.testusb1;

import java.util.HashMap;
import java.util.Iterator;

import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv1 = (TextView) findViewById(R.id.tv1);
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        String txt = "defaulr";
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

        while (deviceIterator.hasNext()) {
            UsbDevice device = deviceIterator.next();
            txt = txt + "  * " + device.getDeviceName();
        }
        tv1.setText(txt);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

and the layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</RelativeLayout>

Xml file: (I'm keeping usb file this because i want the information about all devices)

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <usb-device/>
</resources>

have you checked that your device actually supports the full USB API, and not just USB OTG? That is the obvious thing to check if you are always getting an empty device list, and there are free apps in Google Play that will do just that.

使用USB Host Controller App来检查您的平板电脑是否支持USB主机模式API..OR ..从“关于”菜单中提供平板电脑的一些软件信息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM