Developing For Microsoft Band With WinRT - Getting Connected

In this article, I'm going to cover the basics of getting your WinRT (Windows Store and Windows Phone) apps set-up with the Microsoft Band SDK and getting you connected to your device.

Microsoft Band SDK

Since the package is currently a Nuget pre-release, you'll need to manually add the package using the Package Manager Console. You'll then need to paste in the following line into the console to install it to your project.

  1. Install-Package Microsoft.Band -Pre   
Doing this will get you set-up and ready to start developing for the Microsoft Band as is expected. One thing it does for you that you'll need to understand is if you don't install the package using Nuget then it will add a DeviceCapability into your Package.appxmanifest file that enables the Bluetooth capabilities for the Band that is as follows:

 

  1. <DeviceCapability Name="bluetooth.rfcomm" xmlns="http://schemas.microsoft.com/appx/2013/manifest">    
  2.   <Device Id="any">    
  3.     <!-- Used by the Microsoft Band SDK Preview -->    
  4.     <Function Type="serviceId:A502CA9A-2BA5-413C-A4E0-13804E47B38F" />    
  5.     <!-- Used by the Microsoft Band SDK Preview -->    
  6.     <Function Type="serviceId:C742E1A2-6320-5ABC-9643-D206C677E580" />    
  7.   </Device>    
  8. </DeviceCapability>    
capabilities

If you need to manually add this, you can do it by right-clicking your manifest file and opening it with the XML text editor. However, there is one manual step you do need to do to get going and that is to add the Proximity capability to your manifest. You'll find that under the Capabilities tab if you open the file using the default editor.

Getting Connected

Before we connect the app to the Band, you'll first need to pair the device with your phone (that you'll probably have been done if you're using the Band on a day-to-day basis).

To get yourself going, you'll want to add Microsoft.Band using a directive to your class. From there you can using the BandClientManager to get a list of the paired bands with the phone the app will be running on. You'll do this as follows:
  1. var bandInfo = (await BandClientManager.Instance.GetBandsAsync()).FirstOrDefault();  
You'll most likely have only one Band connected to your device for calling FirstOrDefault from the System.Linq namespace that will return the first device in the array that is returned from the GetBandsAsync method. If no device is connected, it will return null that you'll need to handle in your application.

Now that you hold a reference to your Band, you'll want to get yourself connected. Doing that is as simple as using the BandClientManager. You'll do that as follows:
  1. try    
  2. {    
  3.     using (var bandClient = await BandClientManager.Instance.ConnectAsync(bandInfo))    
  4.     {    
  5.             
  6.     }    
  7. }    
  8. catch (BandException be)    
  9. {    
  10.         
  11. }  
While it is possible that your device is connected to your phone, there will be occasions when the user is not with their mobile device and the Band becomes disconnected. You'll need to handle that in your application as shown above.

Now you've got yourself connected with your Band. I'll be writing up further blogs that will cover everything from creating your app tile on the Band to taking advantages of the Band's sensor to create applications as I work using updating my PhysiHealth application to use the new SDK.


Similar Articles