0

I have a pretty niche question. The question is how do you grab the CPU temperature programmatically from the raspberry pi 4 that is running the LineageOS 20 build of Android 13 and assign it to a var? Below I have included the code that I have been using and not succeeding with:

fun cpuTemperature(): Float {
    val process: Process
    return try {
        process = Runtime.getRuntime().exec("cat sys/class/thermal/thermal_zone0/temp")
        process.waitFor()
        val reader = BufferedReader(InputStreamReader(process.inputStream))
        val line: String = reader.readLine()
        if (line != null) {
            val temp = line.toFloat()
            temp / 1000.0f
        } else {
            51.0f
        }
    } catch (e: Exception) {
        e.printStackTrace()
        0.0f
    }
}

I understand this a bit of a weird question and most people probably wont have experience doing something like this so I appreciate any help or insight into how garbing CPU temp on android apps works.

Alex
  • 238
  • 4
  • 14
  • 1
    Try a `/` before `sys`: `/sys/class/thermal/thermal_zone0/temp`. I don't know whether you can access `/sys/class/thermal/thermal_zone0/temp` from an ordinary Android app without root, but without the leading `/`, you're doomed. – CommonsWare Feb 04 '23 at 20:38
  • @CommonsWare I gave that a try and it didn't help but I appreciate the suggestion – Alex Feb 05 '23 at 05:57

0 Answers0