Sensors API Reference

This section provides documentation for the Universal Windows Platform (UWP) APIs related to device sensors. These APIs allow your applications to access and utilize data from various hardware sensors such as accelerometers, gyroscopes, GPS, and more.

Overview

UWP sensor APIs provide a consistent and abstracted way to interact with hardware sensors. They abstract away the complexities of different sensor drivers and hardware implementations, offering a unified interface for developers.

Key Concepts

  • Sensor Types: UWP supports a wide range of sensor types, each providing specific types of data (e.g., motion, environmental, location).
  • Data Acquisition: Applications can read sensor data in real-time or retrieve historical readings, depending on the sensor and its capabilities.
  • Event-driven Updates: Many sensors provide data through events, allowing your application to react to changes as they happen.
  • Sensor Management: APIs for enumerating available sensors, checking their capabilities, and managing their state (e.g., starting, stopping).

Core Namespaces

The primary namespace for UWP sensor APIs is Windows.Devices.Sensors.

Common Sensor Classes

Here are some of the frequently used sensor classes:

Class Name Description
Accelerometer Provides access to the device's accelerometer, measuring linear acceleration along the X, Y, and Z axes.
Gyrometer Provides access to the device's gyrometer, measuring angular velocity around the X, Y, and Z axes.
Compass Provides access to the device's compass, determining the device's orientation relative to magnetic north.
Geolocator Provides access to the device's location services, such as GPS.
Inclinometer Provides access to the device's inclinometer, measuring the angle of inclination along the X, Y, and Z axes.
LightSensor Provides access to the ambient light sensor.
OrientationSensor Represents a sensor that provides orientation data.

Getting Sensor Data

The general pattern for using sensors involves:

  1. Getting an instance of the desired sensor.
  2. Setting up event handlers for data changes or reading data on demand.
  3. Handling sensor reading objects which contain the data.

Example: Reading Accelerometer Data

Here's a simplified C# example of how to read accelerometer data:


using Windows.Devices.Sensors;
using Windows.UI.Core;

public sealed partial class MainPage : Page
{
    private Accelerometer _accelerometer;
    private TypedEventHandler<Accelerometer, AccelerometerReadingEventArgs> _readingChangedEventToken;

    public MainPage()
    {
        this.InitializeComponent();
        InitializeAccelerometer();
    }

    private void InitializeAccelerometer()
    {
        _accelerometer = Accelerometer.GetDefault();
        if (_accelerometer != null)
        {
            // Subscribe to the ReadingChanged event
            _readingChangedEventToken =
                async (sender, args) => await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    var reading = args.Reading;
                    // Update UI with reading.AccelerationX, reading.AccelerationY, reading.AccelerationZ
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.AppendFormat("X: {0:0.00}", reading.AccelerationX);
                    sb.AppendLine();
                    sb.AppendFormat("Y: {0:0.00}", reading.AccelerationY);
                    sb.AppendLine();
                    sb.AppendFormat("Z: {0:0.00}", reading.AccelerationZ);
                    // YourTextBlock.Text = sb.ToString(); // Example UI update
                });
            _accelerometer.ReadingChanged += _readingChangedEventToken;
        }
        else
        {
            // Handle case where accelerometer is not available
            // YourTextBlock.Text = "Accelerometer not available.";
        }
    }

    // Remember to unsubscribe when the page is unloaded
    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        if (_accelerometer != null)
        {
            _accelerometer.ReadingChanged -= _readingChangedEventToken;
        }
        base.OnNavigatedFrom(e);
    }
}
                

Permissions

Applications that access sensor data, particularly location-based sensors like Geolocator, must declare the necessary capabilities in their app manifest (Package.appxmanifest).

  • For location sensors: ID_CAP_LOCATION

See Also