This article is Day #23 in a series called 31 Days of Windows 8. Each of the articles in this series will be published for both HTML5/JS and XAML/C#. You can find additional resources, downloads, and source code on our website.
Today, we’re going to talk about the compass. If you’ve done any Windows Phone development previously, you might have read my article on the Windows Phone compass, which provides magnetic and true north headings, as well as X, Y, and Z rotation data.
From what I have found so far, the compass in Windows 8 provides significantly less data from the sensor. In fact, we only get the values related to our heading, with no consideration for rotation data. In addition, you’re going to find that since we’re building apps for Windows 8, there’s going to be a wide variety of sensors we’ll encounter. Here’s a quick video of the 4 different machines that I use regularly, and how they each respond differently to the same code:
Writing Some Code to Access the Compass
The compass, like many of the sensors we will cover in the next few days, is actually very accessible and easy to use. With a few lines of code, and one event handler, we can gather rich data from our user’s device very quickly. Here’s my entire MainPage.xaml.cs file for my simple compass app:
using System;
using Windows.Devices.Sensors;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace Day23_Compass
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
Compass c;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
c = Compass.GetDefault();
if (c != null)
{
c.ReadingChanged += c_ReadingChanged;
Data.Visibility = Visibility.Visible;
}
else NoSensorMessage.Visibility = Visibility.Visible;
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
c.ReadingChanged -= c_ReadingChanged;
}
async void c_ReadingChanged(Compass sender, CompassReadingChangedEventArgs args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
MagneticNorth.Text = args.Reading.HeadingMagneticNorth.ToString();
if (args.Reading.HeadingTrueNorth != null)
{
TrueNorth.Text = args.Reading.HeadingTrueNorth.ToString();
}
TimeStamp.Text = args.Reading.Timestamp.ToString();
});
}
}
}
To explain the code above, in my OnNavigatedTo method, I try to initialize my Compass object, and if it’s not null, I create an event handler for each time that the reading of the compass changes. (Unless the user’s device is sitting completely still, it’s likely you’ll consistently get new readings.) In the ReadingChanged event handler, we use a Dispatcher to get back to the UI thread, where we then assign our three values, HeadingMagneticNorth, HeadingTrueNorth, and Timestamp to TextBlock values I’ve placed in my XAML file.
That’s it. As you probably saw, however, most of the sensors you’re going to encounter will not provide a HeadingTrueNorth value, so relying on the HeadingMagneticNorth is going to be a more reliable value for you.
Finally, both of the Heading values are measured in degrees relative to their associated heading. This means that you’ll see a range of values from 0 to 360, with zero being the value headed directly north.
Summary
This was a quick but exhaustive look at the Compass in Windows 8. We saw that you can easily access the Compass data by using an event handler, but that we receive a limited amount of data from this sensor, and your mileage will vary from device to device.
If you would like to download the sample app featured in this article, click the icon below:
Tomorrow, we’re going to look at the light sensor, and how we can manipulate our application to take advantage of knowing if the user is sitting in a light or dark room.
2 thoughts on “31 Days of Windows 8 | Day #23: The Compass”