Sensors in Android
Types of Sensors Motion Sensor Environmental Sensors Position sensors Accelerometers Gravity sensors Gyroscopes Rotational vector sensors Environmental Sensors Barometers Photometers Thermometers Position sensors Orientation sensors Magnetometers
Sensors in Android Sensor Type Description Common Uses TYPE_ ACCELEROMETER Hardware Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity. Motion detection (shake, tilt, etc.). AMBIENT_TEMPERATURE Measures the ambient room temperature in degrees Celsius (°C). Monitoring air temperatures. GRAVITY Software or Hardware Measures the force of gravity in m/s2 that is applied to a device on all three physical axes (x, y, z). GYROSCOPE Measures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z). Rotation detection (spin, turn, etc.). LIGHT Measures the ambient light level (illumination) in lx. Controlling screen brightness. LINEAR_ACCELERATION Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), excluding the force of gravity. Monitoring acceleration along a single axis. MAGNETIC_FIELD Measures the ambient geomagnetic field for all three physical axes (x, y, z) in μT. Creating a compass.
Sensors in Android TYPE_ ORIENTATION Software Measures degrees of rotation that a device makes around all three physical axes (x, y, z). As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method. Determining device position. PRESSURE Hardware Measures the ambient air pressure in hPa or mbar. Monitoring air pressure changes. PROXIMITY Measures the proximity of an object in cm relative to the view screen of a device. This sensor is typically used to determine whether a handset is being held up to a person's ear. Phone position during a call. RELATIVE_HUMIDITY Measures the relative ambient humidity in percent (%). Monitoring dewpoint, absolute, and relative humidity. ROTATION_VECTOR Software or Hardware Measures the orientation of a device by providing the three elements of the device's rotation vector. Motion detection and rotation detection. TEMPERATURE Measures the temperature of the device in degrees Celsius (°C). This sensor implementation varies across devices and this sensor was replaced with the TYPE_AMBIENT_TEMPERATURE sensor in API Level 14
Android Sensor Framework Determining what sensors are available Determining the sensor’s capability Defining min sampling rate Acquiring raw sensor data Registering sensor event listeners
Sensor Framework SensorManager Sensor SensorEvent SensorEventListener Accessing/listing sensors Registering/unregistering sensor event listeners Acquiring orientation information Sensor Determining the capability of sensors SensorEvent Providing information about sensor events Including raw sensor data, sensor, the accuracy, and timestamp. SensorEventListener Containing callbacks to receiving notifications of sensor events
Identifying Sensors Get a SensorManager SensorManager mSM = (SensorManager)getSystemService(Context.SENSOR_SERVICE); Get a list of all sensors available on device List<Sensor> sensors = mSM.getSensorList(Sensor.TYPE_ALL); Get a list of all sensors of a given type List<Sensor> sensors = mSM.getSensorList(Sensor.TYPE_GRAVITY); Get the default sensor of a given type Multiple sensors of the same type One must be assigned as default If (mSM.getDefaultSensor(Sensor.TYPE_GRAVITY) != null) { // sensor available } else { // no gravity sensor available }
Getting Sensor Information Sensor class offers methods to get sensor info getResolution() getMaximumRange() getVendor() getVersion() getMinDelay(): determines the max rate of sampling > 0: streaming sensor = 0: reporting data only when there is a change
Monitoring Sensor Events SensorEventListener onAccuracyChanged(Sensor s, int accuracy) called when the accuracy of the sensor has changed SENSOR_STATUS_ACCURACY_LOW, SENSOR_STATUS_ACCURACY_MEDIUM, SENSOR_STATUS_ACCURACY_HIGH, or SENSOR_STATUS_UNRELIABLE. onSensorChanged(SensorEvent se) Called with a SensorEvent object for new sensor data The accuracy of the data, The sensor, the timestamp of the change, and new data
Registering Listeners -1 public class SensorActivity extends Activity implements SensorEventListener { private SensorManager mSensorManager; private Sensor mLight; public final void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); } public final void onAccuracyChanged(Sensor sensor, int accuracy) { // Do something here if sensor accuracy changes. public final void onSensorChanged(SensorEvent event) { // The light sensor returns a single value. // Many sensors return 3 values, one for each axis. float lux = event.values[0]; // Do something with this sensor value. }
Registering Listeners - 2 public class SensorActivity extends Activity implements SensorEventListener { … protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL); // sample rate } protected void onPause() { super.onPause(); // if not unregistered, the sensor will continue polling data // which would drain the battery mSensorManager.unregisterListener(this); }
SensorEvent public int accuracy: public Sensor sensor: The accuracy of this event. public Sensor sensor: The sensor that generated this event. public long timestamp: The time in nanosecond at which the event happened public final float[] values: raw values of the sensor data. The length and contents of the values are sensor dependent
Sensor Coordinate System Default orientation X axis – horizontal pointing right Y axis – vertical pointing up Z axis – pointing toward the outside of the screen face. Coordinate system does not change when orientation changes You can use getOrientation() to determine screen rotation and use remapCoordinateSystem() to map sensor coordinates to screen coordinates.
Best Practices Unregister sensor listeners Unregistering listners in onPause(). Otherwise the sensor continues acquiring data Don’t test your code on the emulator Emulator cannot accurately emulate sensors Don’t block the onSensorChanged() When sensor data changes often, this method is called often. Keep it as short as possible. Avoid using deprecated methods/sensors Verify sensor’s availability before using them Not all devices provide all sensors Choose sensor delays carefully Specify the largest delay that is fast enough for your app.