Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors
Sensors Overview Motion Sensors Position Sensors Environmental Sensors
Examples Motion Sensors Position Sensors Environmental Sensors Measure acceleration and rotational forces. e.g., accelerometers, gravity sensors, gyroscopes, and rotational vector sensors. Position Sensors Measure the physical position of a device e.g., orientation sensors and magnetometers Environmental Sensors Measure various environmental parameters e.g., barometers, photometers, and thermometers.
Types of Sensors Sensor HW/SW Notes Use TYPE_ACCELEROMETER HW (Ax, Ay, Az) + g; ms2 Shake, Tilt TYPE_AMBIENT_TEMPERATURE 0C TYPE_GRAVITY SW/HW g = (gx, gy, gz); ms2 TYPE_GYROSCOPE ω = (ωx, ωy, ωz) rad/s Spin, Turn TYPE_LIGHT Illumination; lx Brightness control TYPE_LINEAR_ACCELERATION (Ax, Ay, Az); no g; ms2 TYPE_MAGNETIC_FIELD (Bx, By, Bz) µT Compass TYPE_ORIENTATION SW 3 axis rotation; matrix Device position TYPE_PRESSURE hPa or mbar Air pressure TYPE_PROXIMITY cm; distant from screen Phone position TYPE_RELATIVE_HUMIDITY humidity (%) Dew point TYPE_ROTATION_VECTOR Device’s rotation vector TYPE_TEMPERATURE
Types of Sensors Sensor HW/SW Notes Use TYPE_ACCELEROMETER HW (Ax, Ay, Az) + g; ms2 Shake, Tilt TYPE_AMBIENT_TEMPERATURE 0C TYPE_GRAVITY SW/HW g = (gx, gy, gz); ms2 TYPE_GYROSCOPE ω = (ωx, ωy, ωz) rad/s Spin, Turn TYPE_LIGHT Illumination; lx Brightness control TYPE_LINEAR_ACCELERATION (Ax, Ay, Az); no g; ms2 TYPE_MAGNETIC_FIELD (Bx, By, Bz) µT Compass TYPE_ORIENTATION SW 3 axis rotation; matrix Device position TYPE_PRESSURE hPa or mbar Air pressure TYPE_PROXIMITY cm; distant from screen Phone position TYPE_RELATIVE_HUMIDITY humidity (%) Dew point TYPE_ROTATION_VECTOR Device’s rotation vector TYPE_TEMPERATURE
Android Sensor Framework Check Availability What sensors do I have? Get Information e.g., maximum range, manufacturer, power requirements, and resolution. Get Values e.g., raw sensor data, minimum rate. Register/unregister for Sensor Events
Four Relevant Classes SensorManager: Sensor: Create an instance of the sensor service. Accessing and listing sensors. Registering and unregistering sensor event listeners. Report sensor accuracy, set data acquisition rates, and calibrate sensors. Sensor: Create an instance of a specific sensor. Determine a sensor's capabilities.
Four Relevant Classes SensorEvent SensorEventListener Carries information about a sensor event: Raw data, sensor type, accuracy, timestamp SensorEventListener Two callback methods that receive notifications: onSensorChanged() and onAccuracyChanged()
Working with Sensors Step 1: Get the SensorManger from system services. Step 2: Get the Sensor (or a list) from the manager. private SensorManager sm; private Sensor s; private List<Sensor> l; sm = (SensorManager) getSystemService (Context.SENSOR_SERVICE); l = sm.getSensorList(Sensor.TYPE_ALL); s = sm.getDefaultSensor(Sensor.TYPE_LIGHT); Returns “null” if no light sensor
Working with Sensors Step 3 (optional): Get information about a Sensor. s.getName(); s.getMaximumRange(); s.getResolution(); s.getMinDelay(); s.getPower(); s.getVendor(); s.getVersion();
Working with Sensors Step 4: Implement and Register SensorEventListener public class MainActivity extends AppCompatActivity implements SensorEventListener{ private SensorManager sm; private Sensor s; private List<Sensor> l; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); l = sm.getSensorList(Sensor.TYPE_ALL); s = sm.getDefaultSensor(Sensor.TYPE_LIGHT); sm.registerListener(this, s, 1000000); } @Override public void onSensorChanged(SensorEvent event) { } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }
Good Practices: Check sensor before using. Declare <uses-feature> in the manifest. Unregister at onPause(), register at onResume(). if(sm.getDefaultSensor(Sensor.TYPE_LIGHT) == null) { //Display sensor not available message. } <uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" /> @Override protected void onPause() { super.onPause(); sm.unregisterListener(this); } @Override protected void onResume() { super.onResume(); sm.registerListener(this, s, 1000000); }
Good Practices: Don’t block onSensorChanged() Choose sensor delays carefully. @Override public void onSensorChanged(SensorEvent event) { //Not a place to solve an NP-hard problem here. }
Code Practice: Show a list of available sensors on the device. Get Gravity Sensor readings. Report them at 1 second interval. Now, add another sensor, e.g., Light Sensor. Report light sensor at 0.5 second interval.
References (study these) https://developer.android.com/guide/topics/sensors/sensors_overview https://developer.android.com/guide/topics/sensors/sensors_motion https://developer.android.com/guide/topics/sensors/sensors_position https://developer.android.com/guide/topics/sensors/sensors_environment https://www.quora.com/What-are-all-the-sensors-used-in-smartphones-and- explain-its-purpose-and-its-working