title: USB host for android
body: <p>I am trying to communicate with my freeduino board which is similar to arduino uno via usb through android device 'nexus 7' which has 4.2 (jelly beans) in it.<img src="https://i.stack.imgur.com/JU8fz.jpg" alt="This is the photo of my freeduino board. It look exactly like this"></p>
  
  <p>I used the developers guide to communicate with the device from a couple of months but with no result. I think i am missing something minute in it. I am trying to simply display the vendor id of my freeduino board. My manifest file look like this.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.usb_host_final_try"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.usb_host_final_try.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
              &lt;intent-filter&gt;
          &lt;action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /&gt;
      &lt;/intent-filter&gt;
  
      &lt;meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
          android:resource="@xml/device_filter" /&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>I have created a file device_filter in res/xml dir. which look like this</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;usb-device vendor-id="0403" product-id="07d7" /&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>and my MainActivity.java has the following code.</p>
  
  <pre><code>package com.example.usb_host_final_try;
  
  import java.util.HashMap;
  
  import android.hardware.usb.UsbDevice;
  import android.hardware.usb.UsbManager;
  import android.os.Build;
  import android.os.Bundle;
  import android.annotation.TargetApi;
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.view.Menu;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
  
      @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Intent intent = new Intent();
          UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
          HashMap&lt;String, UsbDevice&gt; deviceList = manager.getDeviceList();
          UsbDevice device = deviceList.get("deviceName");
          int vid=device.getVendorId();
          TextView tv = (TextView) findViewById(R.id.textview);
          tv.setText(Integer.toString(vid));
      }
  </code></pre>
  
  <p>please help me out i am screwed up over it from past few months now. I have tried doing this but it is also not working.</p>
  
  <p><a href="http://code.google.com/p/usb-serial-for-android/" rel="nofollow noreferrer">link</a></p>
  
  <p><a href="http://android.serverbox.ch/?p=549" rel="nofollow noreferrer">link</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12110555/how-to-detect-usb-device-in-android-tablet-which-acts-as-usb-host">Stack overfolow question</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11773561/putting-an-android-device-in-usb-host-mode">stack overflow question</a></p>
  
  <p>I have tried all these and still haven't been able to succeed. I also tried to use an if else loop to see if enumeration works where i set the text to device not found if no device is else settext to vendor id. On emulator is showed no device found but on my tablet wen i connect the device it force closes or else it still shows no device found wen nothing is connected.</p>
  
  <p>The stack trace is here..</p>
  
  <blockquote>
    <p>01-05 09:10:35.364: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0
    01-05 09:10:35.364: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0
    01-05 09:10:35.464: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0
    01-05 09:10:35.464: W/Trace(1658): Unexpected value from nativeGetEnabledTags: 0
    01-05 09:10:35.624: D/AndroidRuntime(1658): Shutting down VM
    01-05 09:10:35.624: W/dalvikvm(1658): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
    01-05 09:10:35.644: E/AndroidRuntime(1658): FATAL EXCEPTION: main
    01-05 09:10:35.644: E/AndroidRuntime(1658): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usb_host_final_try/com.example.usb_host_final_try.MainActivity}: java.lang.NullPointerException
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at android.os.Handler.dispatchMessage(Handler.java:99)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at android.os.Looper.loop(Looper.java:137)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at java.lang.reflect.Method.invoke(Method.java:511)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at dalvik.system.NativeStart.main(Native Method)
    01-05 09:10:35.644: E/AndroidRuntime(1658): Caused by: java.lang.NullPointerException
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at com.example.usb_host_final_try.MainActivity.onCreate(MainActivity.java:35)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at android.app.Activity.performCreate(Activity.java:5104)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    01-05 09:10:35.644: E/AndroidRuntime(1658):     ... 11 more
    01-05 09:10:46.074: W/Trace(1712): Unexpected value from nativeGetEnabledTags: 0
    01-05 09:10:46.074: W/Trace(1712): Unexpected value from nativeGetEnabledTags: 0
    01-05 09:10:46.754: D/AndroidRuntime(1712): Shutting down VM
    01-05 09:10:46.754: W/dalvikvm(1712): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
    01-05 09:10:46.766: E/AndroidRuntime(1712): FATAL EXCEPTION: main
    01-05 09:10:46.766: E/AndroidRuntime(1712): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usb_host_final_try/com.example.usb_host_final_try.MainActivity}: java.lang.NullPointerException
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at android.os.Handler.dispatchMessage(Handler.java:99)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at android.os.Looper.loop(Looper.java:137)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at java.lang.reflect.Method.invoke(Method.java:511)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at dalvik.system.NativeStart.main(Native Method)
    01-05 09:10:46.766: E/AndroidRuntime(1712): Caused by: java.lang.NullPointerException
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at com.example.usb_host_final_try.MainActivity.onCreate(MainActivity.java:35)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at android.app.Activity.performCreate(Activity.java:5104)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    01-05 09:10:46.766: E/AndroidRuntime(1712):     ... 11 more
    01-05 09:10:49.104: I/Process(1712): Sending signal. PID: 1712 SIG: 9</p>
  </blockquote>