Simple cross platform plugin to access device sensors.
- Access to accelerometer
- Access to gyroscope
- Access to magnetometer
- Access to barometer
- Access to pedometer
- Available on NuGet: http://www.nuget.org/packages/Plugin.DeviceSensors
- Install into your PCL project and Client projects.
Platform Support
Platform | Version |
---|---|
Xamarin.iOS | iOS 7+ |
Xamarin.Android | API 13+ |
Windows 10 UWP | 10+ |
Tizen | 4.0+ |
Call CrossDeviceSensors.Current from any project or PCL to gain access to APIs.
Add to your Info.plist NSMotionUsageDescription key:
<key>NSMotionUsageDescription</key>
<string>This app needs to be able to access your motion use</string>
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();
}