Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors Date: Feb 11, 2016.

Similar presentations


Presentation on theme: "Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors Date: Feb 11, 2016."— Presentation transcript:

1 Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors Date: Feb 11, 2016

2 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 References (study these)

3 Sensors Overview 1.Motion Sensors 2.Position Sensors 3.Environmental Sensors

4 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.

5 Types of Sensors SensorHW/SWNotesUse TYPE_ACCELEROMETERHW(A x, A y, A z ) + g; ms 2 Shake, Tilt TYPE_AMBIENT_TEMPERATUREHW*C TYPE_GRAVITYSW/HWg = (g x, g y, g z ); ms 2 Shake, Tilt TYPE_GYROSCOPEHWω = (ω x, ω y, ω z ) rad/sSpin, Turn TYPE_LIGHTHWIllumination; lxBrightness control TYPE_LINEAR_ACCELERATIONSW/HW(A x, A y, A z ); no g; ms 2 TYPE_MAGNETIC_FIELDHW(B x, B y, B z ) µTCompass TYPE_ORIENTATIONSW3 axis rotation; matrixDevice position TYPE_PRESSUREHWhPa or mbarAir pressure TYPE_PROXIMITYHWcm; distant from screenPhone position TYPE_RELATIVE_HUMIDITYHWhumidity (%)Dew point TYPE_ROTATION_VECTORSW/HWDevice’s rotation vector TYPE_TEMPERATUREHW*C

6 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

7 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.

8 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()

9 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 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

10 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();

11 Working with Sensors Step 4: Implement and Register SensorEventListener public class MainActivity extends AppCompatActivity implements SensorEventListener{ private SensorManager sm; private Sensor s; private List 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) { } }

12 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.

13 Good Practices: Check sensor before using. Declare in the manifest. Unregister at onPause(), register at onResume(). if(sm.getDefaultSensor(Sensor.TYPE_LIGHT) == null) { //Display sensor not available message. } @Override protected void onPause() { super.onPause(); sm.unregisterListener(this); } @Override protected void onResume() { super.onResume(); sm.registerListener(this, s, 1000000); }

14 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*. } *it’s a joke


Download ppt "Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors Date: Feb 11, 2016."

Similar presentations


Ads by Google