0

I'm using the following code to get the voltage of a battery at two different times (t1,t2). t1 is before the execution of a task and t2 is after the execution of a task. So by rule, t2 must be smaller than t1.

However, in execution it is not true. I am getting multiple values, that are greater, smaller and equal to t1. How could this be possible? Even in android battery monitoring tools, I have noticed that sometimes the total battery mAh value increases few points without plugging the charger.

public void onCreate() {
    BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
        int scale = -1;
        int level = -1;
        int voltage = -1;
        int temp = -1;
        @Override
        public void onReceive(Context context, Intent intent) {
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
            voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
            Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
        }
    };
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryReceiver, filter);
}
4

2 回答 2

1

Android 框架不断从其 power_supply 子系统获取信息。所以在更新之后,它会发出一个 BATTERY_STATE_CHANGED 意图。

private void updateLocked() {
    if (!mUpdatesStopped) {
        // Update the values of mAcOnline, et. all.
        native_update();

        // Process the new values. Sendout the intent
        processValuesLocked();
    }
}

但它只是在一段时间后更新信息。所以它可能包含一定程度的不准确。

在执行na​​tive_update。Android只是简单的读取下一些文件的内容/sys/class/power_supply/battery

setBooleanField(env, obj, gPaths.acOnlinePath, gFieldIds.mAcOnline);
setBooleanField(env, obj, gPaths.usbOnlinePath, gFieldIds.mUsbOnline);
setBooleanField(env, obj, gPaths.wirelessOnlinePath, gFieldIds.mWirelessOnline);
setBooleanField(env, obj, gPaths.batteryPresentPath, gFieldIds.mBatteryPresent);

setIntField(env, obj, gPaths.batteryCapacityPath, gFieldIds.mBatteryLevel);
setVoltageField(env, obj, gPaths.batteryVoltagePath, gFieldIds.mBatteryVoltage);
setIntField(env, obj, gPaths.batteryTemperaturePath, gFieldIds.mBatteryTemperature);

因此,如果您想要电压信息,您可以简单地:

$cat /sys/class/power_supply/battery/voltage_now 
$cat /sys/class/power_supply/battery/batt_vol

请注意,电压现在的单位是微伏,而不是毫伏。

如果需要,您也可以通过编程方式阅读它。

但是,它可能仍然不够准确,因为这些数据的内容是由操作系统更新的(可能类似于电源驱动器),因此要获得真正准确的统计数据,您可能必须尝试硬件方法。:)

于 2013-03-10T00:36:16.440 回答
0

How could this be possible?
电池 mAh 值是作为电池电压测量的。但是这个电压会不时变化。当有高电流消耗时它会下降一点,然后在处理器空闲时恢复。它也会随着温度的变化而变化。所以总体来说是非常不准确的。

于 2013-03-10T00:39:23.360 回答