Reputation: 3
'''
Process process;
try {
process = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone0/temp");
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
System.out.println("Multan "+ line);
if(line!=null) {
float temp = Float.parseFloat(line);
return temp / 1000.0f;
}else{
return 51.0f;
}
} catch (Exception e) {
e.printStackTrace();
return 0.0f;
}
'''
I found this solution but it always return 51.0 as line is always null. Someone please help with this. Thanks in advance.
Upvotes: 0
Views: 910
Reputation: 1535
Firstly it's wrong to read a file like this, you don't need to create a new process each time that you want to get the temperature.
Secondly, you may not have the file your are looking for. This is because the /sys/* is a pseudo-filesystem (sysfs) created by the system and it's different drivers. It may be very different from a phone to another.
I found a non-exhaustive list of cpu temperature file.
String[] sensorFiles = new String[] {
"/sys/devices/system/cpu/cpu0/cpufreq/cpu_temp",
"/sys/devices/system/cpu/cpu0/cpufreq/FakeShmoo_cpu_temp",
"/sys/class/thermal/thermal_zone1/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_zone0/temp",
"/sys/devices/virtual/thermal/thermal_zone0/temp",
"/sys/class/hwmon/hwmon0/device/temp1_input",
"/sys/devices/virtual/thermal/thermal_zone1/temp",
"/sys/devices/platform/s5p-tmu/curr_temp"
}
You have to try each of them until you get a positive answer.
File correctSensorFile = null;
for (String file : sensorFiles) {
File f = new File(file);
if (f.exists()) {
correctSensorFile = f;
break;
}
}
if (correctSensorFile == null)
throw new RuntimeException("Did not find sensor file to read");
RandomAccessFile reader = new RandomAccessFile(correctSensorFile , "r");
String value = reader.readLine();
reader.close();
Upvotes: 2