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 : ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.