how to measure ambient temperature in android

Viewed 32968

I want to measure ambient temperature on android device. but my device doesn t include thermometer sensor. How can I measure it? Thanks.

4 Answers

Android 5.0

Using CPU & Battery to gain Ambient Temperature

  • ***Get Average Running Heat First

    1. Find real temperature & remove Cold Device CPU Temperature

      • Eg. 40°C - 29°C = 11°C Running Temperature
    2. Get Battery Temperature & Compare with CPU Temperature

      • battery > cpu = Charging/Holding/No Usage
      • battery < cpu = Intensive CPU Task / Usage
    3. Calculate Lowest Reading Over 30 Minutes

    4. Calculate Comparison Heat Difference

      • Using = averageTemp - 3°C
      • No Usage = averageTemp

`

// EXAMPLE ONLY - RESET HIGHEST AT 500°C
    public Double closestTemperature = 500.0;
    public Long resetTime = 0L;
    public String getAmbientTemperature(int averageRunningTemp, int period)
    {
    // CHECK MINUTES PASSED ( STOP CONSTANT LOW VALUE )  
        Boolean passed30 = ((System.currentTimeMillis() - resetTime) > ((1000 * 60)*period));
        if (passed30)
        {
            resetTime = System.currentTimeMillis();
            closestTemperature = 500.0;
        }

    // FORMAT DECIMALS TO 00.0°C
        DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
        dfs.setDecimalSeparator('.');
        DecimalFormat decimalFormat = new DecimalFormat("##.0", dfs);

    // READ CPU & BATTERY
        try
        {
       // BYPASS ANDROID RESTRICTIONS ON THERMAL ZONE FILES & READ CPU THERMAL

            RandomAccessFile restrictedFile = new RandomAccessFile("sys/class/thermal/thermal_zone1/temp", "r");
            Double cpuTemp = (Double.parseDouble(restrictedFile.readLine()) / 1000);

        // RUN BATTERY INTENT
            Intent batIntent = this.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));    
       // GET DATA FROM INTENT WITH BATTERY
            Double batTemp = (double) batIntent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10;

       // CHECK IF DATA EXISTS
            if (cpuTemp != null)
            {
           // CPU FILE OK - CHECK LOWEST TEMP
                if (cpuTemp - averageRunningTemp < closestTemperature)
                    closestTemperature = cpuTemp - averageRunningTemp;
            }
            else if (batTemp != null)
            {
             // BAT OK - CHECK LOWEST TEMP
                if (batTemp - averageRunningTemp < closestTemperature)
                    closestTemperature = batTemp - averageRunningTemp;
            }
            else
            {
            // NO CPU OR BATTERY TEMP FOUND - RETURN 0°C
                closestTemperature = 0.0;
            }
        }
        catch (Exception e)
        {
            // NO CPU OR BATTERY TEMP FOUND - RETURN 0°C
            closestTemperature = 0.0;
        }
     // FORMAT & RETURN
        return decimalFormat.format(closestTemperature);
    }

I already know that bypassing restricted folders like this is not recommended, however the CPU temperature is always returned as a solid Degree, i need the full decimal places - So I've used this rather hacky approach to bypass restrictions using RandomAccessFile

Please do not comment saying - It's not possible, i know it isn't fully possible to detect the surrounding temperature using this technique, however after many days - I've managed to get it to 2°C Accuracy

My Average Heat was 11°C higher than the real temperature

Making my Usage,

getAmbientTemperature(11,30);

11 being Average Temperature

30 being Next Reset Time ( Period of minimum check )

NOTES, • Checking Screen UpTime can help to calculate heat changes over time
• Checking Accelerometer can help to suggest the user Places the phone down to Cool the CPU & Battery for an increased accuracy.
• Android 5.0+ Support needs to be added for Permissions

2°C Accuracy on my device

Related