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 :

Leave a Comment:

20 comments
Kasi says July 9, 2011

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….

Reply
    Sushant says July 9, 2011

    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.

    Reply
rain says November 11, 2011

Thank you for this nice post. It helped me alot.

Reply
Ramkumar says November 23, 2011

Is it possible to deactivate USB connection when service of my apps running..

Reply
Mike Trethowan says November 25, 2011

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.

Reply
Mike Trethowan says November 25, 2011

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.

Reply
Mike Trethowan says November 25, 2011

Sorry, I didn’t double post. The first post gave me an error that I entered the wrong security code. When I reentered, the screen refreshed and the double post appeared. Thanks

Reply
what is android says January 29, 2012

Hello there, You’ve performed a fantastic job. I will certainly digg it and in my opinion recommend to my friends. I’m sure they will be benefited from this site.

Reply
игры для android says February 5, 2012

Very great post. I just stumbled upon your weblog and wanted to say that I’ve really enjoyed surfing around your blog posts. After all I will be subscribing on your rss feed and I’m hoping you write again very soon!

Reply
Обзор китайских товаров says February 12, 2012

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.

Reply
Mike Trethowan says February 20, 2012

I guess this topic is obsolete

Reply
bhavik says April 4, 2012

can u send me the code for this example.

Reply
Tuyen says April 27, 2012

please help to me, I copy the same code, but not run, i don’t know why, my android 3.2

Reply
ips says June 22, 2012

its a not working

Reply
AJ says June 25, 2012

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.

Reply
Hannes says August 13, 2012

Seems UMS_CONNECTED is deprecated. I tried MEDIA_MOUNTED but cant seem to get it to work. Any suggestions. Thanks

Reply
mudit srivastava says October 22, 2012

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.

Reply
LauraBacu says December 3, 2012

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!!

Reply
LauraBacu says December 3, 2012

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!

Reply
KrihsnaKanth says February 15, 2013

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 ….. :).

Reply
Add Your Reply