Last Updated on 2022-07-15 by James Croft
In my previous Microsoft Band development post, we looked at how to introduce background execution. Here you’ll learn how to keep that connection open indefinitely using a DeviceUseTrigger background execution method.
Before I go into the explanation of how this works, you’ll want to check out my previous post first. Here we’ll assume that you’ve got a basic understanding of background execution and connecting with the Microsoft Band. To catch up, check out the posts below!
Background on background tasks
Most users developing applications that make use of the Microsoft Band’s sensors are looking to have them run in the background. This is especially important when you’re creating a fitness app for the likes of running or home exercise.
The problem with my previous solution is that it could only run for a short period before being canceled. This meant that the user would have some data from the Band but with gaps.
This post will cover a deeper look into background execution with Windows Phone and how you can use the Microsoft Band for a longer duration.
When creating background tasks on Windows Phone, there are a number of ways that can trigger it to run. In my previous example, I used the TimeTrigger which allowed the background to run every 15 minutes.
There must be a better way of keeping the Bluetooth connection open for as long as I possibly can…
James after the first attempt
Having a look at other Bluetooth background task samples, I came across the DeviceUseTrigger. This allows access to sensors and peripheral devices in background tasks while suspended.
Using DeviceUseTrigger with the Microsoft Band
Setting up the DeviceUseTrigger is relatively the same as the set up for the TimeTrigger. When you use it, you request the use of the device you’re connecting to by passing through the device’s identifier. This is the ID that is associated with your Windows Phone.
I’ll break it down for you so you fully understand what’s going on in the code snippet.
When you work with background tasks, you need to request access to the services. That is what is happening with the first few lines in the snippet. If we’ve been granted access to background execution, we can set up our background task with the DeviceUseTrigger and get it registered with the BackgroundExecutionManager.
At this point, we need to actually get the device ID and get the DeviceUseTrigger to request access to it. Even though we have our background task set up and ready to go, this final step needs to be made in order for the background task to be started.
Note, once this is complete, if your Microsoft Band is connected to your Windows Phone, it will immediately start running the background task and keep it running for an indefinite period of time. This is usually until your phone needs the memory to do OS-based tasks or your Microsoft Band loses connection. You’ll need to handle this case in your app and background task so that you unregister it successfully.
If you’d like to take a look at a sample of how this works, you can head over to my GitHub repo and have a play with it. If you have any questions or find issues, please feel free to drop me a line!
View source on GitHubEnjoy developing with the Microsoft Band!
Discover more from James Croft
Subscribe to get the latest posts sent to your email.







Any plan to update this tutorial/github example for UWP?