title: How to receive USB connection status broadcast?
body: <p>I am trying to detect USB connection in my app, that is, whether or not USB is connected to device.</p>
  
  <p>It's being tested on Marshmallow 6.0.1 (sdk23)</p>
  
  <p>But I'm unable to receive the broadcast actions ACTION_USB_DEVICE_ATTACHED or ACTION_USB_DEVICE_DETACHED..</p>
  
  <p>I tried using both the dynamic way and the AndroidManifest.xml way, neither worked..</p>
  
  <p>Here's my code:</p>
  
  <p><strong>AndroidManifest.xml</strong> :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.gokulnc.blah_blah"
      android:installLocation="auto"
      android:versionCode="15"
      android:versionName="1.5.1"&gt;
  
      &lt;uses-feature android:name="android.hardware.usb.host" /&gt;
  
      &lt;uses-sdk
          android:minSdkVersion="14"
          android:targetSdkVersion="23" /&gt;
  
      &lt;android:uses-permission android:name="android.permission.USB_PERMISSION" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:vmSafeMode="false"&gt;
  
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name"
              android:theme="@style/DrawerTheme"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
  
              &lt;receiver android:name="mUsbReceiver"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /&gt;
              &lt;/intent-filter&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" /&gt;
              &lt;/intent-filter&gt;
              &lt;/receiver&gt;
          &lt;/activity&gt;        
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>MainActivity.java</strong> :</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
      BroadcastReceiver mUsbReceiver;
  
      public void onCreate(Bundle savedInstanceState) {
          .....
          setBroadcastReceivers();
      }
  
      void setBroadcastReceivers() {
      //Reference: http://www.codepool.biz/how-to-monitor-usb-events-on-android.html
  
      mUsbReceiver = new BroadcastReceiver() {
          public void onReceive(Context context, Intent intent) {
              String action = intent.getAction();
              Log.d(LOG_TAG, "Received Broadcast: "+action);
              if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action) || UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {
  
                  updateUSBstatus();
                  Log.d(LOG_TAG, "USB Connected..");
              } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action) || UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
                  UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                  if (device != null) {
                      updateUSBstatus();
                  }
                  Log.d(LOG_TAG, "USB Disconnected..");
              }
          }
      };
  
      IntentFilter filter = new IntentFilter();
      filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
      filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
      //filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
      //filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
      registerReceiver(mUsbReceiver , filter);
      Log.d(LOG_TAG, "mUsbReceiver Registered");
      }
  
      @Override
          public void onResume() {
              super.onResume();
              Log.d(LOG_TAG, "App Resumed..");
              //Refernce: https://stackoverflow.com/questions/18015656/cant-receive-broadcast-intent-of-usbmanager-action-usb-device-attached-usbmanag
              Intent intent = getIntent();
              if (intent != null) {
                  if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
                      Toast.makeText(getApplicationContext(), "Attached", Toast.LENGTH_SHORT).show();
                  } else if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
                      Toast.makeText(getApplicationContext(), "Detached", Toast.LENGTH_SHORT).show();
                  }
              }
          }
  }
  </code></pre>
  
  <p>I also checked this answer: <a href="https://stackoverflow.com/questions/18015656/cant-receive-broadcast-intent-of-usbmanager-action-usb-device-attached-usbmanag">Can't receive broadcast Intent of UsbManager.ACTION_USB_DEVICE_ATTACHED/UsbManager.ACTION_USB_DEVICE_DETACHED</a>, but it didn't help..</p>
  
  <p>Can someone please point out where I'm wrong??</p>