2

I have tried developing a sample app with the help of the code from Developers.android.com.

My code looks like this

  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 always get the Toast as "Device NOT Found".

I want my app to detect the USB device and Read input and Write Output in USB Host Mode.

Is there any way to detect an USB device in our App?

ale
  • 6,267
  • 6
  • 54
  • 64
Allen
  • 681
  • 5
  • 16
  • 29
  • possible duplicate of [Android apps, communicating with a device plugged in the USB port](http://stackoverflow.com/questions/3803871/android-apps-communicating-with-a-device-plugged-in-the-usb-port) – ale Aug 24 '12 at 15:18

3 Answers3

2

I had the same problem and I suspect that the host feature may be disabled on your tablet so I suggest that you check.

The following post is probably the best reference to look at as it is concise Android USB host and hidden devices

You should be able to check if the file android.hardware.usb.host.xml exist with the adb shell

Community
  • 1
  • 1
rom292
  • 48
  • 1
  • 1
  • 8
1

mmmm...

device = deviceList.get("deviceName");

i think u must insert the name of your device instead of "deviceName".

Marco
  • 181
  • 1
  • 5
0

the best example of working with usb devices in android would be here this is a good example for detecting usb device and work with that

sayyed mohsen zahraee
  • 2,872
  • 3
  • 26
  • 41