-3

Hy i am just developing and energy profiling application for which i need to have current energy of battery in nanowatt-hours. I have tried using Battery Manager API but the problem with it is that it requires a certain micro-chip called fuel-gauge to get these values. My question is that is there any other way of getting current energy of battery without using fuel gauge? Also i have managed to get current capacity of battery in percentage using BatteryManager API which means that my code is working fine.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Saad Saeed
  • 83
  • 6
  • you can use 'BatteryManager bm = (BatteryManager)getSystemService(BATTERY_SERVICE); int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);' – Anand Jain Nov 17 '17 at 10:29

1 Answers1

0

I have solution for your problem, use this code:

   BatteryManager bm = (BatteryManager)getSystemService(BATTERY_SERVICE);
    int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

    double capacity = getBatteryCapacity(this);

    double remaining = (capacity*batLevel)/100;


    Log.d("Capapcity Remaining",remaining+" ");

and getBatteryCapacity() implementation is here.

  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;

}

Note: it will not give exact result, but will provide appropriate result for your need.

Ankit Patidar
  • 2,731
  • 1
  • 14
  • 22