I apologize, I recently asked a question that was not received well due to the inaccuracy of describing my problem. I have edited my question.
I am a beginner trying to develop an app that utilizes usb host. I have read through the USB Host|Android Developer tutorial, but I am still lost as to how it is initially set up.
My intent is for the app to use the Enumeration process to locate the device, as I do not know what my connected device's vendor-id or product-id is. I receive a Fatal Exception: main error when I attempt to run what I have.
Below is my code so far. Any help would be greatly appreciated.
Main Activity class
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbDevice;
import android.content.Context;
import java.util.HashMap;
import java.lang.Object;
import android.content.Intent;
import android.util.Log;
import java.util.Iterator;
import java.util.Collection;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap <String, UsbDevice> deviceList = manager.getDeviceList();
UsbDevice device = deviceList.get("deviceName");
}
Manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.trapperdavis.ircontprototype2">
<uses-sdk android:minSdkVersion="12" />
<uses-feature android:name="android.hardware.usb.host" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
</application>
</manifest>
on https://developer.android.com/guide/topics/connectivity/usb/host.html is said basically that there are two possibilities to get information about USB devices attached to the android
when a device is connected the os (operating system) send an event that can be captured by an IntentFilter
(event-based programming)
the app can query for devices that are already connected
i think you want to query for already connected USB devices and get informations about the devices. with your code you can list all attached USB devices but can not get information abou them. the following code will print more informations about the attached devices, i coped it from http://android-er.blogspot.de/2013/10/list-attached-usb-devices-in-usb-host.html
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 = deviceIterator.next();
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";
}
textInfo.setText(i);
}
full source code is on http://android-er.blogspot.de/2013/10/list-attached-usb-devices-in-usb-host.html
the code uses and Iterator
class that accesses all USB devices in deviceList
that was generated by the USB manager. Then the informations (vendorID, ...) are extracted from the device
class by accessing its fields (the fields of the device
class are built internally by the os by native C functions (JNI))