
Understanding Sensors in Android Development
Explore the world of sensors in Android with a focus on motion, position, and environmental sensors. Learn about their types, usage, and Android Sensor Framework for accessing sensor data for diverse applications.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Lecture 4: Lecture 4: Sensors Sensors Topics: Motion, Position, and Environmental Sensors Date: Feb 7, 2017
References ( References (s study tudy t these) hese) http://developer.android.com/guide/topics/sensors/sensors_overview.html http://developer.android.com/guide/topics/sensors/sensors_motion.html http://developer.android.com/guide/topics/sensors/sensors_position.html http://developer.android.com/guide/topics/sensors/sensors_environment.html
Sensors Overview Sensors Overview 1. Motion Sensors 2. Position Sensors 3. Environmental Sensors
Examples Examples 1. Motion Sensors Measure acceleration and rotational forces. e.g., accelerometers, gravity sensors, gyroscopes, and rotational vector sensors. 2. Position Sensors Measure the physical position of a device e.g., orientation sensors and magnetometers 3. Environmental Sensors Measure various environmental parameters e.g., barometers, photometers, and thermometers.
Types of Sensors Types of Sensors Sensor HW/SW Notes Use (Ax, Ay, Az) + g; ms2 TYPE_ACCELEROMETER HW Shake, Tilt 0C TYPE_AMBIENT_TEMPERATURE HW g = (gx, gy, gz); ms2 TYPE_GRAVITY SW/HW Shake, Tilt TYPE_GYROSCOPE HW = ( x, y, z) rad/s Spin, Turn TYPE_LIGHT HW Illumination; lx Brightness control (Ax, Ay, Az); no g; ms2 TYPE_LINEAR_ACCELERATION SW/HW TYPE_MAGNETIC_FIELD HW (Bx, By, Bz) T Compass TYPE_ORIENTATION SW 3 axis rotation; matrix Device position TYPE_PRESSURE HW hPa or mbar Air pressure TYPE_PROXIMITY HW cm; distant from screen Phone position TYPE_RELATIVE_HUMIDITY HW humidity (%) Dew point TYPE_ROTATION_VECTOR SW/HW Device s rotation vector 0C TYPE_TEMPERATURE HW
Android Sensor Framework Android Sensor Framework 1. Check Availability What sensors do I have? 2. Get Information e.g., maximum range, manufacturer, power requirements, and resolution. 3. Get Values e.g., raw sensor data, minimum rate. 4. Register/unregister for Sensor Events
Four Relevant Classes Four Relevant Classes 1. SensorManager: Create an instance of the sensor service. Accessing and listing sensors. Registering and unregistering sensor event listeners. Acquiring orientation information. Report sensor accuracy, set data acquisition rates, and calibrate sensors. 2. Sensor: Create an instance of a specific sensor. Determine a sensor's capabilities.
Four Relevant Classes Four Relevant Classes 3. SensorEvent Carries information about a sensor event: Raw data, sensor type, accuracy, timestamp 4. SensorEventListener Two callback methods that receive notifications: onSensorChanged() and onAccuracyChanged()
Working with Sensors Working with Sensors Step 1: Get the SensorManger from system services. Step 2: Get the Sensor (or a list) from the manager. private SensorManagersm; private Sensors; 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 proximity sensor
Working with Sensors 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 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) { } }
Code Practice: 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.
Good Practices: Good Practices: Check sensor before using. if(sm.getDefaultSensor(Sensor.TYPE_LIGHT) == null) { //Display sensor not available message. } Declare <uses-feature> in the manifest. <uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" /> Unregister at onPause(), register at onResume(). @Override protected void onPause() { super.onPause(); sm.unregisterListener(this); } @Override protected void onResume() { super.onResume(); sm.registerListener(this, s, 1000000); }
Good Practices: Good Practices: Don t block onSensorChanged() @Override public void onSensorChanged(SensorEvent event) { //Not a place to solve an NP-hard problem here*. } Choose sensor delays carefully. *it s a joke