1 / 19

Sensing

Sensing. Sensing. Possible sensors on devices Documented in SensorEvent class Accelerometer (m/s 2 ) acceleration in x, y, z axes Magnetic Field (micro Tesla) ambient magnetic field in x, y, z axes Light (lux) ambient light level Proximity (cm) Temperature (degrees C) Other.

Download Presentation

Sensing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sensing

  2. Sensing • Possible sensors on devices • Documented in SensorEvent class • Accelerometer (m/s2) • acceleration in x, y, z axes • Magnetic Field (micro Tesla) • ambient magnetic field in x, y, z axes • Light (lux) • ambient light level • Proximity (cm) • Temperature (degrees C) • Other

  3. Components of Sensing in Android • SensorManager class • Sensor class • Additional class (relative to Location) to handle multiple Sensor types • Metadata stored here (name, type, etc.) • SensorEvent class • Sensor data stored here • SensorEventListener interface • SensorListener interface has been deprecated

  4. SensorManager class

  5. SensorManager • Concrete class • Gives access to Sensor devices and classes • Not instantiated directly • instantiated through the given context’s getSystemService() method SensorManagersm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

  6. SensorManager • important methods • getSensorList • argument: integer indicating desired sensor type (or all sensors) • returns List <Sensor> of all sensors of argument type • getDefaultSensor • argument: integer indicating desired sensor type • returns device’s sensor of argument type • registerListener • arguments: context, sensor, and delay rate • unregisterListener • arguments: SensorEventListener and optionally a Sensor

  7. SensorManager • Some fields in SensorManager class • Valid delay rates when registering a listener • SENSOR_DELAY_X • X: FASTEST, GAME, NORMAL, UI • UI is most standard – good for typical development use • Other constants • MAGNETIC_FIELD_EARTH_MAX (or MIN) • GRAVITY_X • X: EARTH, MARS, NEPTUNE, etc. • GRAVITY_DEATH_STAR_I (first Star Wars Death Star) • 3.5303614 x 10–7 (of course, this is just an estimate…) • Other (LIGHT_X, PRESSURE_STANDARD_ATMOSPHERE, etc.)

  8. Sample code • Instantiation SensorManagersm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); • Retrieve sensors List <Sensor> ls = sm.getSensorList(Sensor.TYPE_ALL); • Retrieve specific sensor Sensor myAccelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); • Registering a listener sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI); • Unregistering a listener sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));

  9. Sensor class

  10. Sensor class • Concrete class • Represents specific sensor • Contains: • methods to obtain general metadata information • fields for specific sensor types

  11. Sensor class • important methods • getters for data • getName() • returns String value (on Emulator: “Goldfish 3-axis Accelerometer”) • getType() • returns int value (1 for Accelerometer) • others • important fields (all integers) • TYPE_ALL • TYPE_ZZZ (ZZZ is sensor type) • ACCELEROMETER, LIGHT, TEMPERATURE, etc.

  12. SensorEvent class

  13. SensorEvent class • Concrete class • Represents an event tied to a specific sensor • Contains: • identification of the sensor causing the event • data associated with the event

  14. SensorEvent class • no methods (other than Object’s methods) • important fields (all public) • sensor, accuracy, and timestamp • values – documented in SensorEvent class • float array containing values associated with the sensed information • if sensor is accelerometer • 3 element array with x, y, z axes’ data • if sensor is temperature • 1 element array containing temperature • etc.

  15. SensorEventListener interface

  16. SensorEventListener • Interface • Notified by SensorManager if status changes • SensorManager must register with the SensorEventListener for all desired Sensors

  17. SensorEventListener • 2 abstract methods • onAccuracyChanged • sensors can have one of four levels of accuracy • placement or calibration may affect accuracy • parameters are the sensor (Sensor) and the accuracy (int) • onSensorChanged • called whenever the value(s) of a Sensor have changed • 1 parameter • the SensorEvent

  18. Sample Code – onSensorChanged() public void onSensorChanged(SensorEvent event) { switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: ((TextView)findViewById(R.id.tvAccX)).setText("Acc X: " + event.values[0]); ((TextView)findViewById(R.id.tvAccY)).setText("Acc Y: " + event.values[1]); ((TextView)findViewById(R.id.tvAccZ)).setText("Acc Z: " + event.values[2]); break; case Sensor.TYPE_AMBIENT_TEMPERATURE: ((TextView)findViewById(R.id.tvTemp)).setText("Temp: " + event.values[0]); break; ...cases continue for all desired sensors... } }

  19. SensorEventListener • Registering/Unregistering a Manager with a Listener • methods in SensorManager class • take care to utilize resources effectively • register in onResume() • unregister in onPause()

More Related