getDeviceList() always empty - android

I'm trying to use an Arduino Board along with my Odys Neo x8 tablet but it seems, that the UsbManager doesn't recognize the device alright. I connected the arduino to the tablet via an OTG-adapter so that the tablet will work in host mode, the Arduino is successfully receiving power from the device.
I'm fetching the list of available USB-devices on the tablet as follows:
sUsbController = new UsbController(this, mConnectionHandler, 0, 0);
HashMap<String, UsbDevice> devlist = sUsbController.mUsbManager.getDeviceList();
TextView t = ((TextView)findViewById(R.id.textView));
t.setText("Found " + Integer.toString(devlist.size()) + " devices");
And inside the class UsbController:
mUsbManager = (UsbManager) mApplicationContext
.getSystemService(Context.USB_SERVICE);
But unfortunately, the list remains empty, even if i start filtering using the VID and the PID (the two zeros).
Any suggestions on how to fix this?

I have used the following code which works very fine with keyboard, mouse and Mass Storage device to connect with Pandaboard,
UsbManager usbManager = (UsbManager) getSystemService(USB_SERVICE);
HashMap<String, UsbDevice> devicelist = usbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = devicelist.values().iterator();
while(deviceIterator.hasNext()) {
UsbDevice usbDevice = deviceIterator.next();
Log.i(Log_Tag, "Model : " +usbDevice.getDeviceName());
Log.i(Log_Tag, "Id : " +usbDevice.getDeviceId());
}
This should work with Arduino too.

The Arduino board needs a driver which needs to be installed before it can be accessed.
I am not sure if you have a port of the driver for Android.
Edit:
Also check out this answer in another thread.

Related

How detect the OTG cable already connected with device in android?

I can detect whether the OTG cable attached or detached. But how to detect if OTG cable already connected when app runs. My app only detects if otg cable attached or detached.
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e("USB", "Decive Connected -> " + action);
if (action.equalsIgnoreCase(ACTION_USB_ATTACHED)) {
UsbDevice device = (UsbDevice) intent
.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
int vendorID = device.getVendorId();
int productID = device.getProductId();
//If Product and Vendor Id match then set boolean "true" in global variable
tv_otg.setText("External OTG storage device connected !");
Log.e("true", "true");
}
} else if (action.equalsIgnoreCase(ACTION_USB_DETACHED)) {
//When ever device Detach set your global variable to "false"
tv_otg.setText("External OTG storage device disconnected !");
Log.e("ACTION_USB_DETACHED", "ACTION_USB_DETACHED");
}
}
}
bootupreceiver = new BootUpReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_USB_ATTACHED);
filter.addAction(ACTION_USB_DETACHED);
filter.setPriority(100);
registerReceiver(bootupreceiver, filter);
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
...
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext()){
UsbDevice device = deviceIterator.next();
//your code for check device check name logic
}
Check DOC

How to get space Information of Usb conneted Device

I have a an android Setup Box in which 2gb internal Memory available and 8 gb external memory available and device has slots for connect USB device. I wanted to get Memory information of connected USB Device. Suppose i have connected more then one USB Device. I am getting connection Disconnection event using Broadcast Receiver. But if i wanted to get Memory information that i am unable to get. I am only able to get inbuilt external memory information Please Guide me.
I am very new to android development. I have tried this way
private void sizeInfo(){
File path = Environment.getDataDirectory();
StatFs stat2 = new StatFs(path.getPath());
long blockSize = stat2.getBlockSize();
long availableBlocks = stat2.getAvailableBlocks();
String format = Formatter.formatFileSize(this, availableBlocks * blockSize);
// Log.d("space","Path : "+ path +"\n");
Log.d("space","Available Space in GB : "+ format);
String str = Environment.getExternalStorageState();
//Log.d("Checking","Media "+Environment.isExternalStorageEmulated());
}
I am getting list of attached device by following
private void updateDeviceList() {
Log.d("Checking","updateDeviceList function");
int DivCount =0;
HashMap<String, UsbDevice> connectedDevices = mUsbManager.getDeviceList();
if (connectedDevices.isEmpty()) {
usbDevice = null;
Log.d("Checking", "No Currently Device Conneted");
} else {
for (UsbDevice device : connectedDevices.values()) {
DivCount++;
Log.d("Device Info","DeviceId " +device.getDeviceId()+"\n");
Log.d("Device Info","ProductId "+device.getProductId()+"\n");
Log.d("Device Info","DeviceName "+device.getDeviceName()+"\n");
Log.d("Device Info","DeviceClass "+device.getDeviceClass());
String str = Build.MANUFACTURER;
Log.d("Device Info","Model "+str);
usbDevice =device;
sizeInfo();
}
}
}
I am able to get attached device list but not able to getting memory information of removable usb

