3.1 Sensor Basics
3.1 Sensor Basics
Sensors
Lesson 3
● Ambient temperature
● Magnetic field at the x-axis, y-axis, and z-axis.
Values are in microtesla (μT)
● Proximity: Distance of device from object
● Light: Measures illuminance
● Pressure: Measures ambient air pressure
● Relative humidity
This work is licensed under a
Advanced Android Development Sensors Creative Commons Attribution 4.0 Inter 17
national License
Android sensor
framework
SensorManager
● Access and listen to sensors
● Register and unregister sensor event listeners
● Acquire orientation information
● Provides constants for accuracy, data acquisition rates,
and calibration
This work is licensed under a
Advanced Android Development Sensors Creative Commons Attribution 4.0 Inter 19
national License
Important framework classes
mSensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
Use getSensorList()
● To get all device sensors, use TYPE_ALL constant
List<Sensor> deviceSensors =
mSensorManager.getSensorList(Sensor.TYPE_ALL);
<uses-feature
android:name="android.hardware.sensor.accelerometer"
android:required="true" />
@Override
protected void onStop() {
super.onStop();
mSensorManager.unregisterListener(this);
}
if (sensorType == Sensor.TYPE_LIGHT) {
// Get light sensor string and fill data placeholder.
mTextSensorLight.setText(getResources().getString(
R.string.label_light, currentValue));
}
}
This work is licensed under a
Advanced Android Development Sensors Creative Commons Attribution 4.0 Inter 39
national License
What's next?