Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Detect that Android Device is connected with USB OTG Or Not programmatically

I am working with custom OTG fingerprint scanner. I want to check that OTG is connected to my Android device or not in a specific android activity.

like image 443
Hanan Avatar asked Jan 27 '17 17:01

Hanan


People also ask

How do I know if my Android device supports USB OTG?

The easiest way to see if your phone or tablet supports USB OTG is to look at the box it came in or the manufacturer's website. You'll see a logo like the one above or USB OTG listed in the specifications. You may also be able to find USB OTG information in the device's settings.

How do I turn on OTG notifications?

Check OTG Settings Usually, when you try to connect an OTG, you get an alert “Enable OTG”. This is when you need to turn the OTG option ON. To do this, navigate through Settings > Connected devices > OTG. Here, click on the On/Off toggle to activate it.


1 Answers

public class BootUpReceiver extends BroadcastReceiver {
    
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    String TAG = "OTG   ";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
//        Log.e("USB", "Device Connected -> " + action);
//Initialising global class to access USB ATTACH and DETACH state
        final GlobalClass globalVariable = (GlobalClass) context.getApplicationContext();

        if (action.equalsIgnoreCase("android.hardware.usb.action.USB_DEVICE_ATTACHED")) {

            UsbDevice device = (UsbDevice) intent
                    .getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if(device != null) {
                int vendorID = device.getVendorId();
                int productID = device.getProductId();
                if(String.valueOf(productID).equalsIgnoreCase(context.getString(R.string.productID/*product id of your specific device*/))&& (String.valueOf(vendorID).equalsIgnoreCase(context.getString(R.string.vendorID/*vendor id of your specific device*/)))){
    //If Product and Vendor Id match then set boolean "true" in global variable 
                    globalVariable.setIs_OTG(true);
                }else{
                    globalVariable.setIs_OTG(false);
                }
            }
        } else if (action.equalsIgnoreCase("android.hardware.usb.action.USB_DEVICE_DETACHED")) {
      //When ever device Detach set your global variable to "false"
            globalVariable.setIs_OTG(false);
        }  }   

From any Activity you can call global variable to check the current USB state:

 final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
                        if (globalVariable.is_OTG()) {
                            //Perform your functionality
                        } 

GlobalClass:

public class GlobalClass extends Application {

    private boolean is_OTG = false;

    public boolean is_OTG() {
        return is_OTG;
    }

    public void setIs_OTG(boolean is_OTG) {
        this.is_OTG = is_OTG;
    }

}

manifest:

 <application
        android:name="com.GlobalClass"

receiver:

 <receiver
            android:name=".BootUpReceiver"
            android:enabled="true" >
          <!--  <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.HOME" />
            </intent-filter>-->

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
            </intent-filter>
            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />

        </receiver>
like image 61
Hanan Avatar answered Sep 20 '22 20:09

Hanan