blob: 869109b62363929b95e51e77899cf5d50a99fc21 [file] [log] [blame]
Bill Gruberf8c029e2011-11-01 09:31:57 -07001page.title=Position 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-pos-orient">Using the Orientation Sensor</a></li>
11 <li><a href="#sensors-pos-mag">Using the Geomagnetic Field Sensor</a></li>
12 <li><a href="#sensors-pos-prox">Using the Proximity Sensor</a></li>
13 </ol>
14 <h2>Key classes and interfaces</h2>
15 <ol>
16 <li>{@link android.hardware.Sensor}</li>
17 <li>{@link android.hardware.SensorEvent}</li>
18 <li>{@link android.hardware.SensorManager}</li>
19 <li>{@link android.hardware.SensorEventListener}</li>
20 </ol>
21 <h2>Related samples</h2>
22 <ol>
23 <li><a href="{@docRoot}resources/samples/AccelerometerPlay/index.html">Accelerometer
24 Play</a></li>
25 <li><a
26href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/os/RotationVectorDemo.html">
27API Demos (OS - RotationVectorDemo)</a></li>
28 <li><a
29href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/os/Sensors.html">API Demos
30(OS - Sensors)</a></li>
31 </ol>
32 <h2>See also</h2>
33 <ol>
34 <li><a href="{@docRoot}guide/topics/sensors/index.html">Sensors</a></li>
35 <li><a href="{@docRoot}guide/topics/sensors/sensors_overview.html">Sensors Overview</a></li>
36 <li><a href="{@docRoot}guide/topics/sensors/sensors_motion.html">Motion
37 Sensors</a></li>
38 <li><a href="{@docRoot}guide/topics/sensors/sensors_environment.html">Environment
39 Sensors</a></li>
40 </ol>
41 </div>
42</div>
43
44<p>The Android platform provides two sensors that let you determine the position of a device: the
45geomagnetic field sensor and the orientation sensor. The Android platform also
46provides a sensor that lets you determine how close the face of a device is to an object (known as
47the proximity sensor). The geomagnetic field sensor and the proximity sensor are hardware-based.
48Most
49handset and tablet manufacturers include a geomagnetic field sensor. Likewise, handset manufacturers
50usually include a proximity sensor to determine when a handset is being held close to a user's face
51(for example, during a phone call). The orientation sensor is software-based and derives its data
52from the accelerometer and the geomagnetic field sensor.</p>
53
54<p class="note"><strong>Note:</strong> The orientation sensor was deprecated in Android 2.2 (API
55Level 8).</p>
56
57<p>Position sensors are useful for determining a device's physical position in the
58world's frame of reference. For example, you can use the geomagnetic field sensor in
59combination with the accelerometer to determine a device's position relative to
60the magnetic North Pole. You can also use the orientation sensor (or similar sensor-based
61orientation methods) to determine a device's position in your application's frame of reference.
62Position sensors are not typically used to monitor device movement or motion, such as shake, tilt,
63or thrust (for more information, see <a
64href="{@docRoot}guide/topics/sensors/sensors_motion.html">Motion Sensors</a>).</p>
65
66<p>The geomagnetic field sensor and orientation sensor return multi-dimensional arrays of sensor
67values
68for each {@link android.hardware.SensorEvent}. For example, the orientation sensor provides
69geomagnetic
70field strength values for each of the three coordinate axes during a single sensor event. Likewise,
71the orientation sensor provides azimuth (yaw), pitch, and roll values during a single sensor event.
72For more information about the coordinate systems that are used by sensors, see <a
73href="{@docRoot}guide/topics/sensors/sensors_overview.html#sensors-coords">Sensor Coordinate
74Systems</a>. The proximity sensor provides a single value for each sensor event. Table 1 summarizes
75the position sensors that are supported on the Android platform.</p>
76
77<p class="table-caption" id="table1">
78 <strong>Table 1.</strong> Position sensors that are supported on the Android platform.</p>
79<table>
80 <tr>
81 <th scope="col" style="white-space:nowrap">Sensor</th>
82 <th scope="col" style="white-space:nowrap">Sensor event data</th>
83 <th scope="col" style="white-space:nowrap">Description</th>
84 <th scope="col" style="white-space:nowrap">Units of measure</th>
85 </tr>
86 <tr>
87 <td rowspan="3">{@link android.hardware.Sensor#TYPE_MAGNETIC_FIELD}</td>
88 <td><code>SensorEvent.values[0]</code></td>
89 <td>Geomagnetic field strength along the x axis.</td>
90 <td rowspan="3">&mu;T</td>
91 </tr>
92 <tr>
93 <td><code>SensorEvent.values[1]</code></td>
94 <td>Geomagnetic field strength along the y axis.</td>
95 </tr>
96 <tr>
97 <td><code>SensorEvent.values[2]</code></td>
98 <td>Geomagnetic field strength along the z axis.</td>
99 </tr>
100 <tr>
101 <td rowspan="3">{@link android.hardware.Sensor#TYPE_ORIENTATION}<sup>1</sup></td>
102 <td><code>SensorEvent.values[0]</code></td>
103 <td>Azimuth (angle around the z-axis).</td>
104 <td rowspan="3">Degrees</td>
105 </tr>
106 <tr>
107 <td><code>SensorEvent.values[1]</code></td>
108 <td>Pitch (angle around the x-axis).</td>
109 </tr>
110 <tr>
111 <td><code>SensorEvent.values[2]</code></td>
112 <td>Roll (angle around the y-axis).</td>
113 </tr>
114 <tr>
115 <td>{@link android.hardware.Sensor#TYPE_PROXIMITY}</td>
116 <td><code>SensorEvent.values[0]</code></td>
117 <td>Distance from object.<sup>2</sup></td>
118 <td>cm</td>
119 </tr>
120</table>
121
122<p class="note"><sup><strong>1</strong></sup> This sensor was deprecated in Android 2.2 (API Level
123 8). The sensor framework provides alternate methods for acquiring device orientation, which are
124discussed in <a href="#sensors-pos-orient">Using the Orientation Sensor</a>.</p>
125
126<p class="note"><sup><strong>2</strong></sup> Some proximity sensors provide only binary values
127representing near and far.</p>
128
129<h2 id="sensors-pos-orient">Using the Orientation Sensor</h2>
130
131<p>The orientation sensor lets you monitor the position of a device relative to the earth's frame of
132reference (specifically, magnetic north). The following code shows you how to get an instance of the
133default orientation sensor :</p>
134
135<pre>
136private SensorManager mSensorManager;
137private Sensor mSensor;
138...
139mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
140mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
141</pre>
142
143<p>The orientation sensor derives its data by using a device's geomagnetic field sensor in
144combination with a device's accelerometer. Using these two hardware sensors, an orientation sensor
145provides data for the following three dimensions:</p>
146
147<ul>
148 <li>Azimuth (degrees of rotation around the z axis). This is the angle between magnetic north
149and the device's y axis. For example, if the device's y axis is aligned with magnetic north
150this value is 0, and if the device's y axis is pointing south this value is 180. Likewise, when
151the y axis is pointing east this value is 90 and when it is pointing west this value is 270.</li>
152 <li>Pitch (degrees of rotation around the x axis). This value is positive when the positive z axis
153rotates toward the positive y axis, and it is negative when the positive z axis
154rotates toward the negative y axis. The range of values is 180 degrees to -180
155degrees.</li>
156 <li>Roll (degrees of rotation around the y axis). This value is positive when the positive z axis
157rotates toward the positive x axis, and it is negative when the positive z axis
158rotates toward the negative x axis. The range of values is 90 degrees to -90
159degrees.</li>
160</ul>
161
162<p>This definition is different from yaw, pitch, and roll used in aviation, where the X axis is
163along the long side of the plane (tail to nose). Also, for historical reasons the roll angle is
164positive in the clockwise direction (mathematically speaking, it should be positive in the
165counter-clockwise direction).</p>
166
167<p>The orientation sensor derives its data by processing the raw sensor data from the accelerometer
168and the geomagnetic field sensor. Because of the heavy processing that is involved, the accuracy and
169precision of the orientation sensor is diminished (specifically, this sensor is only reliable when
170the roll component is 0). As a result, the orientation sensor was deprecated in Android 2.2 (API
171level 8). Instead of using raw data from the orientation sensor, we recommend that you use the
172{@link android.hardware.SensorManager#getRotationMatrix getRotationMatrix()} method in conjunction
173with the {@link android.hardware#getOrientation getOrientation()} method to compute orientation
174values. You can also use the {@link android.hardware.SensorManager#remapCoordinateSystem
175remapCoordinateSystem()} method to translate the orientation values to your application's frame of
176reference.</p>
177
178<p>The following code sample shows how to acquire orientation data directly from the orientation
179sensor. We recommend that you do this only if a device has negligible roll.</p>
180
181<pre>
182public class SensorActivity extends Activity implements SensorEventListener {
183
184 private SensorManager mSensorManager;
185 private Sensor mOrientation;
186
187 &#64;Override
188 public void onCreate(Bundle savedInstanceState) {
189 super.onCreate(savedInstanceState);
190 setContentView(R.layout.main);
191
192 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
193 mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
194 }
195
196 &#64;Override
197 public void onAccuracyChanged(Sensor sensor, int accuracy) {
198 // Do something here if sensor accuracy changes.
199 // You must implement this callback in your code.
200 }
201
202 &#64;Override
203 protected void onResume() {
204 super.onResume();
205 mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_NORMAL);
206 }
207
208 &#64;Override
209 protected void onPause() {
210 super.onPause();
211 mSensorManager.unregisterListener(this);
212 }
213
214 &#64;Override
215 public void onSensorChanged(SensorEvent event) {
216 float azimuth_angle = event.values[0];
217 float pitch_angle = event.values[1];
218 float roll_angle = event.values[2];
219 // Do something with these orientation angles.
220 }
221}
222</pre>
223
224<p>You do not usually need to perform any data processing or filtering of the raw data that you
225obtain from an orientation sensor, other than translating the sensor's coordinate system to your
226application's frame of reference. The <a
227href="{@docRoot}resources/samples/AccelerometerPlay/index.html">Accelerometer Play</a> sample shows
228you how to translate acceleration sensor data into another frame of reference; the technique is
229similar to the one you might use with the orientation sensor.</p>
230
231<h2 id="sensors-pos-mag">Using the Geomagnetic Field Sensor</h2>
232
233<p>The geomagnetic field sensor lets you monitor changes in the earth's magnetic field. The
234following code shows you how to get an instance of the default geomagnetic field sensor:</p>
235
236<pre>
237private SensorManager mSensorManager;
238private Sensor mSensor;
239...
240mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
241mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
242</pre>
243
244<p>This sensor provides raw field strength data (in &mu;T) for each of the three coordinate axes.
245Usually, you do not need to use this sensor directly. Instead, you can use the rotation vector
246sensor to determine raw rotational movement or you can use the accelerometer and geomagnetic field
247sensor in conjunction with the {@link android.hardware.SensorManager#getRotationMatrix
248getRotationMatrix()} method to obtain the rotation matrix and the inclination matrix. You can then
249use these matrices with the {@link android.hardware.SensorManager#getOrientation getOrientation()}
250and {@link android.hardware.SensorManager#getInclination getInclination()} methods to obtain azimuth
251and geomagnetic inclination data.</p>
252
253<h2 id="sensors-pos-prox">Using the Proximity Sensor</h2>
254<p>The proximity sensor lets you determine how far away an object is from a device. The following
255code shows you how to get an instance of the default proximity sensor:</p>
256
257<pre>
258private SensorManager mSensorManager;
259private Sensor mSensor;
260...
261mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
262mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
263</pre>
264
265<p>The proximity sensor is usually used to determine how far away a person's head is from the face
266of a handset device (for example, when a user is making or receiving a phone call). Most
267proximity sensors return the absolute distance, in cm, but some return only near and
268far values. The following code shows you how to use the proximity sensor:</p>
269
270<pre>
271public class SensorActivity extends Activity implements SensorEventListener {
272 private SensorManager mSensorManager;
273 private Sensor mProximity;
274
275 &#64;Override
276 public final void onCreate(Bundle savedInstanceState) {
277 super.onCreate(savedInstanceState);
278 setContentView(R.layout.main);
279
280 // Get an instance of the sensor service, and use that to get an instance of
281 // a particular sensor.
282 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
283 mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
284 }
285
286 &#64;Override
287 public final void onAccuracyChanged(Sensor sensor, int accuracy) {
288 // Do something here if sensor accuracy changes.
289 }
290
291 &#64;Override
292 public final void onSensorChanged(SensorEvent event) {
293 float distance = event.values[0];
294 // Do something with this sensor data.
295 }
296
297 &#64;Override
298 protected void onResume() {
299 // Register a listener for the sensor.
300 super.onResume();
301 mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
302 }
303
304 &#64;Override
305 protected void onPause() {
306 // Be sure to unregister the sensor when the activity pauses.
307 super.onPause();
308 mSensorManager.unregisterListener(this);
309 }
310}
311</pre>
312
313<p class="note"><strong>Note:</strong> Some proximity sensors return binary values that represent
314"near" or "far." In this case, the sensor usually reports its maximum range value in the far state
315and a lesser value in the near state. Typically, the far value is a value > 5 cm, but this can vary
316from sensor to sensor. You can determine a sensor's maximum range by using the {@link
317android.hardware.Sensor#getMaximumRange} method.</p>