Microsoft Band

In this post, I’m going to cover the basics of getting your WinRT (Windows Store and Windows Phone) apps reading the sensors from your Microsoft Band with the Microsoft Band SDK. If you’d like to know how to get connected, read my previous post here.

If you’ve come here from my first post on getting connected, the last code block where we start a using statement for our Band connection can be taken out and instead just be replaced with an assignment to a property that we can use throughout the app. Once we have the connection, we will be able to access all of the features from the connected band. So now we will have something more on the lines of this:


        public IBandClient BandClient { get; set; }

        private async Task ConnectToBand()
        {
            if (this.BandClient == null)
            {
                var bandInfo = (await BandClientManager.Instance.GetBandsAsync()).FirstOrDefault();

                try
                {
                    this.BandClient = await BandClientManager.Instance.ConnectAsync(bandInfo);
                }
                catch (BandException)
                {
                    // The Microsoft Band failed due to either being too far for the Bluetooth to connect or it is turned off.
                }
            }
        }

        private void DisconnectFromBand()
        {
            if (this.BandClient != null)
            {
                this.BandClient.Dispose();
                this.BandClient = null;
            }
        }

These two methods you can use to connect and disconnect from your default connected Band of your Windows Phone or Windows PC. You’ll need to handle disconnecting so that you can release the connection from the Band with your app when your app suspends, as an example.

Accessing Sensors

There are a variety of sensors within your Band, some of which aren’t currently exposed by default for user viewing such as skin temperature. Here is a quick list of the sensors that you can attach to in order to read from them:

  • Accelerometer
  • Motion
  • Distance
  • Ultraviolet
  • Skin Temperature
  • Pedometer
  • Heart Rate
  • Contact

As you look through this list, most of them are pretty self explanatory. Accelerometer will give you X, Y, Z in acceleration. Motion give you the same functionality of the accelerometer with the added gyroscope readings for angular velocity in X, Y, Z. Distance gives readings of the total distance moved since the readings started along with the speed at which the wearer is moving, pace and the motion type. UV will give you ultraviolet index readings. Skin temperature will give you a double value for your skin temperature. Pedometer will give you the total number of steps the wearer has taken since your app started reading from the sensor. Heart rate will provide the wearer’s heart rate with a level of quality of the recording. Finally, Contact will let your app know if the wearer is actually wearing the Band or not.

Accessing these sensors in your app is relatively easy as the sensors themselves are exposed through the SensorManager of your IBandClient instance. Each sensor has the ability to be capped and only read based on an interval, although for some of the sensors, you’ll most likely be constantly reading. So let us dive into connecting to one of the sensors. The method for doing so is the same for all of the sensors.

First, we’ll need to create a model that we can feed the information from the sensors into. In this example, I’m going to show how we’ll work with the Motion sensor.


namespace MicrosoftBand.Models.Sensors
{
    public class Motion
    {
        public double AccelX { get; set; }
        public double AccelY { get; set; }
        public double AccelZ { get; set; }
        public double GyroX { get; set; }
        public double GyroY { get; set; }
        public double GyroZ { get; set; }
    }
}

You’ll then implement the functionality to connect to the sensor and start reading from it as follows:


        public Motion MotionModel { get; set; }

        private async Task StartMotion()
        {
            if (this.BandClient.SensorManager.Gyroscope.IsSupported)
            {
                this.MotionModel = new Motion();

                this.BandClient.SensorManager.Gyroscope.ReportingInterval = TimeSpan.FromMilliseconds(128.0);

                this.BandClient.SensorManager.Gyroscope.ReadingChanged += this.OnMotionReadingChanged;
                await this.BandClient.SensorManager.Gyroscope.StartReadingsAsync();
            }
        }

        private async Task StopMotion()
        {
            this.BandClient.SensorManager.Gyroscope.ReadingChanged -= this.OnMotionReadingChanged;
            await this.BandClient.SensorManager.Gyroscope.StopReadingsAsync();
        }

        private void OnMotionReadingChanged(object sender, BandSensorReadingEventArgs<IBandGyroscopeReading> e)
        {
            this.MotionModel.AccelX = e.SensorReading.AccelerationX;
            this.MotionModel.AccelY = e.SensorReading.AccelerationY;
            this.MotionModel.AccelZ = e.SensorReading.AccelerationZ;
            this.MotionModel.GyroX = e.SensorReading.AngularVelocityX;
            this.MotionModel.GyroY = e.SensorReading.AngularVelocityY;
            this.MotionModel.GyroZ = e.SensorReading.AngularVelocityZ;
        }

If you’re wanting to keep track of the times of these readings, your BandSensorReadingEventArgs exposes a property on the SensorReading which is a timestamp of the reading. This will be useful if you’re wanting to do any analytics on the information you’re receiving from the Band in your application.

Now you’ve got yourself connecting with your Microsoft Band from your WinRT app and receiving information from the sensors. If you need any help connecting to any of the other sensors of the Band, leave a comment below and I will be sure to help out!

Author James Croft

James is a senior software developer for Black Marble and Microsoft MVP in Windows Development with over 6 years of experience developing applications for the Windows platform. James’s passion for learning new technologies gives him the opportunities to expose them to the wider community through tutorials, lessons learned and best practices through his online blog site, personal YouTube channel, and local communities through talks at events, schools and universities.

More posts by James Croft

Join the discussion 5 Comments

  • pyrobri says:

    Hi James, so this is now running on my Band quite happily.
    I noticed that the band is always moving, even if it’s just sat on a table, but that’s another discussion..
    What I am wondering is now how I can use this data to update the UI. I am pretty new to phone, UWP developing so picking this stuff up slowly.
    My first attempt was just to update the UI directly which got me this error:
    ‘The application called an interface that was marshalled for a different thread’
    I have come across two ways of solving this and both work.
    The first is this bit of code is much simpler and involves using:
    Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>{})
    The second one also uses this but additionally makes use of a PropertyChangedModel (INotifyPropertyChanged)
    There is a lot of extra code here which seems to just do the same thing in the end.
    So I am wondering if the longer method is the ‘proper’ way of doing things and would maybe be easier to use down the line or if the first way is perfectly acceptable and I should just stick with that?

    Cheers

  • pyrobri says:

    Ah never mind I have found it.
    I put:
    Task t = BandModel.ConnectToBand();
    in MainPage()

  • pyrobri says:

    Hi James, I have connected my Band and am receiving accelerometer readings from it.
    But I can only seem to do this once I click a ‘start’ button in the app.
    How would I connect to the band automatically on app startup?

  • […] winrt , windows-store , windev , windows-phone , wpdev , james-croft Read original post at James Croft's BlogIn this post, I’m going to cover the basics of getting your WinRT (Windows Store and Windows […]

Leave a comment