Identify a specific charger input

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

Since I'm building an application, relying on the ability to determine when the device was docked or undocked. Therefore, I need to filter and separate these events:

  • The device charges via USB (without a separate charger)
  • The device is not connected to the computer
  • Some way to separate a slow charging dock from a standard charger.

I noticed that my device (LG optimus 4x HD) manages to react differently for each of these actions. When it is connected to a standard charger, it does not give a notification message when it is connected to a computer, it tells me that the USB mode is activated, and when it is connected to the dock, it gives me a warning about a slow charge.

I need to create a system with the same ability to separate these actions and respond to them. So far, I just made a simple BroadcastReceiver that responds if the device is connected or not connected to the charger. I also managed to monitor the charging status using the code found in the documentation .

Is there a way to determine this particular charger input?

+6
source share
4 answers

Whenever a device is docked or undocked, the ACTION_DOCK_EVENT action is broadcast. To track changes in the status of the device dock, simply register the broadcast receiver in the application manifest, as shown in the following snippet below:

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

If the device is connected, it can be docked in any of the four types of dock:

  • Car
  • Desktop
  • Low Screen (Analog) Table
  • High quality (digital) table

Dock status details are included as optional in the sticky translation of ACTION_DOCK_EVENT. Since it is sticky, you do not need to register BroadcastReceiver. You can simply call registerReceiver (), passing zero as a broadcast receiver, as shown in the following snippet.

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

You can extract the current docking status from EXTRA_DOCK_STATE additionally:

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

The status of the dock can be found at

 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 application still does not receive broadcast, try this code to send a 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.

+4
source

What you can do to solve this problem is look at the% of battery remaining on your phone. Then you can determine that the value is growing, which means that it is docked. The only problem with this may be that if you sometimes charge it with a regular charger, rather than a dock, you should still react as if it were one.

Additional information on battery level, etc.: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

0
source

It would be useful 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 ?

0
source

We 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; 
0
source

Source: https://habr.com/ru/post/957706/


All Articles