Methods for accessing battery status on an android device

Hi,
How is the battery status on an android device accessed?

Any help is much appreciated.

Cheers.

Hi,
update: It looks like there are code examples using battery manager method from ‘android.os.BatteryManager’ at the developer.android.com website however i also found that if i use the ADB shell and go to the power_supply/battery directory i can get access to lots of battery information:

adb shell
cactus:/ $ cd /sys/class/power_supply/battery
cactus:/sys/class/power_supply/battery $ cat uevent
POWER_SUPPLY_NAME=battery
POWER_SUPPLY_STATUS=Charging
POWER_SUPPLY_HEALTH=Good
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_TECHNOLOGY=Li-poly
POWER_SUPPLY_CYCLE_COUNT=88
POWER_SUPPLY_CAPACITY=75
POWER_SUPPLY_CURRENT_NOW=401800
POWER_SUPPLY_CURRENT_AVG=324000
POWER_SUPPLY_VOLTAGE_NOW=4192000
POWER_SUPPLY_CHARGE_FULL=2765
POWER_SUPPLY_CHARGE_COUNTER=2073
POWER_SUPPLY_CHARGE_FULL_DESIGN=3000
POWER_SUPPLY_TEMP=180
POWER_SUPPLY_THERMAL_CONTROL_LIMIT=-1,-1
POWER_SUPPLY_INPUT_CURRENT_NOW=500000
POWER_SUPPLY_VOLTAGE_MAX=4400000
POWER_SUPPLY_CONSTANT_CHARGE_CURRENT_MAX=1100000

So i suppose the question is; is there a way to access this information from within processing script/java code?

Any help is much appreciated.

Cheers.

I’ve almost got it working, this should give you the percentage, you’ll have to rearrange the class yourself to extends functionality.

import android.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;

import android.content.Intent;
import android.app.Activity;
import android.content.Context;

import android.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;

Activity act;
Context ctx;
Battp bp;

int level;
void setup() {
  act = this.getActivity();
  ctx = act.getApplicationContext();
  bp = new Battp();
};

void draw() {
};

void mousePressed() {
  println(getBatteryCapacity(ctx));
  //println((batLevel));
};

public class Battp extends Activity {
  float batteryPercentage,batteryCapacity;

  Battp() {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = act.registerReceiver(null, ifilter);
    level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    getBatteryCapacity(ctx);
    double currentCapacity = (level * getBatteryCapacity(ctx)) / 100;
    println("level", level);
    println("cc", currentCapacity);
  };

  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;
  };
  //return level;
};

Hi,
And thanks for the reply and also thanks for your efforts on this, much appreciated.

I tried you code and unfortunately it failed with the following error related to the getBatteryCapacity() method:

> Task :app:compileDebugJavaWithJavac FAILED
----------
1. ERROR in /tmp/android1857772923646380959sketch/app/src/main/java/processing/test/andbatcapacity_v2/AndBatCapacity_V2.java (at line 55)
	println(getBatteryCapacity(ctx));
	        ^^^^^^^^^^^^^^^^^^
The method getBatteryCapacity(Context) is undefined

Unfortunately my programming skills are not good enough to know what this means.

Any help with this is much appreciated.

Cheers.

Woops that line should have been commented or removed, it should work.