Not all sensors are used to access the state of the device. Some are used to describe the world in which the device exists.
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:
SensorManager
instance to access the sensor:var manager = SensorManager.FromContext(this);
var type = SensorType.Light; var light = manager.GetDefaultSensor(type); if (light == null) { // handle no significant motion sensor }
ISensorEventListener
interface:public class MyActivity : Activity, ISensorEventListener { public void OnAccuracyChanged( Sensor sensor, SensorStatus accuracy) { } public void OnSensorChanged(SensorEvent e) { } }
OnSensorChanged()
method to read the single value from the sensor:var lx = e.Values[0];
manager.RegisterListener( this, accelerometer, SensorDelay.Fastest);
manager.UnregisterListener(this);
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.
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.