Reading your skin temperature using the Microsoft Band SDK Preview

Posted Tuesday, March 31, 2015 12:29 PM by CoreyRoth

Microsoft Band is packed with sensors that let it track your workouts.  However, one sensor you don’t hear about often is the Skin Temperature sensor.  This sensor allows you to read the temperature in Celsius of your skin where your Band is.  There aren’t any apps built-in to Microsoft Band that make use of it directly.  However, as a developer you can take readings from that sensor from an app on your phone using the Microsoft Band SDK Preview.  The SDK is very preliminary and you can only do a handful of functions from a phone including reading the sensors, creating tiles and sending notifications, and changing the theme and background image.  It has a direct dependency on the app you deploy to your phone.  You can’t actually build an app and deploy it so that it runs on Microsoft Band natively.  This limits you quite a bit, but you can still do a few cool things.

For my example, I worked off the Sensors example project for Windows Phone 8.1 and made some customizations.  The app consist of a simple button that initiates our connection to the Band and starts taking readings.  You can customize the Sensors project as well or you can grab my copy from GitHub.  You can get the SDK from nuget by using the following command.  This will add the necessary project references.

Install-Package Microsoft.Band -Pre

Start by editing the XML of the Package.appxmanifest file and add the DeviceCapability section for Microsoft Band inside the Capabilities element.

<DeviceCapability Name="bluetooth.rfcomm" xmlns="http://schemas.microsoft.com/appx/2013/manifest">

  <Device Id="any">

    <!-- Used by the Microsoft Band SDK Preview -->

    <Function Type="serviceId:A502CA9A-2BA5-413C-A4E0-13804E47B38F" />

    <!-- Used by the Microsoft Band SDK Preview -->

    <Function Type="serviceId:C742E1A2-6320-5ABC-9643-D206C677E580" />

  </Device>

</DeviceCapability>

You also need to add the Proximity capability as well but you can do that through the UI.

Now, we’ll work on adding some functionality to MainPage.xaml.  This consists mainly of a few TextBlocks and a Button.  Clicking the button will invoke our code to connect to Band and take a reading.  Create an event handling method for the button and mark it with async.  Now, we first need to get the paired Bands to the phone.  This assumes you have already paired your Band in the Bluetooth settings of the phone.

// Get the list of Microsoft Bands paired to the phone.

IBandInfo[] pairedBands = await BandClientManager.Instance.GetBandsAsync();

if (pairedBands.Length < 1)

{

    this.textBlock.Text = "Band not paired.";

    return;

}

To connect to Band we create an IBandClient in a using block.

using (IBandClient bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]))

{

}

Now we can read from the Skin Temperature sensor.  To do this, we bind an event handling method to the ReadingChanged event of the SkinTemperature class.  We’ll cover the event handling method just below.

bandClient.SensorManager.SkinTemperature.ReadingChanged += SkinTemperature_ReadingChanged;

Finally, we call StartReadingsAsync to start taking readings from Band.  We follow it with a delay of a minute before calling StopReadingsAsync to stop taking readings.

await bandClient.SensorManager.SkinTemperature.StartReadingsAsync();

await Task.Delay(TimeSpan.FromMinutes(1));

await bandClient.SensorManager.SkinTemperature.StopReadingsAsync();

In the event handling method, the value of e has a type of BandSensorReadingEventArgs<IBandSkinTemperatureReading>.  We can get the skin temperature in Celsius by using the SensorReading property.  For us Americans, we can convert that to Fahrenheit.  You know the formula right?  C * (9/5) + 32.  :).  Using the Dispatcher, we then write the value of the reading to the UI.

private async void SkinTemperature_ReadingChanged(object sender, BandSensorReadingEventArgs<IBandSkinTemperatureReading> e)

{

    IBandSkinTemperatureReading temperatureReading = e.SensorReading;

    string text = string.Format("Temperature: {0}C", temperatureReading.Temperature);

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.textBlock.Text = text; }).AsTask(); 

}

Now you are ready to run the program on your Windows Phone and see how hot you are!  Plug your Windows Phone into your PC via the USB port and then debug the program and be sure you choose Device in the options.  You can’t do this with the emulator.

BandSkinTemperature

There you have it, my skin temperature was 30.69 Celsius (87.42 Fahrenheit).  Now you might be thinking now, “ok, neat, but how do I see that information on my Band”.  Well, you can push that information to your Band using a Tile.  In fact, I’ve included that code in my repo on GitHub.  However, you can only push information to your Band when the Windows Phone app is running.  That’s not all that useful.  I’ll cover tiles in a future post, but if you want to try it out for yourself, you can grab my code from GitHub.  I would have included it today but I found that adding tiles so far doesn’t always work as expected.  Maybe it will for you.

This example really has zero practical value but I expect that we’ll see more opened up in the API in future versions.  If you have been interested in seeing what you can do with it as a developer, it’s worth checking out though.  For more examples of working with Band across platforms, be sure and check out the published documentation.

Comments

No Comments

Leave a Comment

(required)
(required)
(optional)
(required)