Is there any Android api to find/sense room temperature programmatically in android code?

前端 未结 9 1203
天命终不由人
天命终不由人 2021-01-04 04:47

I am developing an android app for my project, I need to find room temperature as part of it. I am using Droid 2 A955 model for testing.

My Questions are:

相关标签:
9条回答
  • 2021-01-04 05:41

    Take a look at the Sensor class in the documentation.

    You need to do something along the lines of this:

    public class SensorActivity extends Activity, implements SensorEventListener {
     private final SensorManager mSensorManager;
     private final Sensor mTemp;
    
     public SensorActivity() {
         mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
         mtemp = mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE);
     }
    
     protected void onResume() {
         super.onResume();
         mSensorManager.registerListener(this, mTemp, SensorManager.SENSOR_DELAY_NORMAL);
     }
    
     protected void onPause() {
         super.onPause();
         mSensorManager.unregisterListener(this);
     }
    
     public void onAccuracyChanged(Sensor sensor, int accuracy) {
     }
    
     public void onSensorChanged(SensorEvent event) {
     }
    }
    

    This should give you access to the temperature sensors in it's own activity.

    Play with this and see what you can find. The documentation has great examples for other types of sensors, the temp sensor should be even simpler than most of the provided ones.

    Hope this helps!

    0 讨论(0)
  • 2021-01-04 05:46

    There is the age old method of letting the phone and a "good" thermometer stabilize at room temperature (a few hours) and read both.

    Then put it outside in a nice wintry garage at about 35F let it stabilize. Then pray for linearity.

    Empirical is always nice. I am very interested in measuring a constant temperature in an empty house, and watch for the temperature dropping (or rising!!)

    Bradshaw at Buzzards Bay

    0 讨论(0)
  • 2021-01-04 05:48

    this sounds interesting:

    http://developer.android.com/reference/android/hardware/Sensor.html#TYPE_AMBIENT_TEMPERATURE

    from API >= 13

    0 讨论(0)
提交回复
热议问题