Listening for sensor triggers

Some sensors do not provide a continuous stream of data, but rather raise an event when something happens.

How to do it...

To use trigger-based sensors, we also make use of the sensor manager. Unlike stream-based sensors, we inherit from the TriggerEventListener type:

  1. First, we need the instance of the SensorManager type:
    var manager = SensorManager.FromContext(this);
  2. Next, we get a reference to the sensor. If the sensor is null, then it is not available on the device:
    var type = SensorType.SignificantMotion;
    var motion = manager.GetDefaultSensor(type);
    if (motion == null) {
      // handle no significant motion sensor
    }
  3. Then, we create inherit from the TriggerEventListener type:
    private class MyListener : TriggerEventListener {
      public override void OnTrigger(TriggerEvent e) {
        // handle significant motion
      }
    }
  4. We create an instance of this trigger event listener:
    listener = new MyListener();
  5. Once we have the listener, we register it with the sensor manager along with the sensor:
    manager.RequestTriggerSensor(listener, motion);
  6. When we no longer need the trigger, we unregister it:
    manager.CancelTriggerSensor(listener, motion);

How it works...

Devices have many sensors, some providing a stream of data to apps and others providing data when certain events occur. These events might be triggered when the user takes a step or when the user moves to a new location.

These sensors work by only enabling when a listener is attached, then as soon as the event it raised, the trigger is invoked and the listener detached. In order for the trigger to be invoked when the event occurs again, we need to reregister the listener.

Note

Trigger-based sensors automatically unregister when the event occurs.

Just like when accessing all other sensors, we need to use the SensorManager instance. Once we have obtained the sensor manager, we need to ensure that the particular sensor exists on the device. As with all sensors, we use the GetDefaultSensor() method to do this.

Once we have the sensor, we need an instance of the TriggerEventListener type. Unlike other sensors, this is not an interface to be implemented. Instead, it is a type that we inherit from and override the OnTrigger() method.

Note

Trigger-based sensors do not require the implementation of an interface but rather an instance of a type inheriting from TriggerEventListener.

The OnTrigger() method has a single TriggerEvent argument, which provides access to the timestamp of the event and the actual sensor data. The sensor data is provided in the form of a float array property named Values.

When we want to register the listener with the sensor, we pass an instance of the listener and the sensor to the RequestTriggerSensor() method on the sensor manager. As soon as the event is raised, the OnTrigger() method will be invoked and the listener automatically unregistered. If the trigger needs to be invoked again, it will have to be reregistered.

Once we no longer need the sensor to invoke our trigger, we can cancel the listener by passing both the listener and the sensor to the CancelTriggerSensor() method on the sensor manager.

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

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