Skip to content Skip to sidebar Skip to footer

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 reporte

Solution 1:

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;

publicclassCurrentReaderFactory{

    static publicLong 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);

        returnnull;
    }
}

Solution 2:

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

BatteryManagermBatteryManager= (BatteryManager) getSystemService(Context.BATTERY_SERVICE);
    LongavgCurrent=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

Solution 3:

using this function get Voltage Temperature Current in all devices.

in OnCreate to register broadcast reciever

this.registerReceiver(this.BatteryInfo, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

and create Broadcast Receiver

privateBroadcastReceiverBatteryInfo=newBroadcastReceiver() {
    @OverridepublicvoidonReceive(Context ctxt, Intent intent) {
        intlevel= intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        intscale= intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);

        booleanisPresent= intent.getBooleanExtra("present", false);

        Bundlebundle= intent.getExtras();
        Stringstr= bundle.toString();
        Log.i("Battery Info", str);

        if (isPresent) {
            intpercent= (level * 100) / scale;

            technology.setText("Technology: "+bundle.getString("technology"));
            voltage.setText("Voltage: "+bundle.getInt("voltage")+"mV");
            temp.setText("Temperature: "+bundle.getInt("temperature"));
            curent.setText("Current: "+bundle.getInt("current_avg"));
            health.setText("Health: "+getHealthString(health_));
            charging.setText("Charging: "+getStatusString(status) + "(" +getPlugTypeString(pluggedType)+")");
            battery_percentage.setText("" + percent + "%");


        } else {
            battery_percentage.setText("Battery not present!!!");
        }
    }   
};


 private String getPlugTypeString(int plugged) {
    StringplugType="Unknown";

    switch (plugged) {
    case BatteryManager.BATTERY_PLUGGED_AC:
        plugType = "AC";
        break;
    case BatteryManager.BATTERY_PLUGGED_USB:
        plugType = "USB";
        break;
    }
    return plugType;
}

private String getHealthString(int health) {
    StringhealthString="Unknown";
    switch (health) {
    case BatteryManager.BATTERY_HEALTH_DEAD:
        healthString = "Dead";
        break;
    case BatteryManager.BATTERY_HEALTH_GOOD:
        healthString = "Good Condition";
        break;
    case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
        healthString = "Over Voltage";
        break;
    case BatteryManager.BATTERY_HEALTH_OVERHEAT:
        healthString = "Over Heat";
        break;
    case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
        healthString = "Failure";
        break;
    }
    return healthString;
}
private String getStatusString(int status) {
    StringstatusString="Unknown";

    switch (status) {
    case BatteryManager.BATTERY_STATUS_CHARGING:
        statusString = "Charging";
        break;
    case BatteryManager.BATTERY_STATUS_DISCHARGING:
        statusString = "Discharging";
        break;
    case BatteryManager.BATTERY_STATUS_FULL:
        statusString = "Full";
        break;
    case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
        statusString = "Not Charging";
        break;
    }
    return statusString;
}

Solution 4:

i found this code on Intel android developer site

