In this tutorial we will learn how to detect usb connection in android. The tutorial explains two functionality , one is to detect the usb when the particular file is executing. Suppose if your functionality is to avoid user in connecting usb while your application is running then there is a possibility that user can plug the usb again after usb check is completed in our code, so in second functionality of tutorial i am explaining how to detect a usb connection at the time of plugging it.
Contents
- Check the USB is plugged in android.
- How to detect whether USB is plugged in when the application is running.
Check the USB is plugged in android
Before implementing this we need to understand what is the harm if the USB is plugged in. If the USB is plugged in other people can see what all process and api call are happening in background if they are a smart tech people. So putting a restriction on connecting USB while application is running can be considered as an extra security . First step is to check whether the USB is already connected or not while opening the application. For that you can just add the following function to your first activity.
public boolean isConnected() { Intent intent = registerReceiver(null, new IntentFilter("android.hardware.usb.action.USB_STATE")); return intent.getExtras().getBoolean("connected"); }
Once you add the function in your activity you can use the following code in your onCreate function to directly check whether the USB is connected or not. If it returns true USB connected and if it returns false USB is not connected.
if(isConnected()){ Toast.makeText(getApplicationContext(),"usb is connected",Toast.LENGTH_SHORT).show(); }
How to detect whether USB is plugged in when the application is running
If we check whether the usb is connected or not at the beginning of application and found not connected, but he can connect it while application is ongoing also. So now let us see how your app will automatically recognize if the USB is plugged while your application is running. You need to have Broadcast Receiver class. Create a class in your package and add the following code.
MyReceiver.java
public class MyReceiver extends BroadcastReceiver { private String TAG ="status...."; @Override public void onReceive(Context context, Intent intent) { Intent intent1 = new Intent("usb_detect"); context.sendBroadcast(intent1); Log.d(TAG,"USB connected.."); } }
If you are using is broadcast receiver you need to register it in your AndroidManifest.xml file. You can add the following code to your AndroidManifest.xml inside the application tag for registering the above broadcast receiver.
<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> </intent-filter> </receiver>
Now just simply add the following code to the activity which you want to use this functionality. So whenever the USB connection is detected it will send an internal broadcast to your activity and you can restrict the user from using the application while this message is triggered.
BroadcastReceiver broadcast_reciever = new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent intent) { String action = intent.getAction(); if (action.equals("usb_detect")) { Toast.makeText(getApplicationContext(),"Please remove your usb",Toast.LENGTH_SHORT).show(); // DO WHATEVER YOU WANT. } } }; @Override protected void onResume() { super.onResume(); registerReceiver(broadcast_reciever, new IntentFilter("usb_detect")); } @Override protected void onPause() { super.onPause(); registerReceiver(broadcast_reciever, new IntentFilter("usb_detect")); }