Monday, October 14, 2013

List attached USB devices in USB Host mode

In this exercise, my Android phone (HTC One X) act as USB Host to list attached USB devices via USB OTG cable. It target devices with minSdkVersion="12".

Listed result without any extra device attached.

With card reader attached, via USB OTG cable.

With another Nexus One attached as slave, via USB OTG cable.

To specify the app to be run as UDB Host, add uses-feature of "", and android:minSdkVersion="12" in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

    <uses-feature android:name="" />
        android:targetSdkVersion="18" />

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

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


Main Java code.
package com.example.androidusbhost;

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

import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.Context;

public class MainActivity extends Activity {

 Button btnCheck;
 TextView textInfo;

 protected void onCreate(Bundle savedInstanceState) {
  btnCheck = (Button) findViewById(;
  textInfo = (TextView) findViewById(;
  btnCheck.setOnClickListener(new OnClickListener() {

   public void onClick(View arg0) {

 private void checkInfo() {
  UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
  HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
  Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
  String i = "";
  while (deviceIterator.hasNext()) {
   UsbDevice device =;
   i += "\n" +
    "DeviceID: " + device.getDeviceId() + "\n" +
    "DeviceName: " + device.getDeviceName() + "\n" +
    "DeviceClass: " + device.getDeviceClass() + " - " 
     + translateDeviceClass(device.getDeviceClass()) + "\n" +
    "DeviceSubClass: " + device.getDeviceSubclass() + "\n" +
    "VendorID: " + device.getVendorId() + "\n" +
    "ProductID: " + device.getProductId() + "\n";
 private String translateDeviceClass(int deviceClass){
  case UsbConstants.USB_CLASS_APP_SPEC: 
   return "Application specific USB class";
  case UsbConstants.USB_CLASS_AUDIO: 
   return "USB class for audio devices";
  case UsbConstants.USB_CLASS_CDC_DATA: 
   return "USB class for CDC devices (communications device class)";
  case UsbConstants.USB_CLASS_COMM: 
   return "USB class for communication devices";
  case UsbConstants.USB_CLASS_CONTENT_SEC: 
   return "USB class for content security devices";
  case UsbConstants.USB_CLASS_CSCID: 
   return "USB class for content smart card devices";
  case UsbConstants.USB_CLASS_HID: 
   return "USB class for human interface devices (for example, mice and keyboards)";
  case UsbConstants.USB_CLASS_HUB: 
   return "USB class for USB hubs";
  case UsbConstants.USB_CLASS_MASS_STORAGE: 
   return "USB class for mass storage devices";
  case UsbConstants.USB_CLASS_MISC: 
   return "USB class for wireless miscellaneous devices";
  case UsbConstants.USB_CLASS_PER_INTERFACE: 
   return "USB class indicating that the class is determined on a per-interface basis";
  case UsbConstants.USB_CLASS_PHYSICA: 
   return "USB class for physical devices";
  case UsbConstants.USB_CLASS_PRINTER: 
   return "USB class for printers";
  case UsbConstants.USB_CLASS_STILL_IMAGE: 
   return "USB class for still image devices (digital cameras)";
  case UsbConstants.USB_CLASS_VENDOR_SPEC: 
   return "Vendor specific USB class";
  case UsbConstants.USB_CLASS_VIDEO: 
   return "USB class for video devices";
   return "USB class for wireless controller devices";
  default: return "Unknown USB class!";

<LinearLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:textStyle="bold" />
        android:text="Check USB devices" />
        android:layout_height="wrap_content" />


download filesDownload the files.

Next: - List UsbDevice, UsbInterface and UsbEndpoint in USB Host mode

Step-by-step: Android USB Host Mode programming


nguyen huy Vo said...

manager.getDeviceList() return empty? Why? I can't...=.=

Andr.oid Eric said...

getDeviceList() return empty if no devices are attached, or if USB host mode is inactive or unsupported.

In my case of HTC One X, even no device attached, it return one device.

Have you include minSdkVersion="12" and uses-feature of "" in AndroidManifest.xml?

What device you test on?

nguyen huy Vo said...
This comment has been removed by the author.
nguyen huy Vo said...

I included: minSdkVersion="12" and uses-feature of ""
in AndroidManifest.xml, but it still return empty.
I test on Sony Xperia P...:(
Please help me!

Andr.oid Eric said...

hello nguyen huy Vo,

I have no Xperia P. I tried to search from Internet. The reason may be:
- Used a in-compatible USB OTG.
- Not enough current provide from Xperia P.
- USB OTG blocked by virus apps, such as McAfee...

Campanha said...

Does it works in a laptop connected to the android?

Andr.oid Eric said...

In my understanding, NO.

When Android connected to PC, via normal USB (NOT OTG), ot act as accessory mode.

Anonymous said...

I think that you should check the file "/system/etc/permissions/handheld_core_hardware.xml" of your phone.

The xml file need the line ""

Anonymous said...

I think that you should check the xml file "/system/etc/permissions/handheld_core_hardware.xml" of your phone.

The xml file need
< feature name="" />

so that the android device could support usb host development.

Andr.oid Eric said...

hello Anonymous,

I have check my handheld_core_hardware.xml, there are no feature name="". But it should support USB Host mode.

Upendra Singh said...

Thank you this code is really helpful, I have some issue, When I connected first time it display all devices correctly. But when I disconnected the usb hub, this is still displaying list of devices because device data still present in manager.getDeviceList().
When I restarted the tab it worked fine again first time. In next time I again got same issue. How to clear list manager.getDeviceList() when usb device disconnected.

Andr.oid Eric said...

Hello Upendra Singh,

Yes, I have the same problem on HTC One X. But not on Samsung S3 and Galaxy Nexus. I think it is the problem of the device.

Upendra Singh said...

There are so many methods

but I still don't know what is the use of these methods. After doing all above this, mUsbManager.getDeviceList().values() is still showing usb devices.

Anonymous said...

It is possible to send the SMS from the PC to mobile via USB

Anonymous said...

id like to connect printer on Android tablet is this code is working

Andr.oid Eric said...

Sorry, I have no idea about this!

dunia damay said...

Nice tutorial... I've already tried this code in eclipse and running with emulator... I connect the USB device, smart card reader-ACR122U, it doesn't work well, when I click the button (Check USB devices), the emulator force to closed... Hmmm, any advice? Thank you

Anonymous said...

Hai andr.oid Eric.Can you tell me how to get the list of attached usb devices with its volume names.I tried your code but it was returning the path of the connected usb devices

Ambika said...

Thank you very much for such a useful code. but can you please tell me how to read data from usb otg

Mayank Langalia said...

Thank you so much for sharing the code but I wanted to know that I want path [Attached USB Device] and create dynamic folder and storing the content on it. if you have any idea please help me out on the same ....