publicclassBatteryActivityextendsActivity {

    privatefinalStringTAG="SDP_BATTERY";
    privatefinalStringDEGREE_UNICODE="\u00B0";

    privateStringBuffertextBuffer=newStringBuffer();

    // a text view to show the status of the batteryprivate TextView mStatusTextView;
    // a text view to display the battery status iconprivate TextView mBatteryStatusIcon;

    /** Called when the activity is first created. */publicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.battery);

        mBatteryStatusIcon = (TextView) this.findViewById(R.id.statusBattIcon);
        mStatusTextView = (TextView) this.findViewById(R.id.statusEditText);
    }

    /**
     * Once onResume is called, the activity has become visible (it is now "resumed"). Comes after onCreate
     */protectedvoidonResume() {
        super.onResume();

        IntentFilterfilter=newIntentFilter();

        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        Log.d(TAG, "Register battery status receiver.");
        registerReceiver(mBroadcastReceiver, filter);
    }

    /**
     * Another activity takes focus, so this activity goes to "paused" state
     */protectedvoidonPause() {
        super.onPause();
        Log.d(TAG, "Unegister battery status receiver.");
        unregisterReceiver(mBroadcastReceiver);
    }

    /**
     * BroadcastReceiver is used for receiving intents (broadcasted messages) from the BatteryManager
     */privateBroadcastReceivermBroadcastReceiver=newBroadcastReceiver() {

        privatebooleanisHealth=false;

        publicvoidonReceive(Context context, Intent intent) {
            DecimalFormatformatter=newDecimalFormat();

            Stringaction= intent.getAction();

            // store battery information received from BatteryManagerif (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                Log.d(TAG, "Received battery status information.");
                intstatus= intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
                inthealth= intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
                booleanpresent= intent.getBooleanExtra(
                        BatteryManager.EXTRA_PRESENT, false);
                intlevel= intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
                intscale= intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
                inticon_small= intent.getIntExtra(
                        BatteryManager.EXTRA_ICON_SMALL, 0);
                intplugged= intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,
                        0);
                intvoltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,
                        0);
                inttemperature= intent.getIntExtra(
                        BatteryManager.EXTRA_TEMPERATURE, 0);
                Stringtechnology= intent
                        .getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);

                // display the battery icon that fits the current battery status (charging/discharging)
                mBatteryStatusIcon.setCompoundDrawablesWithIntrinsicBounds(icon_small, 0, 0, 0);

                // create TextView of the remaining information , to display to screen.StringstatusString="";

                switch (status) {
                case BatteryManager.BATTERY_STATUS_UNKNOWN:
                    statusString = "unknown";
                    break;
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    statusString = "charging";
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    statusString = "discharging";
                    break;
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                    statusString = "not charging";
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    statusString = "full";
                    break;
                }

                StringhealthString="";

                switch (health) {
                case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                    healthString = "unknown";
                    break;
                case BatteryManager.BATTERY_HEALTH_GOOD:
                    healthString = "good";
                    isHealth = true;
                    break;
                case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                    healthString = "overheat";
                    break;
                case BatteryManager.BATTERY_HEALTH_DEAD:
                    healthString = "dead";
                    break;
                case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                    healthString = "over voltage";
                    break;
                case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                    healthString = "unspecified failure";
                    break;
                }

                StringacString="";

                switch (plugged) {
                case BatteryManager.BATTERY_PLUGGED_AC:
                    acString = "plugged AC";
                    break;
                case BatteryManager.BATTERY_PLUGGED_USB:
                    acString = "plugged USB";
                    break;
                default:
                    acString = "not plugged";
                }

                textBuffer =  newStringBuffer();
                textBuffer.append("status:" + statusString + "\n");

                formatter.applyPattern("#");
                StringlevelStr= formatter.format( (float)level/scale * 100 );
                textBuffer.append("level:" + levelStr + "% (out of 100)\n");
                textBuffer.append("health:" + healthString + "\n");

                textBuffer.append("present?:" + String.valueOf(present) + "\n");

                textBuffer.append("plugged?:" + acString + "\n");

                // voltage is reported in millivolts
                formatter.applyPattern(".##");
                StringvoltageStr= formatter.format( (float)voltage/1000 );
                textBuffer.append("voltage:" + voltageStr + "V\n");

                // temperature is reported in tenths of a degree Centigrade (from BatteryService.java)
                formatter.applyPattern(".#");
                StringtemperatureStr= formatter.format( (float)temperature/10 );
                textBuffer.append("temperature:" + temperatureStr
                        + "C" + DEGREE_UNICODE + "\n");

                textBuffer.append("technology:" + String.valueOf(technology)
                        + "\n");

                mStatusTextView.setText(textBuffer.toString());

                if (isHealth) {
                    Log.d(TAG, "Battery health: " + healthString);
                    Log.d(TAG, "UMSE_BATTERY_SUCCESSFULLY");
                } else {
                    Log.d(TAG, "UMSE_BATTERY_FAILED");
                }

                Log.d(TAG, textBuffer.toString());

                //finish();
            }
        }
    };

Solution 5:

After several experiments and help from various other groups, I found out that there is no way of getting Battery Current value through software only (as its not supported in h/w). Only way I found was to measure current flowing through battery by means of multimeter.

Post a Comment for "Getting The Battery Current Values For The Android Phone"