Discovering the environment

Not all sensors are used to access the state of the device. Some are used to describe the world in which the device exists.

How to do it...

The environment sensors are the same as most of the sensors, in that they require an implementation of the sensor event listener interface to be registered with the sensor manager:

  1. As with all sensors, we have to use the SensorManager instance to access the sensor:
    var manager = SensorManager.FromContext(this);
  2. When we have the sensor manager, we obtain the specific sensor:
    var type = SensorType.Light;
    var light = manager.GetDefaultSensor(type);
    if (light == null) {
      // handle no significant motion sensor
    }
  3. Next, we implement the ISensorEventListener interface:
    public class MyActivity : Activity, ISensorEventListener {
      public void OnAccuracyChanged(
      Sensor sensor, SensorStatus accuracy) {
      }
      public void OnSensorChanged(SensorEvent e) {
      }
    }
  4. Then, we implement the OnSensorChanged() method to read the single value from the sensor:
    var lx = e.Values[0];
  5. To start receiving events, we register the listener along with the desired update rate:
    manager.RegisterListener(
      this, accelerometer, SensorDelay.Fastest);
  6. Once we are finished with the sensor, we unregister it:
    manager.UnregisterListener(this);

How it works...

Touch describes how the user interacts with the device and sensors describe how the device exists in the world. But, another form of input that we can utilize is the data from the actual environment that the device is in. This data can be the amount of light in the environment or the air pressure, moisture, and temperature.

The four sensors currently available are the relative ambient humidity sensor, the illuminance sensor, the ambient pressure sensor, and the ambient temperature sensor. There are actually two temperature sensors: the Temperature sensor type and the AmbientTemerature instance. The first sensor measures the actual device CPU temperature and the second measures the ambient temperature near the device.

Tip

There are two temperature sensors—one measures the temperature of the CPU and the other measures the air temperature.

Although these sensors are not as common as the others, such as the accelerometer, we can still use these to enhance an app. The light or illuminance sensor is the most commonly available sensor in most devices, because it is used to control the display brightness.

Although they are functionally the same as the other sensors, the environment sensors only return a single sensor value from the Values property on the SensorEvent argument. Also, this value does not require filtering to reduce noise, and it can be consumed directly from the event.

Tip

Environment sensors only have a single sensor value, which does not need filtering.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset