Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

Compass APIs for Windows Phone


In this post, we will discuss about Compass sensor support in Windows Phone. If you are a WPDev, you can integrate it in your apps and/or games. - Article authored by Kunal Chowdhury on .

Today in this blog post we will discuss about Compass APIs available in Windows Phone SDK. After reading this post, you will be able to use your Windows Phone’s device sensors and build some apps and/or games for your targeted users.

 

Before using the compass in your game, make sure to check whether the device supports it or not. The SDK provides APIs to check that too. Let’s start discussing about it.

 

The sealed class “Compass” is available in “Microsoft.Devices.Sensors” namespace and inherits the base class “SensorBase” of type CompassReading. It exposes static property named “IsSupported”, which will let you know whether the device on which the application is running supports the compass sensor. The base class methods Start() and Stop() starts and stops the acquisition of data from the sensor.

 

To use the compass sensor in your application, make sure to include ID_CAP_SENSORS capabilities in your WMAppManifest.xml file, failing which you will get a AccessViolationException.

 

In the next step, you have to add the DLL Reference of Microsoft.Devices.Sensors.dll into your project. To do this, right click on your Windows Phone project and click “Add Reference”. This will pop up the below “Reference Manager” dialog window where you will find the reference named “Microsoft.Devices.Sensors”. Add this to your project to enable you access the compass APIs.

 

Add Microsoft.Devices.Sensors.dll reference in project

 

Once you make sure that your user’s device supports Compass, create an instance of the Compass class and set the property named “TimeBetweenUpdates” to 1 second. The compass will then trigger after each second to get the current Magnetic Heading of the device.

 

The base class event “CurrentValueChanged” will allow you to track the change in magnetic heading. So, just subscribe to the event and when you are ready to track the compass value changes, call the base class method “Start()”. This will start acquiring the data from the sensor and provide you the magnetic heading in the CurrentValueChanged event.

 

Here is the full code snippet for your reference:

 

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (Compass.IsSupported)
{
var compass = new Compass { TimeBetweenUpdates = TimeSpan.FromSeconds(1) };
compass.CurrentValueChanged += Compass_CurrentValueChanged;
compass.Start();
}
}

 

private void Compass_CurrentValueChanged(object sender,
SensorReadingEventArgs<CompassReading> e)
{
Dispatcher.BeginInvoke(() => compassDetails.Text = "You are at: " +
e.SensorReading.MagneticHeading.ToString("0") + " °N");
}

 

Once you are done with building the app, you will be able to get the degree of magnetic heading with respect to north pole. Based on this value, you can show user the direction and/or a full pledged compass in the screen.

 

Here is a demo of what we build above:

 

Windows Phone Compass Demo            Windows Phone Compass Demo

 

I hope, this small post will give you heads up building a sensor specific app like compass and use the API properly in your app or game to show direction as per user’s movement.

 

Don’t forget to read my other articles listed in this blog. Drop a line below if this helped you understanding the compass API. Any queries? Don’t forget to drop your questions. I am also available on Twitter and Facebook. Do connect with me to get regular updates.

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.