Cómo detectar que el dispositivo Android está conectado con USB OTG o no mediante programación

Estoy trabajando con el escáner de huellas dactilares OTG personalizado. Quiero comprobar que OTG está conectado a mi dispositivo Android o no en una actividad específica de Android.

 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", "Decive Connected -> " + action); //Initilizing globel 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); } } 

Desde cualquier actividad se puede llamar variable globel para comprobar el estado USB actual:

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

Clase Global:

 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; } } 

Manifestante

  <application android:name="com.GlobalClass" 

receptor:

  <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> 
  • Depuración Reaccionar Nativo sobre adb tcpip
  • Reproducir vídeo utilizando USB conectado a través de cable OTG en Android?
  • Cómo listar archivos en un dispositivo USB OTG
  • No se detecta el dispositivo USB de modo host - ¿Qué archivos son exactamente necesarios?
  • ¿Cómo puede acceder la aplicación a los archivos de almacenamiento USB OTG en Android 6.0 (API nivel 23) sin root?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.