Android : Unable to enumerate usb device whenever it is connected while my application is running

on Sunday, August 17, 2014


I am building an app for UsbInterfacing and i need to enumerate usb devices continously while my application is running..i need to be notified when it is connected and disconnected.. My usb device's vendor id is 1204 and product-id is 64905..The enumeration works once. Will not work after i unplug the device and plug it back in..Below is my code:


package com.example.enumerate;



import java.util.HashMap;
import java.util.Iterator;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class Enumerate extends Activity {
//My onResume method
@Override
protected void onResume()
{
super.onResume();
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
deviceIterator = deviceList.values().iterator();
//my UsbIterator
while(deviceIterator.hasNext()){
device = deviceIterator.next();

}
if(device.getProductId()==64905) //my device ProductId
{
Toast t=Toast.makeText(this,"Connected",2000);
t.show();

}
else
{
Toast t=Toast.makeText(this,"Not Connected",2000);
}
}
UsbDevice device;
Iterator<UsbDevice> deviceIterator;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

}

0 comments:

Post a Comment