The Android platform supports three broad categories of sensors:
Motion sensors
These sensors measure acceleration forces and rotational forces along three axes. This category includes accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.
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.
Position sensors
These sensors measure the physical position of a device. This category includes orientation sensors and magnetometers.
The sensor framework is part of the android.hardware package and includes the following classes and interfaces:
SensorManager
You can use this class to create an instance of the sensor service. This class provides various methods for accessing and listing sensors, registering and unregistering sensor event listeners, and acquiring orientation information.
Sensor
You can use this class to create an instance of a specific sensor.
SensorEvent
The system uses this class to create a sensor event object, which provides information about a sensor event. A sensor event object includes the following information: the raw sensor data, the type of sensor that generated the event, the accuracy of the data, and the timestamp for the event.
SensorEventListener
You can use this interface to create two callback methods that receive notifications (sensor events) when sensor values change or when sensor accuracy changes.
SensorManager mSensorManager;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Get a listing of every sensor on a device by calling the
getSensorList()
method and using the TYPE_ALL
constant.
List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
// Success! There's a magnetometer.
}
else {
// Failure! No magnetometer.
}
This Listener provides 2 methods:
Called when the accuracy of a sensor has changed.
|
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
@Override
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.
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mSensorManager.registerListener(this, lightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mSensorManager.unregisterListener(this);
}
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
System.out.println("Light value is " + event.values[0]);
}
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float a0 = event.values[0];
float a1 = event.values[1];
float a2 = event.values[2];
System.out.println("Acclerometer value is " + a0 +" "+ a1 +" "+ a2);
}