08A Sensors Flashcards
Sensor Overview
- Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions
- These sensors are capable of providing raw data with high precision and accuracy
Three Categories of Sensors
• Motion sensors: measure acceleration forces and rotational forces along three axes
• Environmental sensors: measure various
environmental parameters, such as ambient air temperature and pressure, illumination, and humidity
• Position sensors: measure the physical
position of a device
Using Sensors
• Access sensors available on the device and acquire raw sensor data by using the Android sensor framework
• The sensor framework provides several
classes and interfaces that help you perform a wide variety of sensor-related tasks
Using Sensors: You can …
- Determine which sensors are available on a device
- Determine an individual sensor’s capabilities, such as its maximum range, manufacturer, power requirements, and resolution
- Acquire raw sensor data and define the minimum rate at which you acquire sensor data
- Register and unregister sensor event listeners that monitor sensor changes
- You can access these sensors and acquire raw sensor data by using the Android sensor framework. The sensor framework is part of the android.hardware package
SensorManager
- Use this class to create an instance of the sensor service.
- Provides various methods for accessing and listing sensors, registering and unregistering sensor event listeners, and acquiring orientation information.
- Provides several sensor constants that are used to report sensor accuracy, set data acquisition rates, and calibrate sensors.
Sensor class
• Use this class to create an instance of a
specific sensor.
• Provides various methods that let you
determine a sensor’s capabilities
SensorEventListener
• Use this interface to create two callback
methods that receive notifications (sensor
events) when sensor values change or when
sensor accuracy changes
• onSensorChanged(SensorEvent e)
• onAccuracyChanged(Sensor s, Accuracy a)
Two Sensor Tasks
- Identifying sensors and sensor capabilities
* Monitor sensor events
Sensor Availability
- Different Android versions support different sensors
- Not every device has all sensors. For example, my phone has a pressure sensor while my touchpad does not. Cheap devices tend to have fewer sensors
Using Sensors
• Use the getResolution() and
getMaximumRange() methods to obtain a
sensor’s resolution and maximum range of
measurement
• Use the getPower() method to obtain a
sensor’s power requirements
• The getVendor() and getVersion() let you look for a specific sensor.
• If it isn’t present, you can use a different one or tell the user your app can’t run
• Another useful method is getMinDelay(),
which returns the minimum time interval (in
microseconds) a sensor can use to sense data
• Any sensor that returns a non-zero value for getMinDelay() is a streaming sensor.
Monitoring Sensor Events
• To monitor raw sensor data you need to implement two callback methods that are exposed through the SensorEventListener interface: onAccuracyChanged() and onSensorChanged().
Sensor Accuracy Changes
• The system invokes the onAccuracyChanged() method, providing you with a reference to the Sensor object that changed and the new accuracy of the sensor. Accuracy is represented by one of four status constants: SENSOR_STATUS_ACCURACY_LOW, SENSOR_STATUS_ACCURACY_MEDIUM, SENSOR_STATUS_ACCURACY_HIGH, or SENSOR_STATUS_UNRELIABLE.
Sensor Value Changes
- The system invokes the onSensorChanged() method, providing you with a SensorEvent object
- A SensorEvent object contains information about the new sensor data, including: the accuracy of the data, the sensor that generated the data, the timestamp at which the data was generated, and the new data that the sensor recorded.
Handling Pause and Resume
• The example uses the onResume() and
onPause() callback methods to register and
unregister the sensor event listener
• As a best practice you should always disable sensors you don’t need, especially when your activity is paused. Failing to do so can drain the battery in just a few hours The system will not disable sensors automatically when the
screen turns off.
Sensor Coordinate System
• The sensor framework generally uses a standard 3-axis coordinate system to express data values