Vikram
Vikram

Reputation: 11

How to detect my android device is connected with usb or car dock through Broadcast Receiver?

I am trying with Broadcast Receiver to get the my device is connected to usb or car dock but not getting the proper result. Please help? Thanks in advance. Receiver code is:

public class CarDockReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Car Dock Receiver registerd", Toast.LENGTH_SHORT).show();
        switch (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)) {
        case BatteryManager.BATTERY_PLUGGED_AC:
            Toast.makeText(context, "Battery plugged AC", Toast.LENGTH_SHORT).show();
            break;
        case BatteryManager.BATTERY_PLUGGED_USB:
            Toast.makeText(context, "Battery plugged USB", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
        }
    }
}

receiver in Manifest file is:

<receiver
     android:name=".CarDockReceiver"
     android:enabled="true" >
     <intent-filter>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
     </intent-filter>
</receiver>

Upvotes: 1

Views: 10808

Answers (2)

fredvanl
fredvanl

Reputation: 193

Just solved a similar problem with detecting the insertion of USB devices. It turns out that - because you specified an intent filter in the manifest - Android calls onResume when something is plugged in. You might try adding this:

@Override
protected void onResume() {
    super.onResume();

    Intent intent = getIntent();
    if (intent != null) {
        Log.d("onResume", "intent: " + intent.toString());
        if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
            // Do your thing ...
        }

Run it to see what is exactly logged and use that information to check for the correct action to check for (replace the ACTION_USB_DEVICE_ATTACHED here in the above example).

Upvotes: 1

sam
sam

Reputation: 2820

To check whether your device is connected to usb accessory, you can use this intent.

<activity ...>
    ...
    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
    </intent-filter>

    <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
     android:resource="@xml/accessory_filter" />
</activity>

Reference: http://developer.android.com/guide/topics/connectivity/usb/accessory.html

If you are just detecting the usb connection, then just use these intents for your intent filter: UsbManager.ACTION_USB_DEVICE_ATTACHED and UsbManager.ACTION_USB_DEVICE_DETACHED

Hope this helps.

UPDATE:

If you are dealing with accessories that provide power, you can also detect the connection using this intent: ACTION_POWER_CONNECTED. This intent is broadcasted when external power has been connected to the device. Here is a sample code.

In your AndroidManifest.xml

<receiver android:name=".YourReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
        <action android:name="android.intent.action.BATTERY_CHANGED" />
    </intent-filter>
</receiver>

and your receiver.java source:

public class YourReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("connection", "power connected");
    }
}

Upvotes: 0

Related Questions