Android系统默认可通过dumpsys获取如下的电池信息:
adb shell dumpsys battery
Current Battery Service state: AC powered: false USB powered: true Wireless powered: false Max charging current: Max charging voltage: Charge counter: status: 2 health: 2 present: true level: 76 scale: 100 voltage: 4111 temperature: 268 technology: Li-ion
讯享网
对应代码实现位置为:aosp/frameworks/base/services/core/java/com/android/server/BatteryService.java
讯享网 private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) { synchronized (mLock) { if (args == null || args.length == 0 || "-a".equals(args[0])) { pw.println("Current Battery Service state:"); if (mUpdatesStopped) { pw.println(" (UPDATES STOPPED -- use 'reset' to restart)"); } pw.println(" AC powered: " + mHealthInfo.chargerAcOnline); pw.println(" USB powered: " + mHealthInfo.chargerUsbOnline); pw.println(" Wireless powered: " + mHealthInfo.chargerWirelessOnline); pw.println(" Max charging current: " + mHealthInfo.maxChargingCurrent); pw.println(" Max charging voltage: " + mHealthInfo.maxChargingVoltage); pw.println(" Charge counter: " + mHealthInfo.batteryChargeCounter); pw.println(" status: " + mHealthInfo.batteryStatus); pw.println(" health: " + mHealthInfo.batteryHealth); pw.println(" present: " + mHealthInfo.batteryPresent); pw.println(" level: " + mHealthInfo.batteryLevel); pw.println(" scale: " + BATTERY_SCALE); pw.println(" voltage: " + mHealthInfo.batteryVoltage); pw.println(" temperature: " + mHealthInfo.batteryTemperature); pw.println(" technology: " + mHealthInfo.batteryTechnology); } else { Shell shell = new Shell(); shell.exec(mBinderService, null, fd, null, args, null, new ResultReceiver(null)); } } }
电池常见log信息
# --------------------------- # BatteryService.java # --------------------------- 2722 battery_level (level|1|6),(voltage|1|1),(temperature|1|1) 2723 battery_status (status|1|5),(health|1|5),(present|1|5),(plugged|1|5),(technology|3) # This is logged when battery goes from discharging to charging. # It lets us count the total amount of time between charges and the discharge level 2730 battery_discharge (duration|2|3),(minLevel|1|6),(maxLevel|1|6)
另外/sys/class/power_supply下包括了所有的有关电池的信息,如下:
讯享网root:/sys/class/power_supply $ ls ac battery charger usb
power_supply文件夹之下的子文件夹下会有一个type,如:
root:/ $ cat /sys/class/power_supply/ac/type Mains root:/sys/class/power_supply $ ls ac battery charger usb root:/sys/class/power_supply $ cat */type Mains Battery Unknown USB
type的值反应了当前的文件中的属性,其字符串和其属性的对应关系为:
讯享网 {"Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN}, {"Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY}, {"UPS", ANDROID_POWER_SUPPLY_TYPE_AC}, {"Mains", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB", ANDROID_POWER_SUPPLY_TYPE_USB}, {"USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_C", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB}, {"Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS},
(1)当前的充电类型:
AC powered: false
USB powered: true
Wireless powered: false
对应节点:
/sys/class/power_supply/%s/online
(2)当前的充电电流与电压:
Charge counter: (瞬时电池容量 uA-h)
//* 当以上两者相乘是会得到其充电功率,上层软件以此基础确定当前是否为慢/快速充电。
SettingsLib 下的BatteryStatus.java
final int maxChargingMicroAmp = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1); int maxChargingMicroVolt = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1); if (maxChargingMicroVolt <= 0) { maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT; } if (maxChargingMicroAmp > 0) { // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor // to maintain precision equally on both factors. maxChargingWattage = (maxChargingMicroAmp / 1000) * (maxChargingMicroVolt / 1000); } else { maxChargingWattage = -1; } / * Return current chargin speed is fast, slow or normal. * * @return the charing speed */ public final int getChargingSpeed(Context context) { final int slowThreshold = context.getResources().getInteger( R.integer.config_chargingSlowlyThreshold); final int fastThreshold = context.getResources().getInteger( R.integer.config_chargingFastThreshold); return maxChargingWattage <= 0 ? CHARGING_UNKNOWN : maxChargingWattage < slowThreshold ? CHARGING_SLOWLY : maxChargingWattage > fastThreshold ? CHARGING_FAST : CHARGING_REGULAR; }
SettingsLib下的config.xml有快慢充电的值,如下:
讯享网 <!-- Threshold in micro watts below which a charger is rated as "slow"; 1A @ 5V --> <integer name="config_chargingSlowlyThreshold"></integer> <!-- Threshold in micro watts above which a charger is rated as "fast"; 1.5A @ 5V --> <integer name="config_chargingFastThreshold"></integer>
对应节点:
/sys/class/power_supply/%s/current_max
/sys/class/power_supply/%s/voltage_max
/sys/class/power_supply/battery/charge_counter
(3)电池状态:
status: 2
BatteryManager.BATTERY_STATUS_UNKNOWN = 1
BatteryManager.BATTERY_STATUS_CHARGING = 2
BatteryManager.BATTERY_STATUS_DISCHARGING = 3
对应节点:
/sys/class/power_supply/battery/status
(4)电池健康状态:
health: 2
BatteryManager.BATTERY_HEALTH_UNKNOWN = 1
BatteryManager.BATTERY_HEALTH_GOOD = 2
BatteryManager.BATTERY_HEALTH_OVERHEAT = 3
BatteryManager.BATTERY_HEALTH_DEAD = 4
BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE = 5
BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE = 6 //*蓄电池出现未知/未指定的FID故障。
BatteryManager.BATTERY_HEALTH_COLD = 7
对应节点:
/sys/class/power_supply/battery/health
(5)电池瞬时电量:
level: 76
剩余电池容量百分比
对应节点:
/sys/class/power_supply/battery/capacity
(6)电池瞬时电压:
voltage: 4111(毫伏)
瞬时电池电压,以毫伏 (mV) 为单位
对应节点:
/sys/class/power_supply/battery/voltage_now
(7)电池瞬时温度:
temperature: 268
以摄氏度为单位的瞬时电池温度
对应节点:
/sys/class/power_supply/battery/temp
参考源码:
aosp/frameworks/base/services/core/java/com/android/server/BatteryService.java
aosp/frameworks/base/core/java/android/os/BatteryManager.java
aosp/frameworks/base/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java
aosp/system/core/healthd/BatteryMonitor.cpp
aosp/hardware/interfaces/health/1.0/types.hal

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/17546.html