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
- There's no CPU temperature available through standard Sensor API
- HardwarePropertiesManager API is only viable for Android Enterprise, not a typical use-case
- 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
sys/devices/virtual/thermal/thermal_zone0/temp
than with Manuel'ssys/class/hwmon/hwmonX/temp1_input
. – Rafaelrafaela