bricklore bricklore - 1 year ago 83
Java Question

native app get batterylevel

I want to read the current battery level in a native app without using java.
The equivalent in Java would be:

BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
int scale = -1;
int level = -1;
int voltage = -1;
int temp = -1;
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);

How is this possible by only using native code?

Answer Source

See this topic about dumpsys utility maybe run programmatically and read IO output buffer. I found few C++ sources you to look for. Running dumpsys may need root privileges.

Android ADB shell dumpsys tool

C++ sources, if Im right, reads /sys/class/power_supply virtual directory but what then happens someone may tell you more about the inner details of Linux system. Most of the code seems to be JNI interface stuff, updating private member fields in class. OnLoad class registers jni<->java hooks but thats not what you need to know, just for reference here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download