Listening to sensor data

Just as touching a device screen is input, so is rotating and moving the actual physical device in its environment. Most devices are built with several sensors that can be used to read the state of the device in its environment.

How to do it...

We can access all the available sensors and the sensor data on the device through the SensorManager type:

  1. The instance of the SensorManager type is obtained from the current context:
    var manager = SensorManager.FromContext(this);
  2. Once we have the manager, we check whether a particular sensor exists, and return the instance of it using the GetDefaultSensor() method:
    var type = SensorType.Accelerometer; var accelerometer = manager.GetDefaultSensor(type); if (accelerometer == null) { // ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.