Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the battery current values for the Android Phone

I am trying to collect power usage statistics for the Android G1 Phone. I am interested in knowing the values of Voltage and Current, and then able to collect statistics as reported in this PDF.

I am able to get the value of Battery voltage through registering for an intent receiver to receive the Broadcast for ACTION_BATTERY_CHANGED. But the problem is that Android does not expose the value of current through this SDK interface.

One way I tried is via sysfs interface, where I can view the battery current value from adb shell, using the following command

$cat /sys/class/power_supply/battery/batt_current 449  

But that too works only if the phone is connected via USB interface. If I disconnect the phone, I see the value of batt_current as '0'. I am not sure why the value of current reported is zero. It should be more than zero, right?

Any suggestion / pointers for getting battery current value? Also please correct me if I am wrong.

like image 813
Chintan Parikh Avatar asked Mar 13 '10 19:03

Chintan Parikh


People also ask

How do I check battery strength on Android?

Go to settings > Battery and device care > Diagnostics. You can now tap on battery status to check its health status. There are other features as well (Camera, speaker and more) of the phone that you can test to see if they are working fine or you should get them fixed.

What is the code to check battery capacity in Android?

Most Android devices offer the ability to check battery health using a secret code, something similar to check account balance. To check battery health via secret code, dial *#*#4636#*#* and choose Battery Information. This secret code also works on select Android smartphones and may or may not work for you.

How do I get my battery capacity?

Open your phone's Settings app. Under "Battery," see how much charge you have left, and about how long it will last. For details, tap Battery.


2 Answers

You could just look at the source code for the Current Widget. It has hard coded path's to where certain platforms store the current values.

/*  *  Copyright (c) 2010-2011 Ran Manor  *    *  This file is part of CurrentWidget.  *      *  CurrentWidget is free software: you can redistribute it and/or modify  *  it under the terms of the GNU General Public License as published by  *   the Free Software Foundation, either version 3 of the License, or  *  (at your option) any later version.  *  *  CurrentWidget is distributed in the hope that it will be useful,  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  *  GNU General Public License for more details.  *  *  You should have received a copy of the GNU General Public License  *  along with CurrentWidget.  If not, see <http://www.gnu.org/licenses/>. */  package com.manor.currentwidget.library;  import java.io.File;  import android.os.Build; import android.util.Log;  public class CurrentReaderFactory {      static public Long getValue() {          File f = null;                // htc desire hd / desire z / inspire?         if (Build.MODEL.toLowerCase().contains("desire hd") ||                 Build.MODEL.toLowerCase().contains("desire z") ||                 Build.MODEL.toLowerCase().contains("inspire")) {              f = new File("/sys/class/power_supply/battery/batt_current");             if (f.exists()) {                 return OneLineReader.getValue(f, false);             }         }          // nexus one cyangoenmod         f = new File("/sys/devices/platform/ds2784-battery/getcurrent");         if (f.exists()) {             return OneLineReader.getValue(f, true);         }          // sony ericsson xperia x1         f = new File("/sys/devices/platform/i2c-adapter/i2c-0/0-0036/power_supply/ds2746-battery/current_now");         if (f.exists()) {             return OneLineReader.getValue(f, false);         }          // xdandroid         /*if (Build.MODEL.equalsIgnoreCase("MSM")) {*/             f = new File("/sys/devices/platform/i2c-adapter/i2c-0/0-0036/power_supply/battery/current_now");             if (f.exists()) {                 return OneLineReader.getValue(f, false);             }         /*}*/          // droid eris         f = new File("/sys/class/power_supply/battery/smem_text");               if (f.exists()) {             Long value = SMemTextReader.getValue();             if (value != null)                 return value;         }          // htc sensation / evo 3d         f = new File("/sys/class/power_supply/battery/batt_attr_text");         if (f.exists())         {             Long value = BattAttrTextReader.getValue();             if (value != null)                 return value;         }          // some htc devices         f = new File("/sys/class/power_supply/battery/batt_current");         if (f.exists())             return OneLineReader.getValue(f, false);          // nexus one         f = new File("/sys/class/power_supply/battery/current_now");         if (f.exists())             return OneLineReader.getValue(f, true);          // samsung galaxy vibrant                f = new File("/sys/class/power_supply/battery/batt_chg_current");         if (f.exists())             return OneLineReader.getValue(f, false);          // sony ericsson x10         f = new File("/sys/class/power_supply/battery/charger_current");         if (f.exists())             return OneLineReader.getValue(f, false);          // Nook Color         f = new File("/sys/class/power_supply/max17042-0/current_now");         if (f.exists())             return OneLineReader.getValue(f, false);          return null;     } } 
like image 184
Kevin Parker Avatar answered Sep 22 '22 04:09

Kevin Parker


API 21 onwards we can get the instantaneous battery current in microamperes, as an integer. Developer docs

BatteryManager mBatteryManager = (BatteryManager) getSystemService(Context.BATTERY_SERVICE);     Long avgCurrent = null, currentNow = null;     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {         avgCurrent = mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);         currentNow = mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);     }     Log.d(TAG, "BATTERY_PROPERTY_CURRENT_AVERAGE = " + avgCurrent + "mAh");     Log.d(TAG, "BATTERY_PROPERTY_CURRENT_NOW =  " + currentNow + "mAh"); 

Using mBatteryManager you can get the instantaneous current reading.

Measuring Device Power and reading power consumption and the available properties on NEXUS devices. Android open source docs

like image 31
LokiDroid Avatar answered Sep 24 '22 04:09

LokiDroid