6

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:

  • Device is charging through the USB connector(no separate charger)
  • Device is not connected to a computer
  • Some sort of way to separate the slow charging dock from a standard charger

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?

matt.
  • 2,355
  • 5
  • 32
  • 43
Magakahn
  • 498
  • 9
  • 31
  • The slow charging warning has nothing to do with being docked or not. there are also slow charging chargers that use wall socket. There would be no way to detect that specific dock. That dock doesn't work correctly – Ivo Nov 27 '13 at 14:14

4 Answers4

4

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.

Jatin Malwal
  • 5,133
  • 2
  • 23
  • 26
  • I already know this, but the problem is that this "dock" does not send a dock event. Thanks anyways! – Magakahn Nov 21 '13 at 14:42
  • You can check your code by externally send broadcast by adb. It may be possible that u r not receiving broadcast in perticular device – Jatin Malwal Nov 22 '13 at 08:58
  • adb shell am broadcast -a android.intent.action.POWER_CONNECTED -n com.jm.monitoringbatterydemo/.PowerConnectionReceiveradb shell am – Jatin Malwal Nov 22 '13 at 08:59
  • How is that going to solve my problem. My problem is that I have to make a BroadcastReceiver that only reacts when it is connected to this type of device. It does not send a DOCK_EVENT of any kind. – Magakahn Nov 22 '13 at 18:59
  • Make a receiver which react when dock event is happened but non of the above mentioned specific event occur. – Jatin Malwal Nov 22 '13 at 19:34
  • I do not want it to react when it is connected to a computer or a charger. – Magakahn Nov 22 '13 at 19:43
  • Just place some checks there. When it is connected to charger or computer there is some notifiers as well. So when when you find something identical don't react to it otherwise do what you want to do in receiver, – Jatin Malwal Nov 22 '13 at 20:07
  • That is the core of the problem. I am unable to find a single or a combination of checks/receivers that only reacts when it is connected to this "dock". – Magakahn Nov 23 '13 at 11:56
  • 1
    What is if this is a particular problem of your used dock? My Dock *does* actually broadcast a DOCK_EVENT on my GalaxyNexus – Rafael T Nov 27 '13 at 09:41
  • Is it the exact same dock, or just a similar one? – Magakahn Nov 27 '13 at 14:28
  • I'll also suggest you to try the different dock with differnt device. or see my edit... – Jatin Malwal Nov 30 '13 at 07:22
0

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

Voidpaw
  • 910
  • 1
  • 5
  • 18
  • I can not figure out a way for it to only react when docked and not connected to a charger. Thanks anyways. – Magakahn Nov 27 '13 at 14:27
0

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?

kirsche40
  • 981
  • 1
  • 10
  • 19
0

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;