Skip to content

Commit 97aafdf

Browse files
Bill GruberAndroid (Google) Code Review
authored andcommitted
Merge "Dev guide for sensors" into ics-mr0
2 parents 48e0617 + f8c029e commit 97aafdf

File tree

9 files changed

+1869
-67
lines changed

9 files changed

+1869
-67
lines changed

docs/html/guide/guide_toc.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,25 @@ localized titles are added in the language order specified below.
309309
<a href="<?cs var:toroot ?>guide/topics/clipboard/copy-paste.html">
310310
<span class="en">Copy and Paste</span>
311311
</a></li>
312-
<!--<li class="toggle-list">
313-
<div><a style="color:gray;">Sensors</a></div>
314-
<ul>
315-
<li><a style="color:gray;">Camera</a></li>
316-
<li><a style="color:gray;">Compass</a></li>
317-
<li><a style="color:gray;">Accelerometer</a></li>
318-
</ul>
319-
</li> -->
312+
<li class="toggle-list">
313+
<div><a href="<?cs var:toroot ?>guide/topics/sensors/index.html">
314+
<span class="en">Sensors</span>
315+
</a><span class="new">new!</span></div>
316+
<ul>
317+
<li><a href="<?cs var:toroot ?>guide/topics/sensors/sensors_overview.html">
318+
<span class="en">Sensors Overview</span>
319+
</a></li>
320+
<li><a href="<?cs var:toroot ?>guide/topics/sensors/sensors_motion.html">
321+
<span class="en">Motion Sensors</span>
322+
</a></li>
323+
<li><a href="<?cs var:toroot ?>guide/topics/sensors/sensors_position.html">
324+
<span class="en">Position Sensors</span>
325+
</a></li>
326+
<li><a href="<?cs var:toroot ?>guide/topics/sensors/sensors_environment.html">
327+
<span class="en">Environment Sensors</span>
328+
</a></li>
329+
</ul>
330+
</li>
320331
<li class="toggle-list">
321332
<div><a href="<?cs var:toroot ?>guide/topics/location/index.html">
322333
<span class="en">Location and Maps</span>

docs/html/guide/topics/sensors/accelerometer.jd

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/html/guide/topics/sensors/camera.jd

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/html/guide/topics/sensors/compass.jd

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/html/guide/topics/sensors/index.jd

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,87 @@ page.title=Sensors
22
@jd:body
33

44
<div id="qv-wrapper">
5-
<div id="qv">
5+
<div id="qv">
6+
<h2>Topics</h2>
7+
<ol>
8+
<li><a href="{@docRoot}guide/topics/sensors/sensors_overview.html">Sensors Overview</a></li>
9+
<li><a href="{@docRoot}guide/topics/sensors/sensors_motion.html">Motion Sensors</a></li>
10+
<li><a href="{@docRoot}guide/topics/sensors/sensors_position.html">Position
11+
Sensors</a></li>
12+
<li><a href="{@docRoot}guide/topics/sensors/sensors_environment.html">Environment
13+
Sensors</a></li>
14+
</ol>
15+
<h2>Key classes and interfaces</h2>
16+
<ol>
17+
<li>{@link android.hardware.Sensor}</li>
18+
<li>{@link android.hardware.SensorEvent}</li>
19+
<li>{@link android.hardware.SensorManager}</li>
20+
<li>{@link android.hardware.SensorEventListener}</li>
21+
</ol>
22+
<h2>Related samples</h2>
23+
<ol>
24+
<li><a href="{@docRoot}resources/samples/AccelerometerPlay/index.html">Accelerometer
25+
Play</a></li>
26+
<li><a
27+
href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/os/RotationVectorDemo.html">
28+
API Demos (OS - RotationVectorDemo)</a></li>
29+
<li><a
30+
href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/os/Sensors.html">API Demos
31+
(OS - Sensors)</a></li>
32+
</ol>
33+
</div>
34+
</div>
635

36+
<p>Most Android-powered devices have built-in sensors that measure motion, orientation,
37+
and various environmental conditions. These sensors are capable of providing raw data with high
38+
precision and accuracy, and are useful if you want to monitor three-dimensional device movement or
39+
positioning, or you want to monitor changes in the ambient environment near a device. For example, a
40+
game might track readings from a device's gravity sensor to infer complex user gestures
41+
and motions, such as tilt, shake, rotation, or swing. Likewise, a weather application might use a
42+
device's temperature sensor and humidity sensor to calculate and report the dewpoint, or a travel
43+
application might use the geomagnetic field sensor and accelerometer to report a compass
44+
bearing.</p>
745

46+
<p>The Android platform supports four broad categories of sensors:</p>
847

