How is the Android battery health determined?

前端 未结 3 1977
无人及你
无人及你 2020-12-17 02:52

I am not talking about how to read the value.

Rather, I am interested in how the value of BatteryManager.EXTRA_HEALTH is being set.

Does it come from the fir

相关标签:
3条回答
  • 2020-12-17 03:21

    To be more specific: The battery health is determined by the driver (in the kernel). Said driver exports information via the sys file system (/sys/class/power_supply, to be exact). The Android daemon healthd (as of KK, 4.4.x) picks up this information, and implements an IBatteryPropertiesRegistrar interface on it. It polls continuously (using epoll_wait to run in a timeout loop for periodic updates, as well as netlink notifications for the POWER subsystem - say, for example, if you connect or disconnect a charger). The system's BatteryStatsService then registers a listener with healthd (via binder) and then gets the data that is published by healthd,

    Check out /sys/class/power_supply, and you'll see:

    lrwxrwxrwx root     root              1970-02-05 14:20 ac -> ../../devices/f9923000.i2c/i2c-84/84-006b/power_supply/ac
    lrwxrwxrwx root     root              1970-02-05 14:20 batt_therm -> ../../devices/battery_tm_ctrl.78/power_supply/batt_therm
    lrwxrwxrwx root     root              1970-02-05 14:20 battery -> ../../devices/f9923000.i2c/i2c-84/84-0036/power_supply/battery
    lrwxrwxrwx root     root              1970-02-05 14:20 touch -> ../../devices/virtual/power_supply/touch
    lrwxrwxrwx root     root              1970-02-05 14:20 usb -> ../../devices/msm_dwc3/power_supply/usb
    lrwxrwxrwx root     root              1970-02-05 14:20 wireless -> ../../devices/bq51013b_wlc.77/power_supply/wireless
    

    then check out "battery"

    /sys/devices/f9923000.i2c/i2c-84/84-0036/power_supply/battery:
    -r--r--r-- root     root         4096 2014-02-26 13:26 capacity
    -r--r--r-- root     root         4096 2014-02-26 13:26 charge_full_design
    -r--r--r-- root     root         4096 2014-02-26 13:26 current_now
    lrwxrwxrwx root     root              2014-02-26 13:26 device -> ../../../84-0036
    -r--r--r-- root     root         4096 2014-02-26 13:26 health
    drwxr-xr-x root     root              2014-02-26 13:26 power
    -r--r--r-- root     root         4096 2014-02-26 13:26 present
    -r--r--r-- root     root         4096 2014-02-26 13:26 status
    lrwxrwxrwx root     root              2014-02-26 13:26 subsystem -> ../../../../../../class/power_supply
    -r--r--r-- root     root         4096 2014-02-26 13:26 technology
    -r--r--r-- root     root         4096 2014-02-26 13:26 temp
    -r--r--r-- root     root         4096 2014-02-26 13:26 type
    -rw-r--r-- root     root         4096 2014-02-26 13:26 uevent
    -r--r--r-- root     root         4096 2014-02-26 13:26 voltage_max_design
    -r--r--r-- root     root         4096 2014-02-26 13:26 voltage_min_design
    -r--r--r-- root     root         4096 2014-02-26 13:26 voltage_now
    

    and by looking at the files, all details will be revealed.

    0 讨论(0)
  • 2020-12-17 03:26

    Lithium ion batteries have simple onboard computers that track and report the health; the state indicators are standardized but the conditions that trigger them to be reported by the battery are determined by the battery manufacturer.

    0 讨论(0)
  • 2020-12-17 03:35

    By using this code you can get information regarding battery..

    private BroadcastReceiver battery_receiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            boolean isPresent = intent.getBooleanExtra("present", false);
            String technology = intent.getStringExtra("technology");
            int plugged = intent.getIntExtra("plugged", -1);
            int scale = intent.getIntExtra("scale", -1);
            int health = intent.getIntExtra("health", 0);
            int status = intent.getIntExtra("status", 0);
            int rawlevel = intent.getIntExtra("level", -1);
            int level = 0;
            String temp=null;
    
            Bundle bundle = intent.getExtras();
    
            Log.i("BatteryLevel", bundle.toString());
    
            if(isPresent)
            {
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }
    
                String info = "Battery Level: " + level + "%\n";
    
                info += ("Technology: " + technology + "\n");
                info += ("Plugged: " + getPlugTypeString(plugged) + "\n");
                info += ("Health: " + getHealthString(health) + "\n");
                info += ("Status: " + getStatusString(status) + "\n");
                info += ("Temp: "+getTempStatus(temp,intent)+"\n");
    
                setBatteryLevelText(info + "\n\n" + bundle.toString());
            }
            else
            {
                setBatteryLevelText("Battery not present!!!");
            }
        }
    };
    private void registerBatteryLevelReceiver(){
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    
        registerReceiver(battery_receiver, filter);
    }
    
    0 讨论(0)
提交回复
热议问题