20.08.2016 Views

Professional Android 4 Application Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

506 x CHAPTER 12 HARDWARE SENSORS<br />

LISTING 12-9 (continued)<br />

angle[2] += sensorEvent.values[2] * dT;<br />

}<br />

lastTime = sensorEvent.timestamp;<br />

}<br />

public void onAccuracyChanged(Sensor sensor, int accuracy) {}<br />

};<br />

SensorManager sm<br />

= (SensorManager)getSystemService(Context.SENSOR_SERVICE);<br />

int sensorType = Sensor.TYPE_GYROSCOPE;<br />

sm.registerListener(myGyroListener,<br />

sm.getDefaultSensor(sensorType),<br />

SensorManager.SENSOR_DELAY_NORMAL);<br />

code snippet PA4AD_Ch12_Sensors/src/MyActivity.java<br />

It’s worth noting that orientation values derived solely from a gyroscope can become increasingly<br />

inaccurate due to calibration errors and noise. To account for this effect, gyroscopes are often used<br />

in combination with other sensors — particularly accelerometers — to provide smoother and more<br />

accurate orientation results. A virtual gyroscope was introduced in <strong>Android</strong> 4.0 (API level 14) that<br />

attempts to reduce this drift effect.<br />

INTRODUCING THE ENVIRONMENTAL SENSORS<br />

One of the most exciting areas of innovation in mobile hardware is the inclusion of an increasingly<br />

rich array of sensors. In addition to the orientation and movement sensors described earlier in this<br />

chapter, environmental Sensors are now becoming available in many <strong>Android</strong> devices.<br />

Like orientation Sensors, the availability of specific environmental Sensors depends on the host<br />

hardware. Where they are available, environmental Sensors can be used by your application to:<br />

‰ Improve location detection by determining the current altitude<br />

‰ Track movements based on changes in altitude<br />

‰ Alter the screen brightness or functionality based on ambient light<br />

‰ Make weather observations and forecasts<br />

‰ Determine on which planetary body the device is currently located<br />

Using the Barometer Sensor<br />

A barometer is used to measure atmospheric pressure. The inclusion of this sensor in some <strong>Android</strong><br />

devices makes it possible for a user to determine his or her current altitude and, potentially, to<br />

forecast weather changes.<br />

To monitor changes in atmospheric pressure, register an implementation of SensorEventListener<br />

with the Sensor Manager, using a Sensor object of type Sensor.TYPE_PRESSURE. The current

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!