1
votes

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>
2

2 Answers

0
votes

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.

0
votes

Use the USB Host Controller App to check whether your Tablet support USB Host Mode APIs or not ..OR.. provide some software info of your tablet from About menu.