190 likes | 335 Views
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.
E N D
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 • 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 • 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);
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
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 • 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));
Sensor class • Concrete class • Represents specific sensor • Contains: • methods to obtain general metadata information • fields for specific sensor types
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 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 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 • Interface • Notified by SensorManager if status changes • SensorManager must register with the SensorEventListener for all desired Sensors
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() 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... } }
SensorEventListener • Registering/Unregistering a Manager with a Listener • methods in SensorManager class • take care to utilize resources effectively • register in onResume() • unregister in onPause()