How to get CPU temperature in Android
Asked Answered
M

4

13

Sensor Type TYPE_TEMPERATURE has been deprecated [since Android 2.3] probably which was giving info about CPU temperature. Now we have Sensor Type TYPE_AMBIENT_TEMPERATURE which will provide us room temperature (none of my use, & btw not all devices&/Android versions support it)

I checked few apps which measures CPU temperature. Probably they're reading system files. I tried locating, in some devices I'm able to locate it inside following path:

sys/devices/virtual/thermal/thermal_zone0/temp

The structure bit varies from vendor to vendor & also the unit of measurement. fine! But in many devices I'm simply unable to locate any such files & in same devices those apps work! I wonder, how!

How can we measure CPU temperature in Android?

Magnificat answered 17/7, 2014 at 7:55 Comment(2)
I am having more success with this sys/devices/virtual/thermal/thermal_zone0/temp than with Manuel's sys/class/hwmon/hwmonX/temp1_input.Rafaelrafaela
This is the most comprehensive list of paths for CPU temperature to date: https://mcmap.net/q/807990/-how-do-i-get-the-cpu-temperatureMehalick
M
5

I've used the following path:

sys/class/hwmon/hwmonX/temp1_input

that works on the most phones

Mazdaism answered 24/2, 2016 at 14:23 Comment(3)
For Huawei Honor 4C not: No such file or directory.Risible
This didn't work on a Samsung Galaxy Core Prime, but the OP's sys/devices/virtual/thermal/thermal_zone0/temp did. :-)Rafaelrafaela
On most phone really looking inside hwmon folder you will find cpu temperature. But how to know which exactly hwmonX is your CPU?Schafer
D
2

Deprecated does not equal removed! If you find that the device still offers the deprecated sensor type, then go ahead and continue to use it.

Part of the reason for deprecating the old sensor name was that on seeing a temperature sensor, the public expectation was that the temperature would be that of the ambient air. However in many cases the sensor was part of/so close to the CPU that it ended up being mostly a reflection of the CPU temperature.

By adding a new (optional) sensor type that is explicitly the air temperature, this expectation can be better managed:

  • if you want the air temperature, use the new sensor if available
  • if you don't care what you are measuring (but in most practical cases will end up with something heavily influenced by the CPU temperature), use the deprecated sensor if available
Diann answered 17/7, 2014 at 8:54 Comment(1)
agreed with your answer that to get CPU temp I can use deprecated sensor if available. But most of the devices does not support it, & in same devices CPU temp measurement app works which shows CPU temp & not the ambient temp [correct behaviour]; As I've mentioned in my question, I too found the way for it but does not seem completely reliable. i.e. Tracing system files which varies vendor to vendorMagnificat
A
1

Xamarin.Android sample is here - it's as straight forward as looping through a list of predefined file paths (23 in my case) and reading a line from the first file which do not fail upon opening (and then converting the string to a number, scaling it to degrees).

Verified it on the following devices:

  • Samsung Galaxy Note 10 (Snapdragon) API 29
  • Samsung Galaxy A5 2017 API level 24
  • Samsung Galaxy A9 2018, API level 26
  • Essential Phone API 26
  • Xiaomi MI 6 API 28
  • Xiaomi MI 8 Pro, API Level 28
  • Huawei Mate 9, API Level 24
  • Huawei Mate RS, API Level 28
  • Nokia N1  (Intel Atom) API 21
  • Huawei Honor Play, API Level 27
  • Galaxy Note 3 Duos, API Level 19
  • Xperia XZ2, API Level 28
  • Motorola One XT1941-4, API Level 28
  • Lenovo S5, API 26

Not working:

  • Xiaomi Mi 8SE API 27 (found one of CPU core's temp at /sys/devices/virtual/thermal/thermal_zone9/temp)
  • Xiaomi Mi 5s API 26
  • Galaxy S devices with Exynos CPUs
    • Samsung Galaxy S10 SM-G973F (Exynos), API Level 28
    • Samsung Galaxy S9+ SM-G965F (Exynos) API 28
    • Samsung Galaxy S8+ SM-G955N (Exynos) API 26
  • Nokia 1 (Android GO), API Level 27

Context

  1. There's no CPU temperature available through standard Sensor API
  2. HardwarePropertiesManager API is only viable for Android Enterprise, not a typical use-case
  3. The only way to get CPU temperature is having a comprehensive list of "magic" paths with system files reporting temperature readings

Programs such as CPU-Z have internal list of paths which can be probed for files that contain the name of the sensor/reading (typically named "type" or "name") and actual value (typically named "temp"). E.g. under /sys/devices/virtual/thermal/thermal_zone0 one might read "cpu-0-0-user" value from type file and "32100" value from temp file.

There're multiple files with different sensor names at a single device. One might guess by the name of the sensor if it related to CPU or not (containing CPU or SoC or smth else in the name). Precisely speaking there's no such thing as single "CPU temperature" value, there are many. If you don't care about the specifics (e.g. telling the difference between core or package temperature) picking one value should be representative of CPU thermal situation.

Also sensor names (as well as file paths) would be different for various device vendors. Sensor values can be in thousands as integers or not. The variety of the ways temp reading can be implemented prompts why there's no standard API in Android for CPU temp.

The list I gathered is not full (and seems it is smaller than the one inside CPU-Z) though I did spend quite some time googling.

How do I find the right paths for a specific device

  • Get the device
  • Install Termux and do cd, ls and cat going through various paths (starting with the list in the sample above) investigating what's inside and searching for the sensor of interest
Actual answered 7/5, 2020 at 15:49 Comment(2)
Is there a way to make it work on Samsung S-8-9-10+ devices?Dominion
Assuming you mean the Exynis variants (sincr the samplebabive is OK with Snapdragon)... Get the device Install Termux and do cd, ls and cat going through various paths (starting with the list in the sample above) investigating what's inside and searching for the sensor of interestActual
H
1

If your app is a system app integrated in the device OS, you could just use HardwarePropertiesManager.

If not, here's a list of sysfs paths that you could read to get the temps:

/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp
/sys/devices/system/cpu/cpu0/cpufreq/FakeShmoo_cpu_temp
/sys/class/thermal/thermal_zone0/temp
/sys/class/i2c-adapter/i2c-4/4-004c/temperature
/sys/devices/platform/tegra-i2c.3/i2c-4/4-004c/temperature
/sys/devices/platform/omap/omap_temp_sensor.0/temperature
/sys/devices/platform/tegra_tmon/temp1_input
/sys/kernel/debug/tegra_thermal/temp_tj
/sys/devices/platform/s5p-tmu/temperature
/sys/class/thermal/thermal_zone1/temp
/sys/class/hwmon/hwmon0/device/temp1_input
/sys/devices/virtual/thermal/thermal_zone1/temp
/sys/devices/virtual/thermal/thermal_zone0/temp
/sys/class/thermal/thermal_zone3/temp
/sys/class/thermal/thermal_zone4/temp
/sys/class/hwmon/hwmonX/temp1_input
/sys/devices/platform/s5p-tmu/curr_temp

You can learn more about how to read system files here.

Hypogeous answered 21/10, 2020 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.