Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to measure ambient temperature in android

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

like image 230
Furkan Avatar asked Aug 16 '12 12:08

Furkan


People also ask

Can I measure ambient temperature with my phone?

Room thermometer is app which precisely measure temperature in your room. It uses ambient temperature sensor which are built-in to Android phones. Our tool just reads the measurements from ambient temperature sensors and shows it in Celsius and Fahrenheit degrees.

How can I measure my room temperature in Android?

Using a Smartphone. Download a thermometer application to your smartphone. Many smartphones are equipped with sensors that they use to monitor the temperature of the device. You can download an app that uses these sensors to take an ambient temperature reading of the room.

Can Android phones detect room temperature?

There are very few smartphones with thermometers. On conventional Android and iOS devices, however, it is almost impossible to find. While it is possible to use your smartphone to find out the temperature in a room, it is not through a thermometer function.

How do you measure ambient temperature?

Ambient temperature is measured with a thermometer while room temperature is based more on feeling. The ambient temperature of an environment may vary greatly from its accepted room temperature, such as when an air conditioner or heater malfunctions.


Video Answer


2 Answers

this is a basic example of how to get the Ambient Temperature in Android:

import android.support.v7.app.AppCompatActivity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private TextView temperaturelabel;
    private SensorManager mSensorManager;
    private Sensor mTemperature;
    private final static String NOT_SUPPORTED_MESSAGE = "Sorry, sensor not available for this device.";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        temperaturelabel = (TextView) findViewById(R.id.myTemp);
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){
            mTemperature= mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); // requires API level 14.
        }
        if (mTemperature == null) {             
            temperaturelabel.setText(NOT_SUPPORTED_MESSAGE);
        }       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mTemperature, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {                 
        super.onPause();
        mSensorManager.unregisterListener(this);
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        float ambient_temperature = event.values[0];
        temperaturelabel.setText("Ambient Temperature:\n " + String.valueOf(ambient_temperature) + getResources().getString(R.string.celsius));     
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here if sensor accuracy changes.
    }
}

you can download the complete example from : https://github.com/Jorgesys/Android_Ambient_Temperature

like image 158
Jorgesys Avatar answered Sep 26 '22 09:09

Jorgesys


This cannot be done. The temperature sensor even if exists if for the battery temperature and cpu temperature.

Edit: As swayam pointed out there is Ambient Temperature sensor added in API 14 but gingerbread compatibility document explicitly says not to include temperature measurement

Device implementations MAY but SHOULD NOT include a thermometer (i.e. temperature sensor.) If a device implementation does include a thermometer, it MUST measure the temperature of the device CPU. It MUST NOT measure any other temperature. (Note that this sensor type is deprecated in the Android 2.3 APIs.)

But most phones only include cpu temperature measurement sensor Sensor.TYPE_TEMPERATURE which is deprecated. So this does not give accurate temperature. You should use Sensor.TYPE_AMBIENT_TEMPERATURE which I dont think many phones have.

like image 35
nandeesh Avatar answered Sep 22 '22 09:09

nandeesh