Detect USB Connection

Detect USB connection

.This is a sample program to show the broadcast notification that USB connected and disconnected. Last topic published in this forum is Location Manager

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project DetactUSB.
2.) Create a class DetactUSB.java, which will extends BroadcastReceiver.
3.) Put the following code in DetactUSB.java:

 

 

 

4.) Add receiver in AndroidManifest.xml :

 

 

 

5.) To detect USB :

 

 

 

6.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. DetactUSB. Enter following information:
Project name: DetactUSB
Build Target: Android 2.1
Application name: DetactUSB
Package name: com.app.DetactUSB
Create Activity: MyActivity

On Clicking Finish DetactUSB code structure is generated with the necessary Android Packages being imported along with MyActivity.java. MyActivity class will look like following:

 

 

 

Output : The final output will look like following :

Comments 20

  1. i am fully confused with this code… How the activity changes from MyActivity.java to DetactUSB.java…. and also we need to add MyActivity.java to android manifest file…. i have done all these… but it is still showing an error “the application has stopped unexpectdely” plz help on this topic….

    • When you set a receiver in your activity (DetactUSB for above example) and define filters in it as we did

      [code lang=”xml”]
      <receiver android:name=".DetactUSB">
      <intent-filter>
      <action android:name="android.intent.action.UMS_CONNECTED" />
      <action android:name="android.intent.action.UMS_DISCONNECTED" />
      </intent-filter>
      </receiver>
      [/code]

      on launching your app your main activity (MyActivity in this case) will call that receiver internally on receiving defined intents or actions. This is how and why activity changes from MyActivity.java to DetactUSB.java.

      There is no need to add manually MyActivity.java into your manifest as it gets added automatically when you create project.

      [code lang=”xml”]
      <activity android:name=".MyActivity" android:label="@string/app_name">
      <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      </activity>
      [/code]

      If you still getting some force close check you manifest permissions and send me the “logcat” results.

      Thanks.

  2. Hi. I have created a project using your example. I am debugging on a BeagleBoard xM running Rowboat which has 4USB Host ports and one USB Device port which is used for ADB connections. I can compile and run the program from Eclipse, but I get no indication that the app is detecting USB activity. I have tried two separate devices, one is a FTDI R232 interface cable, the other a thumb drive with an activity LED. When plugging in the thumb drive it’s activity light flashes indicating communication is taking place. In Debug and DDMS I can see the app running, but I see no activity when I insert or remove a device. Any thoughts? Thank You.

    p.s. Once I get this figured out, I plan to extend this out to a Microchip DM240415 Demo Board for Android.

  3. Hi. I have created a project using your example. I am debugging on a BeagleBoard xM running Rowboat which has 4USB Host ports and one USB Device port which is used for ADB connections. I can compile and run the program from Eclipse, but I get no indication that the app is detecting USB activity. I have tried two separate devices, one is a FTDI R232 interface cable, the other a thumb drive with an activity LED. When plugging in the thumb drive it’s activity light flashes indicating communication is taking place. In Debug and DDMS I can see the app running, but I see no activity when I insert or remove a device. Any thoughts? Thank You.

    p.s. Once I get this figured out, I plan to extend this out to a Microchip DM240415 Demo Board for Android.

  4. Thanks , I’ve just been searching for information about this subject for a long time and yours is the best I have discovered so far. But, what concerning the bottom line? Are you positive about the supply?|What i do not realize is in reality how you are now not actually a lot more neatly-appreciated than you might be now. You are very intelligent.

  5. hi i can’t start the program. i’m using android 2.1 but a notice keep popping out after i start the program stating that an error has occurred. please help.

  6. this code is not working in 2.3.3 as the “usb connected” is not displaying on the screen. please help me to solve the error.

  7. hello, i’m quite new in android programming, i’m trying to learn how to configure the usb access and permissions to let my app comunicate with a serial port ftdi by usb. i’ve copy your code but i have another main class and i dont know how to call the class DetactUSB.java to make it works. thanks!!

  8. now i’ve understood the change of class that you explain in answer 2. but when i connect the usb doesnt appear any toast or any text. can you help me? thanks!

  9. It is working for me!!!!!! But if i uninstall app and run again it is not working. :( I don’t know why. But i do suggest that the 2 permissions “android.permission.INTERNET” and “android:name=”android.permission.RECORD_AUDIO” are not required ….. :).

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">