4

I'm having trouble trying to access most of the stats from my Android device's battery such as BATTERY_PROPERTY_CAPACITY, BATTERY_PROPERTY_CHARGE_COUNTER or BATTERY_PROPERTY_CURRENT_AVERAGE. These properties are all clearly documented here: http://developer.android.com/reference/android/os/BatteryManager.html

I have the following permission declared on my manifest:

<uses-permission android:name="android.permission.BATTERY_STATS" />

I have a non-null BatteryManager instance:

mBatteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);

And whenever I try to retrieve these stats:

double remainingCapacity = mBatteryManager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CAPACITY);
double batteryCapacityMicroAh = mBatteryManager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
double averageCurrentMicroA = mBatteryManager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);

The results are all 0 for all of those. Always, without fail, 0. I've tried this on an emulator, an actual device, and everything I've thought of has failed to modify my results.

I'd greatly appreciate any help. Ultimately, what I'm trying to do is calculate the time remaining for a device to reach its full charge (while charging). Thanks in advance.

Sergio Morales
  • 2,600
  • 6
  • 32
  • 40
  • I believe you have to use the ACTION_BATTERY_CHANGED intent, take a look at [this question](http://stackoverflow.com/questions/12283454/getting-battery-status-from-a-service-in-android). – Jack Wilsdon Aug 28 '15 at 22:33
  • You don't need that permission AFAIK, as you can't hold it anyway (it is `signature` or `system`). Try `getLongProperty()` and see if you get better results. – CommonsWare Aug 28 '15 at 22:37
  • @jackwilsdon: The intent doesn't provide any of the properties I need (I need those that are queried through the getIntProperty methods), not EXTRA_SCALE or EXTRA_LEVEL. Please re-read the question. – Sergio Morales Aug 30 '15 at 05:42
  • It looks like [someone has done it successfully using Reflection](http://stackoverflow.com/questions/22243461/android-is-there-anyway-to-get-battery-capacity-of-a-device-in-mah). – Jack Wilsdon Aug 30 '15 at 14:32

4 Answers4

4

Not sure if this is any help for you now, but only phones that have a battery fuel gauge (https://source.android.com/devices/tech/power/device.html) can report back some of the values you are looking for. AFAIK, only the Nexus 6, 9, and 10 gives all three values you are looking for, with Nexus 5 giving only BATTERY_PROPERTY_CAPACITY.

Pablo Baxter
  • 2,144
  • 1
  • 17
  • 36
0

You need to register for Action_Battery_Changed broadcast . This will get invoked whenever this event occurs. Since this is a sticky Intent, it keeps on broadcasting once registered. The method below would get the current level of battery.

private void getBatteryPercentage() {
    BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            context.unregisterReceiver(this);
            int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            int level = -1;
            if (currentLevel >= 0 && scale > 0) {
                level = (currentLevel * 100) / scale;
            }
            Toast.makeText(MainActivity.this, level + "%", Toast.LENGTH_LONG).show();
        }
    };
    IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
Mohammad Hammadi
  • 733
  • 2
  • 11
  • 34
  • None of those properties are the ones I require. Please re-read the question. The properties I need are BATTERY_PROPERTY_CAPACITY and the others, which are NOT provided through the ACTION_BATTERY_CHANGED intent. – Sergio Morales Aug 29 '15 at 18:02
0

Value of BatteryManager.EXTRA_LEVEL is current battery capacity. I guess you are looking for the total capacity(full), you need to read /sys/class/power_supply/battery/charge_full_design or charge_full, different manufacturers may have different path.

e13386
  • 1
0

Works real device api 30, without permission

val batteryManager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
val capacity = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
val counter = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER)
val average = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE)
Arman
  • 25
  • 7