Search code examples
javaandroidandroid-intentbroadcastreceiverusb-drive

Detect thumb drive(flash drive) when plugged in android via OTG cable


I am trying to detect a flash drive plugged in an android tablet. Based on what i've seen is out there, i tried registering a receiver like below but that does not work:

 <receiver android:name=".receiver.UsbBroadcastReceiver"
              android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED"/>
            <data android:scheme="file"/>
        </intent-filter>
</receiver>

I have the following permissions:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_LOGS"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I don't see a security exception either. Is polling the only way?


Solution

  • Maybe a good place to start would be to look at the Dev Docs. Seems there are other actions associated with this:

    http://developer.android.com/reference/android/hardware/usb/UsbManager.html#ACTION_USB_ACCESSORY_ATTACHED

    What you're looking at is an External Media if I undestand correctly, which is not exactly the same as OTG