Skip to content Skip to sidebar Skip to footer

Android Honeycomb 3.2 Detect Mtp Connection Via Usb

I saw other posts about detecting a USB connection, but they didn't talk about MPT specifically and just a more general detection so I figured I'd ask specifically about MTP. I'm c

Solution 1:

I was wondering the same thing and I came up with an alternative:

I check the batery state. There is an intent called ACTION_BATTERY_CHANGED called when an event happens on the battery. In this intent there are some extra fields containing some informations. One of them is EXTRA_PLUGGED:

Indicating whether the device is plugged into a power source; 0 means it ison battery, other constants are different types of power sources.

The other constants are BATTERY_PLUGGED_AC and BATTERY_PLUGGED_USB

So with this broadcast you can know if the Smartphone has been plugged in USB even if it uses the MTP protocol.

To know if the Smartphone is unplugged you juste have to check when the EXTRA_PLUGGED value changes from BATTERY_PLUGGED_USB to 0

Solution 2:

Listen to this broadcast android.hardware.usb.action.USB_STATE I have tested it and it works with PTP or MTP enabled device. there is a boolean extra "connected" that will tell you if USB cable is connected or not.

Post a Comment for "Android Honeycomb 3.2 Detect Mtp Connection Via Usb"