Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sensors – Part I SE 395/595.

Similar presentations


Presentation on theme: "Sensors – Part I SE 395/595."— Presentation transcript:

1 Sensors – Part I SE 395/595

2 Topics Overview of Android sensor types Overview of Sensors Framework
Sensor class SensorManager class SensorEvent class SensorEventListener class Supported Sensor Types Basic elements of sensor utilization Accelerometer Example

3 Android Sensors Motion Position Environmental Rotation Acceleration
Orientation Location Environmental Ambient Temperature Barometric pressure Light intensity Humidity

4 Android Sensors Framework
Three main classes to work with: SensorManager class Request access to sensors Assign sensor event listeners Sensor class Encapsulates details of a particular sensor instance SensorEvent Encapsulation of new sensor data (data, type, accuracy, timestamp) SensorEventListener Responds to changes in value or accuracy for a sensor

5 Sensor.TYPE_<Sensor Type>
List at: ACCELEROMETER (1) AMBIENT_TEMEPRATURE (13) GRAVITY (9) GYROSCOPE (4) LIGHT (5) LINEAR_ACCELERATION (10) MAGNETIC_FIELD (2) ORIENATION (deprecated) PRESSURE (6) PROXIMITY (8) RELATIVE_HUMIDITY (12) ROTATION_VECTOR (11) TEMPERATURE (deprecated)

6 Sensor Manager Class Must request the service in your application’s onCreate method //Get Sensor Manager Instance _sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

7 Sensor Class Methods Identification Methods: Performance Methods:
getName() – name of the snesor getType() - what category of component is it? getVendor() – Who makes it? getVersion() – Which version of this component are we working with? Performance Methods: getPower() – “power” in mA draw getResolution() – resolution of the sensor’s measurements getMaximumRange() – maximum getMinimumDelay() – minimum time interval for updates Non-zero - streaming at the specified interval Zero – Reports only when changed Get it all!!! toString()

8 Sensor Manager Class: Obtain List of All Sensors
public void printAllSensors() { List<Sensor> sensors = _sensorManager.getSensorList(Sensor.TYPE_ALL); for (Sensor s :sensors) { Log.i("SensorDetails", s.toString()); }

9 Sample Output :59:17.310: I/SensorDetails(28379): {Sensor name="MPL Gyroscope", vendor="Invensense", version=1, type=4, maxRange= , resolution= , power=5.5, minDelay=1000} :59:17.310: I/SensorDetails(28379): {Sensor name="MPL Raw Gyroscope", vendor="Invensense", version=1, type=16, maxRange= , resolution= , power=5.5, minDelay=1000} :59:17.310: I/SensorDetails(28379): {Sensor name="MPL Accelerometer", vendor="Invensense", version=1, type=1, maxRange= , resolution= , power=5.5, minDelay=1000} :59:17.310: I/SensorDetails(28379): {Sensor name="MPL Magnetic Field", vendor="Invensense", version=1, type=2, maxRange=5461.0, resolution=0.9, power=0.15, minDelay=10000} :59:17.310: I/SensorDetails(28379): {Sensor name="MPL Raw Magnetic Field", vendor="Invensense", version=1, type=14, maxRange=5461.0, resolution=0.9, power=0.15, minDelay=10000} :59:17.310: I/SensorDetails(28379): {Sensor name="MPL Orientation", vendor="Invensense", version=1, type=3, maxRange=360.0, resolution=1.0E-5, power=11.15, minDelay=5000} :59:17.310: I/SensorDetails(28379): {Sensor name="MPL Rotation Vector", vendor="Invensense", version=1, type=11, maxRange=1.0, resolution=1.0E-5, power=11.15, minDelay=5000}

10 SensorEventListener Class
Activity may implement the listener interface Class inherits interface Class creates an instance of an object that implements the listener interface Two methods must be implemented public void onAccuracyChanged(Sensor sensor, int accuracy) public void onSensorChaged(SensorEvent event)

11 Sensor Event Class sensor – instance of Sensor class for sensor that generated the data values – array stores raw values from the sensor e.g. If type is TYPE_ACCELEROMETER event.values[0] – acceleration along x-axis event.values[1] – acceleration along y-axis event.values[2] – acceleration along z-axis accuracy – flag for sensor accuracy SensorManger.SENSOR_STATUS_ACCURACY_LOW timestamp – time of event in nanoseconds

12 Android Coordinate System
Source:

13 Accelerometer Example
Let’s write a simple app Displays x, y, and z axes accelerations in m/s^2 Layout Appropriate text fields Class Implement SensorEventListener Create stubs for its methods onCreate – create a sensor manager instance for accelerometer onResume – register this class as listener for accelerometer onPause – pause unregister onSensorChanged Set text views from layout to the accelerometer values: values[0], values[1], and values[2]

14 Accelerometer Application Demo
Demonstration of the accelerometer class

15 References Android.com, “Sensors Overview | Android Developers”, 2013.
Android.com. SensorEvent Reference


Download ppt "Sensors – Part I SE 395/595."

Similar presentations


Ads by Google