简体   繁体   English

无法在Android中使用USB主机获取USB设备列表

[英]Unable to get USB device list using usb host in Android

I want to retrieve a USB device list on my tablet. 我想在平板电脑上检索USB设备列表。

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. 根据代码,当连接USB时,它必须启动一个活动,甚至我正在检查设备是否可用,但没有任何列表。

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: Java代码:

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文件:(我保留USB文件是因为我想要有关所有设备的信息)

<?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? 您是否检查过您的设备确实支持完整的USB API,而不仅仅是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. 很明显,要检查您是否总是得到一个空的设备列表,并且Google Play中有免费的应用程序可以做到这一点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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