9-
<h2>Accelerometer</h2>
10-
<p>The accelerometer sensors allow you to detect the various movements of the device.</p>
48+
<ul>
49+
<li>Motion sensors
50+
<p>These sensors measure acceleration forces and rotational forces along three axes. This
51+
category includes accelerometers, gravity sensors, gyroscopes, and rotational vector
52+
sensors.</p>
53+
</li>
54+
<li>Environmental sensors
55+
<p>These sensors measure various environmental parameters, such as ambient air temperature
56+
and pressure, illumination, and humidity. This category includes barometers, photometers, and
57+
thermometers.</p>
58+
</li>
59+
<li>Position sensors
60+
<p>These sensors measure the physical position of a device. This category includes
61+
orientation sensors and magnetometers.</p>
62+
</li>
63+
</ul>
1164

12-
<h2>Compass</h2>
13-
<p>The compass provides data on the devices current polar orientation.</p>
65+
<p>To access these sensors, you can use the Android sensor framework. The sensor framework provides
66+
several classes and interfaces that help you perform a wide variety of sensor-related tasks. To
67+
learn more about the framework and the sensors that are supported on the Android system, read the
68+
following documents:</p>
69+
70+
<dl>
71+
<dt><strong><a href="{@docRoot}guide/topics/sensors/sensors_overview.html">Sensors
72+
Overview</a></strong></dt>
73+
<dd>Learn how to list the sensors that are on a device, set up sensor event listeners, and
74+
acquire sensor data. Also learn best practices for accessing and using sensors.</dd>
75+
<dt><strong><a href="{@docRoot}guide/topics/sensors/sensors_motion.html">Motion
76+
Sensors</a></strong></dt>
77+
<dd>Learn how to use the sensors that provide acceleration data, such as the accelerometer,
78+
gravity sensor, and linear acceleration sensor. Also learn how to use the sensors that
79+
provide rotational data, such as gyroscopes and rotational vector sensors.</dd>
80+
<dt><strong><a href="{@docRoot}guide/topics/sensors/sensors_position.html">Position
81+
Sensors</a></strong></dt>
82+
<dd>Learn how to use the sensors that provide orientation and compass data, such as the
83+
orientation sensor and the geomagnetic field sensor.</dd>
84+
<dt><strong><a href="{@docRoot}guide/topics/sensors/environment.html">Environment
85+
Sensors</a></strong></dt>
86+
<dd>Learn how to use the sensors that provide environmental data, such as the light,
87+
humidity, pressure, temperature, and proximity sensors.</dd>
88+
</dl>
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
page.title=Environment Sensors
2+
parent.title=Sensors
3+
parent.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
11+
Sensors</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
19+
href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/os/RotationVectorDemo.html">
20+
API Demos (OS - RotationVectorDemo)</a></li>
21+
<li><a
22+
href="{@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.
37+
You can use these sensors to monitor relative ambient humidity, illuminance, ambient pressure, and
38+
ambient temperature near an Android-powered device. All four environment sensors are hardware-based
39+
and are available only if a device manufacturer has built them into a device. With the exception of
40+
the light sensor, which most device manufacturers use to control screen brightness, environment
41+
sensors are not always available on devices. Because of this, it's particularly important that you
42+
verify at runtime whether an environment sensor exists before you attempt to acquire data from
43+
it.</p>
44+
45+
<p>Unlike most motion sensors and position sensors, which return a multi-dimensional array of sensor
46+
values for each {@link android.hardware.SensorEvent}, environment sensors return a single sensor
47+
value for each data event. For example, the temperature in &deg;C or the pressure in hPa.
48+
Also, unlike motion sensors and position sensors, which often require high-pass or low-pass
49+
filtering, environment sensors do not typically require any data filtering or data processing. Table
50+
1 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
94+
device. 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
99+
calibration, filtering, or modification, which makes them some of the easiest sensors to use. To
100+
acquire data from these sensors you first create an instance of the {@link
101+
android.hardware.SensorManager} class, which you can use to get an instance of a physical sensor.
102+
Then you register a sensor listener in the {@link android.app.Activity#onResume
103+
onResume()} method, and start handling incoming sensor data in the {@link
104+
android.hardware.SensorEventListener#onSensorChanged onSensorChanged()} callback method. The
105+
following code shows you how to do this:</p>
106+
107+
<pre>
108+
public 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
151+
android.hardware.SensorEventListener#onAccuracyChanged onAccuracyChanged()} and {@link
152+
android.hardware.SensorEventListener#onSensorChanged onSensorChanged()} callback methods. Also, be
153+
sure that you always unregister a sensor when an activity pauses. This prevents a sensor from
154+
continually 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
159+
the 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
161+
android.hardware.Sensor#TYPE_AMBIENT_TEMPERATURE}) you can use these two data streams to calculate
162+
the 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
168+
can calculate the dew point:</p>
169+
170+
<pre class="no-pretty-print classic">
171+
ln(RH/100%) + m·t/(T<sub>n</sub>+t)
172+
t<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)
194+
d<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+

0 commit comments

Comments
 (0)