Devices/Sensors API
This section provides reference documentation for the Windows Universal Platform (UWP) APIs related to device sensors. UWP apps can leverage a wide range of device sensors to gather environmental data, track user motion, and enhance user experiences.
Core Concepts
UWP sensor APIs are designed to provide a consistent and simplified interface for accessing hardware sensors. Key concepts include:
- Sensor Types: A variety of sensor types are supported, such as accelerometers, gyroscopes, magnetometers, barometers, light sensors, and more.
- Data Access: Apps can read sensor data asynchronously, typically receiving readings at a specified report interval.
- Permissions: Access to certain sensors requires user consent, managed through app manifest declarations.
- Sensor States: APIs allow checking sensor availability, reading capabilities, and managing sensor power states.
Key Classes and Interfaces
The primary namespace for sensor interactions is Windows.Devices.Sensors. Here are some of the most commonly used classes:
| Class/Interface | Description | Common Usage |
|---|---|---|
Windows.Devices.Sensors.Accelerometer |
Represents the device's accelerometer, which measures acceleration forces along three axes. | Detecting device orientation, motion, and impacts. |
Windows.Devices.Sensors.Compass |
Represents the device's compass, providing magnetic heading information. | Determining the device's orientation relative to magnetic north. |
Windows.Devices.Sensors.Gyrometer |
Represents the device's gyrometer, measuring angular velocity around three axes. | Tracking device rotation and detecting subtle movements. |
Windows.Devices.Sensors.LightSensor |
Represents the ambient light sensor, measuring the intensity of light. | Adjusting screen brightness automatically or adapting app UI. |
Windows.Devices.Sensors.Magnetometer |
Represents the device's magnetometer, measuring magnetic field strength along three axes. | Assisting in compass functionality and detecting magnetic interference. |
Windows.Devices.Sensors.Barometer |
Represents the device's barometer, measuring atmospheric pressure. | Estimating altitude changes and weather conditions. |
Windows.Devices.Sensors.SensorDataThreshold |
Represents a threshold for data reporting, allowing sensors to trigger events only when readings change significantly. | Optimizing battery usage by reducing frequent data polling. |
Windows.Devices.Sensors.SensorReading |
Base class for sensor readings, containing timestamp and accuracy information. | Abstract representation of sensor data. |
Windows.Devices.Sensors.ISensorData |
Interface implemented by sensor readings that provide specific data points. | Contract for accessing sensor-specific data. |
Getting Started with Sensors
Here's a simplified example of how to access accelerometer data:
using Windows.Devices.Sensors;
using System;
public sealed partial class MainPage : Page
{
private Accelerometer _accelerometer;
private uint _desiredReportInterval;
public MainPage()
{
this.InitializeComponent();
InitializeSensor();
}
private void InitializeSensor()
{
_accelerometer = Accelerometer.GetDefault();
if (_accelerometer != null)
{
// Set the desired report interval to the fastest supported by the sensor
_desiredReportInterval = _accelerometer.MinimumReportInterval;
_accelerometer.ReportInterval = _desiredReportInterval;
// Subscribe to the ReadingChanged event
_accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
// Enable sensor reporting
_accelerometer.ReportLatency = 0; // Get readings as soon as they are available
}
else
{
// Handle case where no accelerometer is found
System.Diagnostics.Debug.WriteLine("No accelerometer found.");
}
}
private void Accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
{
// Get the latest reading
AccelerometerReading reading = args.Reading;
// Update UI on the UI thread
var dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
if (dispatcher.HasThreadAccess)
{
UpdateSensorReadings(reading);
}
else
{
dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => UpdateSensorReadings(reading));
}
}
private void UpdateSensorReadings(AccelerometerReading reading)
{
// Display X, Y, and Z values
XValueTextBlock.Text = $"X: {reading.AccelerationX:F2}";
YValueTextBlock.Text = $"Y: {reading.AccelerationY:F2}";
ZValueTextBlock.Text = $"Z: {reading.AccelerationZ:F2}";
}
// Remember to unsubscribe from events and potentially disable sensors when the page is unloaded
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (_accelerometer != null)
{
_accelerometer.ReadingChanged -= Accelerometer_ReadingChanged;
}
base.OnNavigatedFrom(e);
}
}