Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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?

like image 897
Magakahn Avatar asked Nov 08 '13 21:11

Magakahn


1 Answers

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.

like image 169
Jatin Malwal Avatar answered Sep 22 '22 13:09

Jatin Malwal