Total Battery Capacity in mAh of device Programmatically
Asked Answered
U

2

12

I have tried powerprofile of Android....I have tried this code...but it gives me 1000 answer every time in all the devices... Is there any other way in android to get battery capacity... Eg.if mobile device capacity is 2000mAh it should return me 2000

public Double getBatteryCapacity() {

        Object mPowerProfile_ = null;
        double batteryCapacity = 0;
        final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";
        try {
            mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS)
                    .getConstructor(Context.class).newInstance(this);

        } catch (Exception e) {

            // Class not found?
            e.printStackTrace();
        }

        try {

            // Invoke PowerProfile method "getAveragePower" with param
            // "battery.capacity"
            batteryCapacity = (Double) Class.forName(POWER_PROFILE_CLASS)
                    .getMethod("getAveragePower", java.lang.String.class)
                    .invoke(mPowerProfile_, "battery.capacity");

        } catch (Exception e) {

            // Something went wrong
            e.printStackTrace();
        }

        return batteryCapacity;
    }
Unconquerable answered 17/1, 2015 at 9:25 Comment(4)
possible duplicate of Android get battery current capacity in mA and total capacity of battery in mAhDonor
Ranjith but it is not working that's why i posted this...Can u help me ?Unconquerable
possible duplicate of Android : Is there anyway to get battery capacity of a device in mah?Estrogen
You should use "getBatteryCapacity" method, not the "getAveragePower" method of the PowerProfile class.Collage
C
19

For those users interested in the implementation of the @Yehan suggestions, here is a method that return the total battery capacity: (only for API Level >= 21)

public long getBatteryCapacity(Context context) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        BatteryManager mBatteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
        Integer chargeCounter = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
        Integer capacity = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

        if(chargeCounter == Integer.MIN_VALUE || capacity == Integer.MIN_VALUE)
            return 0;

        return (chargeCounter/capacity) *100;
    }
    return 0;
}

Unfortunately, for some reason this approach doesn't always work. In those cases, you can use Java's Reflection APIs to retreive the value returned by getBatteryCapacity method of com.android.internal.os.PowerProfile :

public double getBatteryCapacity(Context context) {
    Object mPowerProfile;
    double batteryCapacity = 0;
    final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

    try {
        mPowerProfile = Class.forName(POWER_PROFILE_CLASS)
                .getConstructor(Context.class)
                .newInstance(context);

        batteryCapacity = (double) Class
                .forName(POWER_PROFILE_CLASS)
                .getMethod("getBatteryCapacity")
                .invoke(mPowerProfile);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return batteryCapacity;

}

Source: https://android.googlesource.com/platform/frameworks/base/+/a029ea1/core/java/com/android/internal/os/PowerProfile.java

Collage answered 13/10, 2017 at 19:14 Comment(4)
Can I get the value of Current flow through JAVA api?Heterochromatin
As far as I know, i think there is not a way to get the value of the Current flow. But i suggest you to search in the internals of the AOSP for something so specific. Maybe i'm wrong. I would start from here: android.googlesource.com/platform/frameworks/base/+/master/…Collage
The first solution crashes my app java.lang.SecurityException: uid *** does not have android.permission.UPDATE_DEVICE_STATS. Adding it to manifest says that "Permission is only granted to system apps".Automobile
First solution is using a standard API, however it never produces the same results. At 59% capacity, the actual remaining mAh capacity will slowly drop, hence total capacity will slowly drop as well and jump higher on next %. Second solution is grey-listed for now and may be removed by Google at any time.Gemagemara
G
3

You can use following properties of android.os.BatteryManager. BATTERY_PROPERTY_CHARGE_COUNTER which gives you the remaining battery capacity in microampere-hours. BATTERY_PROPERTY_CAPACITY which gives you the remaining battery capacity as an integer percentage.

So,

BATTERY_PROPERTY_CHARGE_COUNTER / BATTERY_PROPERTY_CAPACITY * 100

would give you the Total Battery Capacity. This would not be the precise calculation but you will be able to close the actual capacity.

References: https://source.android.com/devices/tech/power/index.html#device-power http://developer.android.com/reference/android/os/BatteryManager.html

Geyser answered 4/5, 2015 at 9:6 Comment(1)
Its only available to android 5.0 not for lower version.Unconquerable

© 2022 - 2024 — McMap. All rights reserved.