Magakahn
Magakahn

Reputation: 508

Determine specific charger input

I am currently trying to make a reliable way to determine a specific charger type, in my case a music dock like this. The problem is that this dock unfortunately does not send a dock event when docked.

Since I am making an app relying on being able to determine when the device has been docked or undocked. I therefore need a way to filter out and separate these events:

I have noticed that my device (LG optimus 4x HD) manages to react differently for every one of these actions. When it is connected to a standard charger it gives no notification message, when it is connected to a computer it tells me USB mode has been activated, and when it is connected to the dock it gives me a slow charger warning.

I need to make a system with the same ability to separate these actions and react to them. Until now I have only made a simple BroadcastReceiver that reacts if the device is connected or unconnected to a charger. I have also managed to monitor the charging state using the code found in the documentation.

Is there any way of determine this specific charger input?

Upvotes: 6

Views: 1542

Answers (4)

user3368570
user3368570

Reputation: 64

Hope this code helps:

    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = mContext.registerReceiver(null, ifilter);

    // Are we charging / charged?
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
            status == BatteryManager.BATTERY_STATUS_FULL;

    // How are we charging?
    int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    boolean usbCharge = chargePlug == 2;
    boolean acCharge = chargePlug == 1;

    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    float batteryPct = level / (float)scale;

Upvotes: 0

Jatin Malwal
Jatin Malwal

Reputation: 5263

Whenever the device is docked or undocked, the ACTION_DOCK_EVENT action is broadcast. To monitor changes in the device's dock-state, simply register a broadcast receiver in your application manifest as shown in the snippet below:

<action android:name="android.intent.action.ACTION_DOCK_EVENT"/>

If a device is docked, it can be docked in any one of four different type of dock:

  • Car
  • Desk
  • Low-End (Analog) Desk
  • High-End (Digital) Desk

The dock-state details are included as an extra in a sticky broadcast of the ACTION_DOCK_EVENT action. Because it's sticky, you don't need to register a BroadcastReceiver. You can simply call registerReceiver() passing in null as the broadcast receiver as shown in the next snippet.

IntentFilter ifilter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
Intent dockStatus = context.registerReceiver(null, ifilter);

You can extract the current docking status from the EXTRA_DOCK_STATE extra:

int dockState = battery.getIntExtra(EXTRA_DOCK_STATE, -1);
boolean isDocked = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;

You can find the dock state by

boolean isCar = dockState == EXTRA_DOCK_STATE_CAR;
boolean isDesk = dockState == EXTRA_DOCK_STATE_DESK || 
                 dockState == EXTRA_DOCK_STATE_LE_DESK ||
                 dockState == EXTRA_DOCK_STATE_HE_DESK;

EDIT :

If your app still not receiving the broadcast try this code to sent manual broadcast and check the code :

adb shell am broadcast -a android.intent.action.POWER_CONNECTED -n com.jm.monitoringbatterydemo/.PowerConnectionReceiver

Change the name of the broadcast and your receiver.

Upvotes: 4

kirsche40
kirsche40

Reputation: 1061

Would it be helpful to identify the power source using

  int   BATTERY_PLUGGED_AC          Power source is an AC charger.
  int   BATTERY_PLUGGED_USB         Power source is a USB port.
  int   BATTERY_PLUGGED_WIRELESS    Power source is wireless.

from BatteryManager?

Upvotes: 0

Voidpaw
Voidpaw

Reputation: 920

What you can do to solve this problem is look at the % of battery left on your phone. You can then determine that the value is going up, which means it is docked. Only problem with this could be that if you sometimes charge it on a regular charger rather than a dock, you'd still have it respond as if it were one.

More info on reading battery level etc: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

Upvotes: 0

Related Questions