Skip to content

Instantly share code, notes, and snippets.

@ValCanBuild
Created January 9, 2017 21:04
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ValCanBuild/726f46e1a6817caf5ef6d03596bd3f0a to your computer and use it in GitHub Desktop.
Save ValCanBuild/726f46e1a6817caf5ef6d03596bd3f0a to your computer and use it in GitHub Desktop.
A way to get the CPU temperature from an android device by using the sys/class/thermal/temp command
public float getCpuTemp() {
Process p;
try {
p = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone0/temp");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
float temp = Float.parseFloat(line) / 1000.0f;
return temp;
} catch (Exception e) {
e.printStackTrace();
return 0.0f;
}
}
@farble1670
Copy link

On modern devices selinux is going to prevent you from accessing this node. Also, how the thermal zones map to actual hardware components depends on the CPU. On one device this might be the CPU diode but on another it could be the battery (or invalid).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment