1page.title=Environment Sensors
2parent.title=Sensors
3parent.link=index.html
4@jd:body
5
6<div id="qv-wrapper">
7  <div id="qv">
8  <h2>In this document</h2>
9    <ol>
10      <li><a href="#sensors-using-temp">Using the Light, Pressure, and Temperature
11Sensors</a></li>
12      <li><a href="#sensors-using-humid">Using the Humidity Sensor</a></li>
13    </ol>
14  <h2>Related samples</h2>
15  <ol>
16    <li><a href="{@docRoot}resources/samples/AccelerometerPlay/index.html">Accelerometer
17        Play</a></li>
18    <li><a
19href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/os/RotationVectorDemo.html">
20API Demos (OS - RotationVectorDemo)</a></li>
21    <li><a
22href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/os/Sensors.html">API Demos
23(OS - Sensors)</a></li>
24  </ol>
25  <h2>See also</h2>
26  <ol>
27    <li><a href="{@docRoot}guide/topics/sensors/index.html">Sensors</a></li>
28    <li><a href="{@docRoot}guide/topics/sensors/sensors_overview.html">Sensors Overview</a></li>
29    <li><a href="{@docRoot}guide/topics/sensors/sensors_position.html">Position Sensors</a></li>
30    <li><a href="{@docRoot}guide/topics/sensors/sensors_motion.html">Motion
31        Sensors</a></li>
32  </ol>
33  </div>
34</div>
35
36<p>The Android platform provides four sensors that let you monitor various environmental properties.
37You can use these sensors to monitor relative ambient humidity, illuminance, ambient pressure, and
38ambient temperature near an Android-powered device. All four environment sensors are hardware-based
39and are available only if a device manufacturer has built them into a device. With the exception of
40the light sensor, which most device manufacturers use to control screen brightness, environment
41sensors are not always available on devices. Because of this, it's particularly important that you
42verify at runtime whether an environment sensor exists before you attempt to acquire data from
43it.</p>
44
45<p>Unlike most motion sensors and position sensors, which return a multi-dimensional array of sensor
46values for each {@link android.hardware.SensorEvent}, environment sensors return a single sensor
47value for each data event. For example, the temperature in &deg;C or the pressure in hPa.
48Also, unlike motion sensors and position sensors, which often require high-pass or low-pass
49filtering, environment sensors do not typically require any data filtering or data processing. Table
501 provides a summary of the environment sensors that are supported on the Android platform.</p>
51
52<p class="table-caption" id="table1">
53  <strong>Table 1.</strong> Environment sensors that are supported on the Android platform.</p>
54<table>
55  <tr>
56    <th scope="col" style="white-space:nowrap">Sensor</th>
57    <th scope="col" style="white-space:nowrap">Sensor event data</th>
58    <th scope="col" style="white-space:nowrap">Units of measure</th>
59    <th scope="col" style="white-space:nowrap">Data description</th>
60  </tr>
61  <tr>
62    <td>{@link android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE}</td>
63    <td><code>event.values[0]</code></td>
64    <td>&deg;C</td>
65    <td>Ambient air temperature.</td>
66  </tr>
67  <tr>
68    <td>{@link android.hardware.Sensor#TYPE_LIGHT}</td>
69    <td><code>event.values[0]</code></td>
70    <td>lx</td>
71    <td>Illuminance.</td>
72  </tr>
73  <tr>
74    <td>{@link android.hardware.Sensor#TYPE_PRESSURE}</td>
75    <td><code>event.values[0]</code></td>
76    <td>hPa or mbar</td>
77    <td>Ambient air pressure.</td>
78  </tr>
79  <tr>
80    <td>{@link android.hardware.Sensor#TYPE_RELATIVE_HUMIDITY}</td>
81    <td><code>event.values[0]</code></td>
82    <td>%</td>
83    <td>Ambient relative humidity.</td>
84  </tr>
85  <tr>
86    <td>{@link android.hardware.Sensor#TYPE_TEMPERATURE}</td>
87    <td><code>event.values[0]</code></td>
88    <td>&deg;C</td>
89    <td>Device temperature.<sup>1</sup></td>
90  </tr>
91</table>
92
93<p class="note"><sup><strong>1</strong></sup>  Implementations vary from device to
94device. This sensor was deprecated in Android 4.0 (API Level 14).</p>
95
96<h2 id="sensors-using-temp">Using the Light, Pressure, and Temperature Sensors</h2>
97
98<p>The raw data you acquire from the light, pressure, and temperature sensors usually requires no
99calibration, filtering, or modification, which makes them some of the easiest sensors to use. To
100acquire data from these sensors you first create an instance of the {@link
101android.hardware.SensorManager} class, which you can use to get an instance of a physical sensor.
102Then you register a sensor listener in the {@link android.app.Activity#onResume
103onResume()} method, and start handling incoming sensor data in the {@link
104android.hardware.SensorEventListener#onSensorChanged onSensorChanged()} callback method. The
105following code shows you how to do this:</p>
106
107<pre>
108public class SensorActivity extends Activity implements SensorEventListener {
109  private SensorManager mSensorManager;
110  private Sensor mPressure;
111
112  &#64;Override
113  public final void onCreate(Bundle savedInstanceState) {
114    super.onCreate(savedInstanceState);
115    setContentView(R.layout.main);
116
117    // Get an instance of the sensor service, and use that to get an instance of
118    // a particular sensor.
119    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
120    mPressure = mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
121  }
122
123  &#64;Override
124  public final void onAccuracyChanged(Sensor sensor, int accuracy) {
125    // Do something here if sensor accuracy changes.
126  }
127
128  &#64;Override
129  public final void onSensorChanged(SensorEvent event) {
130    float millibars_of_pressure = event.values[0];
131    // Do something with this sensor data.
132  }
133
134  &#64;Override
135  protected void onResume() {
136    // Register a listener for the sensor.
137    super.onResume();
138    mSensorManager.registerListener(this, mPressure, SensorManager.SENSOR_DELAY_NORMAL);
139  }
140
141  &#64;Override
142  protected void onPause() {
143    // Be sure to unregister the sensor when the activity pauses.
144    super.onPause();
145    mSensorManager.unregisterListener(this);
146  }
147}
148</pre>
149
150<p>You must always include implementations of both the {@link
151android.hardware.SensorEventListener#onAccuracyChanged onAccuracyChanged()} and {@link
152android.hardware.SensorEventListener#onSensorChanged onSensorChanged()} callback methods. Also, be
153sure that you always unregister a sensor when an activity pauses. This prevents a sensor from
154continually sensing data and draining the battery.</p>
155
156<h2 id="sensors-using-humid">Using the Humidity Sensor</h2>
157
158<p>You can acquire raw relative humidity data by using the humidity sensor the same way that you use
159the light, pressure, and temperature sensors. However, if a device has both a humidity sensor
160({@link android.hardware.Sensor#TYPE_RELATIVE_HUMIDITY}) and a temperature sensor ({@link
161android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE}) you can use these two data streams to calculate
162the dew point and the absolute humidity.</p>
163
164<h4>Dew point</h4>
165
166<p>The dew point is the temperature at which a given volume of air must be cooled, at constant
167  barometric pressure, for water vapor to condense into water. The following equation shows how you
168can calculate the dew point:</p>
169
170<pre class="no-pretty-print classic">
171                       ln(RH/100%) + m·t/(T<sub>n</sub>+t)
172t<sub>d</sub>(t,RH) = T<sub>n</sub> · ------------------------------------
173                    m - [ln(RH/100%) + m·t/(T<sub>n</sub>+t)]
174</pre>
175
176<p>Where,</p>
177
178<ul type="none">
179  <li>t<sub>d</sub> = dew point temperature in degrees C</li>
180  <li>t = actual temperature in degrees C</li>
181  <li>RH = actual relative humidity in percent (%)</li>
182  <li>m = 17.62</li>
183  <li>T<sub>n</sub> = 243.12</li>
184</ul>
185
186<h4>Absolute humidity</h4>
187
188<p>The absolute humidity is the mass of water vapor in a given volume of dry air. Absolute
189  humidity is measured in grams/meter<sup>3</sup>. The following equation shows how you
190  can calculate the absolute humidity:</p>
191
192<pre class="no-pretty-print classic">
193                          (RH/100%) · A · exp(m·t/(T<sub>n</sub>+t)
194d<sub>v</sub>(t,RH) = 216.7 · ------------------------------------
195                                    273.15 + t
196</pre>
197
198<p>Where,</p>
199
200<ul type="none">
201  <li>d<sub>v</sub> = absolute humidity in grams/meter<sup>3</sup></li>
202  <li>t = actual temperature in degrees C</li>
203  <li>RH = actual relative humidity in percent (%)</li>
204  <li>m = 17.62</li>
205  <li>T<sub>n</sub> = 243.12 degrees C</li>
206  <li>A = 6.112 hPa</li>
207</ul>
208
209