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.
Solution 1:
publicclassBootUpReceiverextendsBroadcastReceiver{
privatestaticfinalString ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
String TAG = "OTG ";
@Override
publicvoid onReceive(Context context, Intent intent) {
String action = intent.getAction();
// Log.e("USB", "Device Connected -> " + action);//Initialising global class to access USB ATTACH and DETACH statefinal 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);
}
}
} elseif (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:
finalGlobalClassglobalVariable= (GlobalClass) getApplicationContext();
if (globalVariable.is_OTG()) {
//Perform your functionality
}
GlobalClass:
publicclassGlobalClassextendsApplication {
privateboolean is_OTG = false;
publicbooleanis_OTG() {
return is_OTG;
}
publicvoidsetIs_OTG(boolean is_OTG) {
this.is_OTG = is_OTG;
}
}
manifest:
<application
android:name="com.GlobalClass"
receiver:
<receiverandroid: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><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter><intent-filter><actionandroid:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /><actionandroid:name="android.hardware.usb.action.USB_DEVICE_DETACHED" /></intent-filter><meta-dataandroid:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"android:resource="@xml/device_filter" /></receiver>
Solution 2:
Your can use UsbManager to detect the attached otg devices.
UsbManagerusbManager= (UsbManager) this.getSystemService(Context.USB_SERVICE);
Intentintent=newIntent("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();
}
}
Post a Comment for "How To Detect That Android Device Is Connected With Usb Otg Or Not Programmatically"