The Android platform supports three broad categories of sensors:
    Motion sensors
    These sensors measure acceleration forces and rotational forces along three axes. This category includes accelerometers, gravity  sensors, gyroscopes, and rotational vector sensors.
    Environmental sensors
    These sensors measure various environmental parameters, such as ambient air temperature and pressure, illumination, and humidity. This category includes barometers, photometers, and thermometers.
      Position sensors

    These sensors measure the physical position of a device. This category includes orientation sensors and magnetometers.

TO DO

  •     Determine which sensors are available on a device.


  •    Determine an individual sensor's capabilities, such as its maximum range, manufacturer, power requirements, and resolution.


  •     Acquire raw sensor data and define the minimum rate at which you acquire sensor data.


  •     Register and unregister sensor event listeners that monitor sensor changes.


Sensor Framework

   The sensor framework is part of the android.hardware package and includes the following classes and interfaces:
    SensorManager
    You can use this class to create an instance of the sensor service. This class provides various methods for accessing and listing sensors, registering and unregistering sensor event listeners, and acquiring orientation information.
   Sensor
    You can use this class to create an instance of a specific sensor.
   SensorEvent
    The system uses this class to create a sensor event object, which provides information about a sensor event. A sensor event object includes the following information: the raw sensor data, the type of sensor that generated the event, the accuracy of the data, and the timestamp for the event.
SensorEventListener
    You can use this interface to create two callback methods that receive notifications (sensor events) when sensor values change or when sensor accuracy changes.


Identifying Sensors and Sensor Capabilities

SensorManager mSensorManager;

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


Get a listing of every sensor on a device by calling the getSensorList() method and using the TYPE_ALL constant.


List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);


mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
  // Success! There's a magnetometer.
  }
else {
  // Failure! No magnetometer.
  }

Sensor Event Listener

This Listener provides 2 methods:

         Called when the accuracy of a sensor has changed.

         Called when sensor values have changed.


Getting Value from a sensor

   

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
  }

  @Override
  public final void onAccuracyChanged(Sensor sensor, int accuracy) {
    // Do something here if sensor accuracy changes.
  }

  @Override
  public final void onSensorChanged(SensorEvent event) {
    // The light sensor returns a single value.
    // Many sensors return 3 values, one for each axis.
    float lux = event.values[0];
    // Do something with this sensor value.
  }
 

Register & Unregister sensor listeners

  • Register
@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mSensorManager.registerListener(this, lightSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    } 

  • Unregister

@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mSensorManager.unregisterListener(this);
    } 

Handling Different Sensors


  • If you are using different sensors in your app, detect the sensors at runtime and to whatever you wish to do.

if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
            System.out.println("Light value is " + event.values[0]);
        }

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float a0 = event.values[0];
            float a1 = event.values[1];
            float a2 = event.values[2];
           
            System.out.println("Acclerometer value is " + a0 +" "+ a1 +" "+ a2);
        } 

Best Practices for Accessing and Using Sensors

  • Unregister sensor listeners
  • Don't test your code on the emulator
  • Don't block the onSensorChanged() method
    Sensor data can change at a high rate, which means the system may call the onSensorChanged(SensorEvent) method quite often.You should do as little as possible within the onSensorChanged(SensorEvent) method so you don't block it.
  • Avoid using deprecated methods or sensor types
    Several methods and constants have been deprecated. In particular, the TYPE_ORIENTATION sensor type has been deprecated. To get orientation data you should use the getOrientation() method instead. Likewise, the TYPE_TEMPERATURE sensor type has been deprecated. You should use the TYPE_AMBIENT_TEMPERATURE sensor type instead on devices that are running Android 4.0.
  • Dont assume, verify sensors before you use them
  • Choose sensor delays carefully
    When you register a sensor with the registerListener() method, be sure you choose a delivery rate that is suitable for your application or use-case. Sensors can provide data at very high rates. Allowing the system to send extra data that you don't need wastes system resources and uses battery power.












                                                                                                           THANK YOU :-)

Android Sensors

By Torry Harris Business Solutions