12

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.

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
Hanan
  • 444
  • 4
  • 16
  • Can any one has solution of this ? – Hanan Jan 30 '17 at 08:17
  • 1
    Have you tried [USB host](https://developer.android.com/guide/topics/connectivity/usb/host.html)? maybe `android.hardware.usb.action.USB_DEVICE_ATTACHED` receiver will help. if you need forward communication [HERE](http://blog.blecentral.com/2015/10/01/handling-usb-connections-in-android/) you have nice example (for Arduino, but mostly about Android-side). but if it is "specific android device" then maybe there is a custom rom or you have "specific" doc for that device – snachmsm Mar 28 '17 at 16:50
  • Yes @snachmsm I am using same technique. When ever device is attached I check vendor Id and Device Id and then set status of device accordingly. – Hanan Mar 29 '17 at 12:24
  • so I should put my comment as an answer? :-) – snachmsm Mar 29 '17 at 13:51
  • I already figure out before your answer :D – Hanan Mar 29 '17 at 14:08
  • you have 4 upvotes (including mine), you should self-answered this question, we were all curious ;) good luck! – snachmsm Mar 29 '17 at 21:10

2 Answers2

4
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>
Saurav_Sharma
  • 130
  • 1
  • 10
Hanan
  • 444
  • 4
  • 16
  • Is this source working for the connection of *USB OTG* Pendrive ? How can i get total available space if i will get successful connection ? And how to get device Vendor id and Product id after connection ? Actually i need to connect *USB OTG* pendrive and get available size from connected device. Any help will be appreciated. – Jay Rathod Jan 18 '18 at 10:59
  • Use above code, When OTG is attached it will tiger broadcast and you can fetch user id and vendor id as well. – Hanan Jan 18 '18 at 11:01
  • Fetch user id and vendor id that is fine. But my other requirement is to get all available folders and get total available space of connected USB device. How can i do that? – Jay Rathod Jan 18 '18 at 11:19
  • Did you find anything for above solution ? – Jay Rathod Jan 19 '18 at 05:34
  • @JayRathodRJ did you find the solution for your requirement that you mentioned above? If achieved could you please share the solution here? We are on the same track to do so. Thanks in Advance! – Sathish Jul 23 '19 at 09:55
1

Your can use UsbManager to detect the attached otg devices.

    UsbManager usbManager = (UsbManager) this.getSystemService(Context.USB_SERVICE);
    Intent intent = new Intent("android.hardware.usb.action.USB_DEVICE_ATTACHED");
    intent.addCategory("android.hardware.usb.action.USB_DEVICE_DETACHED");
    Map<String, UsbDevice> usbDeviceList = usbManager.getDeviceList();
    Toast.makeText(this, "Deivce List Size: " + usbDeviceList.size(), Toast.LENGTH_SHORT).show();

    if (usbDeviceList.size() > 0) {

        //vid= vendor id .... pid= product id
            Toast.makeText(this, "device pid: " + device.getProductId() + " Device vid: " + device.getVendorId(), Toast.LENGTH_SHORT).show();

        }
    }
Sulman Rasheed
  • 92
  • 1
  • 13