MSDN Community

Universal Windows Platform (UWP) Devices API

The UWP Devices API provides a comprehensive set of interfaces and classes for interacting with various hardware devices on Windows. This allows developers to build rich applications that can leverage the full capabilities of the underlying hardware, from sensors and cameras to specialized peripherals.

Overview

The Devices API is designed to offer a consistent and abstracted way to access hardware, regardless of the specific device model or manufacturer. It supports a wide range of device types, including:

This abstraction layer simplifies development by providing common patterns and event-driven models for device communication.

Key Namespaces and Classes

The core functionality for device interaction is primarily found within the following namespaces:

Commonly Used Classes:

Class Description Namespace
DeviceInformation Represents a device discovered by the system. Windows.Devices.Enumeration
Geolocator Gets the current location of the device. Windows.Devices.Geolocation
CameraCaptureUI Provides a UI for capturing photos and videos. Windows.Media.Capture
BluetoothLEDevice Represents a Bluetooth Low Energy device. Windows.Devices.Bluetooth

Discovering Devices

You can enumerate and discover devices connected to the system using the Windows.Devices.Enumeration namespace. This is often the first step before interacting with a specific device.

C# Example: Discovering all available devices


using Windows.Devices.Enumeration;
using System;
using System.Threading.Tasks;

public sealed class DeviceDiscovery
{
    public async Task FindDevicesAsync()
    {
        // Find all AQS Filter Examples:
        // "System.Devices.InterfaceClassGuid:=""{EFC87A0E-75DF-4F1E-B327-8765FE183493}"""  // Camera
        // "System.Devices.InterfaceClassGuid:=""{65E8773D-8F56-11D0-A3B9-00A0C922E6CF}"""  // Bluetooth
        // "System.Devices.InterfaceClassGuid:=""{8895B19B-CF97-409A-9273-6897E0620739}"""  // USB
        // Find all devices that have a Windows.Foundation.IStringable interface.
        string advancedQuerySyntaxString = "System.Devices.InterfaceClassGuid:<>''";
        var resultSelector = DeviceInformation.FindAllAsync(advancedQuerySyntaxString);
        DeviceInformationCollection devices = await resultSelector;

        foreach (DeviceInformation di in devices)
        {
            Console.WriteLine($"Device Name: {di.Name}, ID: {di.Id}");
        }
    }
}
                

Interacting with Sensors

Accessing sensor data, like accelerometer or GPS, is straightforward using the Windows.Devices.Sensors namespace. You typically request access to a specific sensor and then subscribe to its data-reporting events.

C# Example: Getting accelerometer readings


using Windows.Devices.Sensors;
using System;

public sealed class AccelerometerReader
{
    private Accelerometer _accelerometer;
    private uint _readingBuffer = 1; // Read every 1 reading

    public AccelerometerReader()
    {
        _accelerometer = Accelerometer.GetDefault();
        if (_accelerometer != null)
        {
            // Set the report interval to be as fast as possible
            _accelerometer.ReportInterval = _accelerometer.MinimumReportInterval >= 16 ? _accelerometer.MinimumReportInterval : 16;
        }
    }

    public void StartReading()
    {
        if (_accelerometer != null)
        {
            _accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
        }
    }

    public void StopReading()
    {
        if (_accelerometer != null)
        {
            _accelerometer.ReadingChanged -= Accelerometer_ReadingChanged;
        }
    }

    private void Accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
    {
        var reading = args.Reading;
        // Process reading.AccelerationX, reading.AccelerationY, reading.AccelerationZ
        Console.WriteLine($"X: {reading.AccelerationX:F2}, Y: {reading.AccelerationY:F2}, Z: {reading.AccelerationZ:F2}");
    }
}
                

Device Permissions

Access to certain devices (like cameras, location, or microphone) requires explicit user consent. Ensure you handle the necessary capability declarations in your app's manifest and gracefully manage permission requests in your code.

Resources