First steps with Xamarin and the Microsoft Band SDK

Last Saturday my Microsoft Band 2 was finally delievered to me, I preordered it at Amazon.co.uk on the time it was presented in NY. Till today I do not see a way to get in Austria on another way, very unfortunate – it’s a really nice piece of tech.

Normally one would think the first think to do with it, is to go out for run, but wait – there is a Band SDK out for Xamarin already.
So I finished up the hole weekend to mess around with it and on my second weekend I thought about wrtiting a few things down, so here we go with the first part.

I want to start with a quick Hello World (Surprise!) Android app.

Prerequisites

Before we can start with the code, we have to get a few things frist.

Setup

Create a new Project

Xamarim Microsoft Band First Steps New Proect

and add the downloaded *.DLLs to the references.

Xamarim Microsoft Band Add DLLs

Next we want to make sure that we allow our application to use bluetooth and also the permission to use the Microsoft Band service. For this purpose add the following lines to the AssemblyInfo.cs (Project -> Properties in VS Solution Explorer)

[assembly: UsesPermission(Android.Manifest.Permission.Bluetooth)]
[assembly: UsesPermission(Microsoft.Band.BandClientManager.BindBandService)]
view raw assembly hosted with ❤ by GitHub

Connect to the Band

To connect to the Band create a new class with the code below or use the code inside the PairBand() method.

public class Band {
private static Band BandDevice;
public BandClient BandClient { get; set; }
public String Name { get; set; }
private Band() {
}
private async void PairBand() {
var bandClientManager = BandClientManager.Instance;
var bandsFound = await bandClientManager.GetPairedBandsAsync();
if (bandsFound != null && bandsFound.Count() == 1)
{
var bandFound = bandsFound.FirstOrDefault();
BandDevice.BandClient = await bandClientManager.ConnectAsync(bandFound);
BandDevice.Name = bandFound.Name;
}
}
public static Band Instance {
get {
if (BandDevice == null)
{
BandDevice = new Band();
}
return BandDevice;
}
}
}
view raw Band hosted with ❤ by GitHub

We get the BandClientManager instance here, call for all connected Bands and (assuming you have only one Band) connect to the first one we have found.

Next we want to change the MainActivity.cs a little bit, so we can actually see if we are connected to the Band. For this purpose we will modify the button click event.
If we click on the button in our app later, it will show the name of the connected device.

[Activity(Label = "Hello World Band Android", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity {
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<button>(Resource.Id.MyButton);
SetupBand();
button.Click += delegate
{
if (myBand != null && myBand.BandClient.IsConnected)
button.Text = string.Format("Connected to {0}!", myBand.Name);
else
Toast.MakeText(this, "Band not yet connected or not found!", ToastLength.Long);
};
}
private async void SetupBand() {
// get instance
myBand = Band.Instance;
await myBand.PairBand();
}
}
view raw MainActivity hosted with ❤ by GitHub

Result

Note: The Android emulator does not support Bluetooth, so you will get an exception when execute your project. Instead of using the emulator you should plug in your phone.
If everything worked out, you should see something like this on your device:
Xamarim Microsoft Band Android App Xamarim Microsoft Band Android App

The complete project can be found on Github – Complete Xamarin Band Android Hello World Projectas well.

You may also like...