Sensors Overview
The Android platform supports three broad categories of sensors:
a> Motion sensors
These sensors measure acceleration forces and rotational forces along three axes. This category includes accelerometers, gravity sensors, gyroscopes, and rotation vector sensors.
b> Environmental sensors
These sensors measure various environmental parameters, such as ambient air temperature and pressure, illumination, and humidity. This category includes barometers, photometers, and thermometers.
c> Position sensors
These sensors measure the physical position of a device. This category includes orientation sensors and magnetometers.
We can use the sensor framework to do the following:
a> Determine which sensors are available on a device.
b> Determine an individual sensor's capabilities
c> Acquire raw sensor data and define the minimum rate at which you acquire sensor data.
d> Register and unregister sensor event listeners that monitor sensor changes.
Introduction to Sensors
Most handset devices and tablets have an accelerometer and a magnetometer, but fewer devices have barometers or thermometers.
Sensor Framework
In a typical application we use these sensor-related APIs to perform two basic tasks: a> Identifying sensors and sensor capabilities, and b> Monitor sensor events.
Identifying sensors ans Sensor Capabilities
To identify the sensors that are on a device you first need to get a reference to the sensor service. To do this, you create an instance of the SensorManager class by calling the getSystemService() method and passing in the SENSOR_SERVICE argument. Next, you can get a listing of every sensor on a device by calling the getSensorList() method and using the TYPE_ALL constant.
private List<Sensor> getAllSensorsOnDevice() {
SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
return manager.getSensorList(Sensor.TYPE_ALL);
}
You can also determine whether a specific type of sensor exists on a device by using the getDefaultSensor() method and passing in the type constant for a specific sensor.
private boolean isAccelerometerSensorAvailable(){
SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
return manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) == null;
}
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().
a> A sensor's accuracy changes:
In this case 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, SENSOR_STATUS_ACCURACY_UNRELIABLE.
b> A sensor reports a new value:
In this case the system invokes the onSensorChanged() method, providing you with a SensorEvent object.
public class MainActivity extends Activity {
private SensorManager mSensorManager;
private Sensor mSensor;
private SensorEventListener mSensorEventListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
Log.i("tag", "onSensorChanged");
int value = (int) event.values[0];
Log.i("tag", "value = " + value);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.i("tag", "onAccuracyChanged");
}
};
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mSensorEventListener, mSensor,
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(mSensorEventListener, mSensor);
}
}