UWP Compass Sensor

Reference documentation for the UWP Compass Sensor.

Overview

The Compass sensor provides access to the magnetic field sensor on a device. This sensor can be used to determine the device's orientation relative to the Earth's magnetic field. The Compass sensor is a core component of UWP applications that require orientation detection.

Properties

The Compass Sensor exposes several properties for configuration and status monitoring.

Methods

The Compass Sensor provides methods for starting and stopping sampling, and for configuring the sample rate.

Code Samples

Here are a few example code snippets demonstrating the usage of the Compass Sensor.

Example 1: Getting the Heading

                    
                        // Assuming you have a CompassSensor object named compassSensor
                        if (compassSensor != null && compassSensor.IsSamplingRequested == false)
                        {
                            compassSensor.StartSampling();
                            while (compassSensor.IsSamplingRequested)
                            {
                                double heading = compassSensor.Heading;
                                Console.WriteLine("Heading: " + heading);
                            }
                            compassSensor.StopSampling();
                        }