Skip to content

Device Sensors Notification Plugin for Xamarin, Tizen and Windows

License

Notifications You must be signed in to change notification settings

CrossGeeks/DeviceSensorsPlugin

Repository files navigation

DeviceSensors Plugin for Xamarin iOS, Android, UWP and Tizen

Simple cross platform plugin to access device sensors.

Features

  • Access to accelerometer
  • Access to gyroscope
  • Access to magnetometer
  • Access to barometer
  • Access to pedometer

Setup

Platform Support

Platform Version
Xamarin.iOS iOS 7+
Xamarin.Android API 13+
Windows 10 UWP 10+
Tizen 4.0+

API Usage

Call CrossDeviceSensors.Current from any project or PCL to gain access to APIs.

iOS Setup

Add to your Info.plist NSMotionUsageDescription key:

<key>NSMotionUsageDescription</key>
<string>This app needs to be able to access your motion use</string>

Getting Started

Device Sensor interface

public interface IDeviceSensor<T>
    {
        /// <summary>
        /// Check if sensor supported
        /// </summary>
        bool IsSupported { get; }
        /// <summary>
        /// Check if sensor is active
        /// </summary>
        bool IsActive { get; }
        /// <summary>
        /// Get latest sensor reading
        /// </summary>
        T LastReading {get;}
        /// <summary>
        /// Sets/get sensor report interval
        /// </summary>
        int ReadingInterval { get; set; }
        /// <summary>
        /// Starts sensor reading
        /// </summary>
        void StartReading(int readingInterval = -1);
        /// <summary>
        /// Stops sensor reading
        /// </summary>
        void StopReading();

        /// <summary>
        /// Sensor reading changes event
        /// </summary>
        event EventHandler<DeviceSensorReadingEventArgs<T>> OnReadingChanged;
    }

Available sensors

    public interface IDeviceSensors
    {
       IDeviceSensor<VectorReading> Accelerometer {get; }
       IDeviceSensor<VectorReading> Gyroscope { get; }
       IDeviceSensor<VectorReading> Magnetometer { get; }
       IDeviceSensor<double> Barometer { get; }
       IDeviceSensor<int> Pedometer { get; }
    }

Usage sample:

Accelerometer

    if(CrossDeviceSensors.Current.Accelerometer.IsSupported)
    {
         CrossDeviceSensors.Current.Accelerometer.OnReadingChanged += (s,a)=>{

         };
         CrossDeviceSensors.Current.Accelerometer.StartReading();
    }
   

Gyroscope

    if(CrossDeviceSensors.Current.Gyroscope.IsSupported)
    {
         CrossDeviceSensors.Current.Gyroscope.OnReadingChanged += (s,a)=>{

         };
         CrossDeviceSensors.Current.Gyroscope.StartReading();
    }
   

Magnetometer

    if(CrossDeviceSensors.Current.Magnetometer.IsSupported)
    {
         CrossDeviceSensors.Current.Magnetometer.OnReadingChanged += (s,a)=>{

         };
         CrossDeviceSensors.Current.Magnetometer.StartReading();
    }
   

Contributors