Android : how to detect already connected usb device?

I'm trying to detect usb devices which are already connected to android.
I understand there are actions to detect when USB is either attached or detached.
But I don't really get how to check devices after connecting usb device to android.
Also, I've found that each USB device has it's device class code, but how do I figure out what kind of device is connected? For instance, I need to detect both usb mouse and keyboard; how do I differentiate them?
Try this:
First register Broadcast for USB connection.
manifest permission:
:
<intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter>
Get the List of USB Device with details by using this
public void getDetail() {
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext()) {
UsbDevice device = deviceIterator.next();
manager.requestPermission(device, mPermissionIntent);
String Model = device.getDeviceName();
int DeviceID = device.getDeviceId();
int Vendor = device.getVendorId();
int Product = device.getProductId();
int Class = device.getDeviceClass();
int Subclass = device.getDeviceSubclass();
}}
Just to answer Benny's question here is what the mPermissionIntent could look like:
string actionString = context.PackageName + ".action.USB_PERMISSION";
PendingIntent mPermissionIntent = PendingIntent.GetBroadcast(context, 0, new
Intent(actionString), 0);
mUsbManager.RequestPermission(device, permissionIntent);

Connect Android to RambO (Reprap) via USB

Hy Guys
i try to connect my Rambo Board (http://reprap.org/wiki/Rambo) with android using https://code.google.com/p/usb-serial-for-android/. The app allready starts when i plug in the usb port, but i cant open the device.
The testoutput of the vendor id and product id is right but "UsbSerialProber.acquire(mUsbManager, device);" returns null?
what can i do?
My App Works with an Arduino.
Thank you
Markus
protected void onResume() {
super.onResume();
//Markus K hinzugefügt um device zu finden
mUsbManager.getDeviceList();
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
mTitleTextView.setText(mTitleTextView.getText()+""+ deviceList.size()+"Geräte gefunden");
UsbDevice device=null;
while(deviceIterator.hasNext()){
device = deviceIterator.next();
mTitleTextView.setText(mTitleTextView.getText()+ device.getDeviceName()+" vendorid: "+device.getVendorId()+" productid"+device.getProductId());
//abfrage auf richtige vendor id!!
}
mSerialDevice = UsbSerialProber.acquire(mUsbManager, device);
mTitleTextView.setText(mTitleTextView.getText()+ "Resumed, mSerialDevice=" + mSerialDevice);
if (mSerialDevice == null) {
mTitleTextView.setText(mTitleTextView.getText()+ "No serial device.");
} else {

How to detect hands free garniture on Android 2.2+?

I want to know when user has connected hands free accessories and hasn't blocked calls\sms. Is it possible to know when it is connected via hardware ports or bluetooth?
Try this in your onCreate or onResume.
BluetoothAdapter myLocalAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice garniture;
Set<BluetoothDevice> connectedDevices = myLocalAdapter.getBondedDevices();
for (BluetoothDevice device : connectedDevices){
String name = device.getName();
//... check for the name you want
if( name.equals("whatnameisit"){
garniture = device
}
}
if (garniture != null){
// yay we found it, lets do our work with the device here
}

Categories

Resources