Saturday, September 8, 2012

Detect USB connection In Android

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:
package com.app.DetactUSB;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;

import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;
public class DetactUSB extends BroadcastReceiver
{
    private static final String TAG = "DetactUSB";
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (intent.getAction().equalsIgnoreCase("android.intent.action.UMS_CONNECTED"))
        {
                TextView textView = new TextView(context);
                textView.setBackgroundColor(Color.MAGENTA);
                textView.setTextColor(Color.BLUE);
                textView.setPadding(10,10,10,10);
                textView.setText("USB connected……….");
                Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
                toastView.setGravity(Gravity.CENTER, 0,0);
                toastView.setView(textView);
                toastView.show();
                Log.i(TAG,"USB connected..");
        }
        if (intent.getAction().equalsIgnoreCase("android.intent.action.UMS_DISCONNECTED"))
        {
                TextView textView = new TextView(context);
                textView.setBackgroundColor(Color.MAGENTA);
                textView.setTextColor(Color.BLUE);
                textView.setPadding(10,10,10,10);
                textView.setText("USB Disconnected……….");
                Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
                toastView.setGravity(Gravity.CENTER, 0,0);
                toastView.setView(textView);
                toastView.show();
        }
    }
}
4.) Add receiver in AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.app.DetactUSB"
     android:versionCode="1"
     android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.INTERNET">
    </uses-permission>
    <uses-permission android:name="android.permission.RECORD_AUDIO">
    </uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
       <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>
    <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>
    </application>
</manifest>
5.) To detect USB :
android.intent.action.UMS_CONNECTED
android.intent.action.UMS_DISCONNECTED
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:
package com.app.DetactUSB;
import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity
{
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
        }
}
Output : The final output will look like following :

5 comments:

  1. Listed code is not working any alternative solution :(

    I also found some defects for
    android.hardware.usb.action.USB_DEVICE_ATTACHED

    https://code.google.com/p/android/issues/detail?id=25701
    https://code.google.com/p/android/issues/detail?id=25703

    Thanks,
    Sathishwaran

    ReplyDelete
  2. Hi,without registering the Broadcast receiver in activity how will it work?

    ReplyDelete
  3. Third Class Code Didn't work....

    ReplyDelete