SensorManager lets you access the device's sensors. Get an instance of this class by calling getSystemService()
with the argument SENSOR_SERVICE
.
private SensorManager mSensorManager;
...
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
SensorManager provides two methods to access Sensor objects:
getSensorList()
: returns all the sensors.getDefaultSensor()
: returns the default sensor for the specified type.Example:
List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
Example:
if (mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE) != null) {
// Success! There's a pressure sensor.
}
else {
// Failure! No pressure sensor.
}
Source code: SensorManager.java