sensing
Download
Skip this Video
Download Presentation
Sensing

Loading in 2 Seconds...

play fullscreen
1 / 19

Sensing - PowerPoint PPT Presentation


  • 102 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Sensing' - vandana-kartik


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
sensing1
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
components of sensing in android
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
sensormanager
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);

sensormanager1
SensorManager
  • important methods
    • getSensorList
      • argument: integer indicating desired sensor type (or all sensors)
      • returns List 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
sensormanager2
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.)
sample code
Sample code
  • Instantiation

SensorManagersm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

  • Retrieve sensors

List 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));

sensor class1
Sensor class
  • Concrete class
  • Represents specific sensor
  • Contains:
    • methods to obtain general metadata information
    • fields for specific sensor types
sensor class2
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.
sensorevent class1
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
sensorevent class2
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.
sensoreventlistener
SensorEventListener
  • Interface
  • Notified by SensorManager if status changes
  • SensorManager must register with the SensorEventListener for all desired Sensors
sensoreventlistener1
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
sample code onsensorchanged
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...

}

}

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