0

i have tried developing a sample app with the help of the code from USB Host.

public class MainActivity extends Activity {

UsbManager manager;
HashMap<String, UsbDevice> deviceList;
Button scanButton;
UsbDevice device;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    scanButton = (Button)this.findViewById(R.id.button1);
    scanButton.setOnClickListener(new OnClickListener ()
    {
        public void onClick(View v) 
        {
            checkForDevices ();
        }
    });
}

@Override
public void onResume ()
{
    super.onResume();
    checkForDevices ();
}

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

protected void checkForDevices ()
{
    manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    deviceList = manager.getDeviceList();
    device = deviceList.get("deviceName");
    //Collection<UsbDevice> devices = deviceList.values();

   if (device != null)
       Toast.makeText(this, "Device Found", Toast.LENGTH_LONG).show();
   else
       Toast.makeText(this, "Device NOT Found", Toast.LENGTH_LONG).show();
}
}

When i run this code with a USB device connected, i get the Toast as "Device NOT Found".

can anyone help me out to fix this?

Thank You.

Allen
  • 681
  • 5
  • 16
  • 30
  • Are you sure deviceName is not a placeholder name you are supposed to replace with something? – Machinarius Aug 22 '12 at 04:32
  • No idea. I will cross check it. – Allen Aug 22 '12 at 04:58
  • i believe your best course of action would be to list devices and spit that out in a special activity with a listview customized to hold entries with fields for all values you might get/need. That way you can be sure of what to seek for exactly – Machinarius Aug 26 '12 at 19:00
  • ditto happens with me, my pendrive gets detected by the tablet but i cannot detect it through code, has anybody found anything? – Calvin Oct 11 '12 at 10:10

1 Answers1

2

Check if your device supports host mode. try this perhaps this may help: Android USB host and hidden devices

Community
  • 1
  • 1
Calvin
  • 617
  • 1
  • 12
  • 34