2

I'm using the latest Google phone (Nexus 6 / Android 5.1.1) and I tried to use the new BatteryManager API:

getLongProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER)

but it returned Long.MIN_VALUE, which means the device does not support this property.
It doesn't make sense because from the offical document the device is supposed to support this property.

I used the following code to test this property. Am I missing anything?

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BatteryManager bm = (BatteryManager)this.getSystemService(Context.BATTERY_SERVICE);
        long energyCounter = bm.getLongProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER);
    }
}
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
Xavier
  • 65
  • 1
  • 6

1 Answers1

0

You can check /sys/class/power_supply path in adb shell.

You should be able to see the folders related to battery info. Folder names change from device to device. Go into the related folder.

For my case it's /sys/class/power_supply/battery. (Galaxy_S4)

Use cat uevent or cat related_file. Then you should be able to see if the battery driver informs about the information you seek for.

Note:

Android is responsible for only reading the supported values from the files which the battery driver provides. The calculation of remaining battery as micro amperes is being performed in battery driver source code. If the driver doesn't provide you the information, Android can not get the information.

For instance;

shell@ja3g:/ $ cd /sys/class/power_supply/battery
shell@ja3g:/sys/class/power_supply/battery $ cat uevent
POWER_SUPPLY_NAME=battery
POWER_SUPPLY_STATUS=Charging
POWER_SUPPLY_CHARGE_TYPE=Fast
POWER_SUPPLY_HEALTH=Good
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_ONLINE=4
POWER_SUPPLY_TECHNOLOGY=Li-ion
POWER_SUPPLY_VOLTAGE_NOW=4211000
POWER_SUPPLY_VOLTAGE_AVG=4211000
POWER_SUPPLY_CURRENT_NOW=360
POWER_SUPPLY_CURRENT_AVG=360
POWER_SUPPLY_CHARGE_NOW=1
POWER_SUPPLY_CAPACITY=87
POWER_SUPPLY_TEMP=315
POWER_SUPPLY_TEMP_AMBIENT=-250

shell@ja3g:/sys/class/power_supply/battery $ cat capacity
87

POWER_SUPPLY_CAPACITY=87 means battery driver calculates the remaining energy percentage as 87 and writes into capacity file in this folder. Android reads from this file and shows the battery percentage as 87. It's all about the driver.


Optionally,

There's an app which reads BatteryManager.BATTERY_PROPERTY_CURRENT_NOW from current_now file in the device. File location changes from device to device.

I'm working on Vestel Liva Android 5.1 phone and for my case current_now file path is:

/sys/devices/soc.0/qpnp-vm-bms-ffffffc073455400/power_supply/bms/current_now

Here's the link for the project source codes: CurrentWidget

Maybe you can find the file in your device related to BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER

Hope this helps,

Cheers

Community
  • 1
  • 1
Burak Day
  • 907
  • 14
  • 28