Developing For Microsoft Band With WinRT - Sensors

In this article, I will 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 understand how to get connected, read my previous article here.

If you've come here from my first article 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:
  1. public IBandClient BandClient   
  2. {  
  3.     get;  
  4.     set;  
  5. }  
  6.   
  7. private async Task ConnectToBand()   
  8. {  
  9.     if (this.BandClient == null)   
  10.     {  
  11.         var bandInfo = (await BandClientManager.Instance.GetBandsAsync()).FirstOrDefault();  
  12.   
  13.         try   
  14.         {  
  15.             this.BandClient = await BandClientManager.Instance.ConnectAsync(bandInfo);  
  16.         }   
  17.         catch (BandException)   
  18.         {  
  19.             // The Microsoft Band failed due to either being too far for the Bluetooth to connect or it is turned off.      
  20.         }  
  21.     }  
  22. }  
  23.   
  24. private void DisconnectFromBand()   
  25. {  
  26.     if (this.BandClient != null)   
  27.     {  
  28.         this.BandClient.Dispose();  
  29.         this.BandClient = null;  
  30.     }  
  31. }  
You can use these two methods 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 provide X, Y and Z in acceleration. 
  • Motion provides the same functionality of the accelerometer with the added gyroscope readings for angular velocity in X, Y and 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 provide Ultraviolet index readings. 
  • Skin temperature will provide a double value for your skin temperature. 
  • Pedometer will provide the total number of steps the wearer has taken. 
  • Heart rate will provide the wearer's heart rate with a level of quality of the recording.
  • 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 since the sensors themselves are exposed using 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.
  1. namespace MicrosoftBand.Models.Sensors    
  2. {    
  3.    public class Motion    
  4.    {    
  5.        public double AccelX { getset; }    
  6.        public double AccelY { getset; }    
  7.        public double AccelZ { getset; }    
  8.        public double GyroX { getset; }    
  9.        public double GyroY { getset; }    
  10.        public double GyroZ { getset; }    
  11.    }    
  12. }   
You'll then implement the functionality to connect to the sensor and start reading from it as follows:
  1. public Motion MotionModel   
  2. {  
  3.     get;  
  4.     set;  
  5. }  
  6. private async Task StartMotion()   
  7. {  
  8.     if (this.BandClient.SensorManager.Gyroscope.IsSupported)   
  9.     {  
  10.         this.MotionModel = new Motion();  
  11.         this.BandClient.SensorManager.Gyroscope.ReportingInterval = TimeSpan.FromMilliseconds(128.0);  
  12.         this.BandClient.SensorManager.Gyroscope.ReadingChanged += this.OnMotionReadingChanged;  
  13.         await this.BandClient.SensorManager.Gyroscope.StartReadingsAsync();  
  14.     }  
  15. }  
  16.   
  17. private async Task StopMotion()   
  18. {  
  19.     this.BandClient.SensorManager.Gyroscope.ReadingChanged -= this.OnMotionReadingChanged;  
  20.     await this.BandClient.SensorManager.Gyroscope.StopReadingsAsync();  
  21. }  
  22.   
  23. private void OnMotionReadingChanged(object sender, BandSensorReadingEventArgs < IBandGyroscopeReading > e)   
  24. {  
  25.     this.MotionModel.AccelX = e.SensorReading.AccelerationX;  
  26.     this.MotionModel.AccelY = e.SensorReading.AccelerationY;  
  27.     this.MotionModel.AccelZ = e.SensorReading.AccelerationZ;  
  28.     this.MotionModel.GyroX = e.SensorReading.AngularVelocityX;  
  29.     this.MotionModel.GyroY = e.SensorReading.AngularVelocityY;  
  30.     this.MotionModel.GyroZ = e.SensorReading.AngularVelocityZ;  
  31. }  
If you intend to monitor the times of these readings, your BandSensorReadingEventArgs exposes a property on the SensorReading that is a timestamp of the reading. This will be useful if you are doing 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!

Up Next
    Ebook Download
    View all
    Learn
    View all