Last Updated on 2022-07-15 by James Croft

In this post, we’ll cover the basics of getting your Microsoft Band connected using a background task in a Windows Phone app.

If you’re new to working with the Microsoft Band SDK, check out my previous posts in this series. You’ll need some experience to understand what is covered and how you can extend it to fit your needs.

Setup the background task

To get started, you’ll need to add a new project to your solution which will be for your background task. You’ll need to create a new Windows Runtime Component, not a portable class library.

You will then need to create a sealed class within this project which inherits from the IBackgroundTask interface in the Windows.ApplicationModel.Background namespace.

You’ll then have to add the interfaces Run method. As it says on the method name, this runs when the background task is executing.

It will provide you with a parameter that you can access a deferral from. You’ll need this if you’re going to be using your background task to access the data from the sensors.

The basic layout for this Run method will look like this:

As you can see, we’re getting the deferral from the task instance and keeping hold of it for later. This will keep our background task alive and not drop out before we’ve done what we need it to do.

Getting data from the Microsoft Band

Microsoft Band Sensors
Microsoft Band Sensors

From my previous posts, you should know how to get yourself connected with the Microsoft Band. Now I’ll show you how to get the data in a background task.

If you want to access the heart rate sensor of the Band, you’ll need to get the user’s consent first. This is done before the background task registers to the phone as this is a new requirement in the SDK.

The best way to do this is to connect to the device using the following method when your app loads.

If you’ve not implemented the DisposableAction class, you can get it here to add to your solution – DisposableAction.cs. You’ll be using it in the background task, so it’s worth having.

The above code will bring a dialog up in your application that your user will need to accept. If they refuse to do so, you can call this method again the next time the user enters your application. This will remind them that they need to do so if they want to use the sensors in your app.

Retrieving the data

Now let’s get the Band data in your background class. We’ll create a new asynchronous private method called GetBandData which will setup the Band connection and register to listen for events fired by the Band’s sensors. I’m going to provide you with the setup code but if you’d like to understand what you’re doing, please go back and read my previous posts.

As you can see in this method, we’re making a connection to the Band client and keeping a reference to it so we can remember to dispose of it when we’ve done. If you let it dispose in this method, you’ll lose the events that have been attached to the sensors and won’t ever receive the readings.

We’re creating 3 methods to attach to our sensors and these will look something as follows:

We’ve now connected to the Microsoft Band, got the sensor data and have got to the end of our background task but the last thing we need to do is disconnect from the Band if we’re connected and complete the deferral. As you’ve probably noticed throughout the code snippets, I’ve purposely left areas that have notes to complete the deferral. In place of all of these comments, we can create a method that does the disconnection and completion of the deferral. The reason we need to disconnect from the Band is that if we don’t, the background task will throw an exception when it runs again.

We’ve now come full circle and our background task is complete. The only thing left to do is now register it within the application to execute.

Registering the Microsoft Band background task

The first thing you’ll want to do to get set up with the registration process is to reference your background project from your Windows Phone or Windows project.

Once you’ve done that, you’ll need to make a change to your app’s manifest file so that it supports the background task. Depending on how you’re wanting to trigger this, you’ll need to choose the correct trigger from the app manifest. In this tutorial, I’ll show you a time based one that runs every 15 minutes.

This is how your app manifest should look:

Package Manifest for Band Background Task
Package Manifest for Band Background Task

Now, after your call in the first page of your app that asks for access to the Band’s sensors, you’ll want to register your background task and here is how you do that:

This background task will register as a time based trigger task which will run every 15 minutes.

You can now sit back and let your background task do all the work for your application!

If you have any questions, please feel free to leave them below and I will do my best to answer them.


Discover more from James Croft

Subscribe to get the latest posts sent to your email.

James Croft

James is a senior software engineer at Microsoft with over 10 years experience designing and building large-scale, distributed, cloud-native systems. He's deeply experienced in C#, Python, and TypeScript, with specialization in AI agent architectures, retrieval-augmented generation (RAG), and production-grade Azure systems.

2 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.