Skip to content

Instantly share code, notes, and snippets.

@mattleibow
Last active August 29, 2015 14:16
Show Gist options
  • Save mattleibow/33d73a7bdc9e970a2ff1 to your computer and use it in GitHub Desktop.
Save mattleibow/33d73a7bdc9e970a2ff1 to your computer and use it in GitHub Desktop.
MSBand

Introducing the Microsoft Band SDK Preview for Xamarin.Android

Microsoft finally released an SDK for the Microsoft Band! The Band is very much designed for health and fitness uses with many sensors. But, it is certainly not limited to health uses. Because we have access to the sensors and can interact with the device, we can use the Band to enhance and extend your app. Working with Band essentially means communicating with the Band from the app installed on the device.

There are 4 main areas that we can interact with the Band:

  1. Reading information from the Sensors
  2. Creating themed app Tiles
  3. Sending Messages to the Band
  4. Theming and Personalizing the Band

Getting Started

Before we can connect to a Band, we need to make sure that the latest Microsoft Health app is installed on the Android device and the Band updated with the latest firmware. The minimum supported Android version is 4.2, or level 17.

In order to communicate with the Band, we need two permissions:

[assembly: UsesPermission(Android.Manifest.Permission.Bluetooth)]
[assembly: UsesPermission(Microsoft.Band.BandClientManager.BindBandService)]

Now that we have the Band, the device and the Xamarin.Android project set up, we can start connecting to the Band. Before we connect to the Band, we can get a list of all the Bands paired with the device and we can pick one to connect to.

Working with Sensors

The main feature of the Band is the many sensors. We can pick from the Accelerometer, Contact, Distance, Gyroscope, Heart Rate, Pedometer, Skin Temperature and Ultraviolet (UV) sensors.

Connecting to the sensors involves only a few steps, so first, we want to get hold of a sensor:

var accelerometer = client.SensorManager.CreateAccelerometerSensor();

Then we want to attach an event handler to the sensor so that we can be notified of updates. This is called from another thread and we need to be able to handle a high volume of events:

accelerometer.ReadingChanged += (sender, args) => {
    var yReading = args.SensorReading.AccelerationY;
    RunOnUiThread(() => {
        label.Text = yReading;
    });
};

Finally, we start listening to the sensor. Some sensors (namely the accelerometer and gyroscope) require a listening interval, or sample rate, which could be 16ms, 32ms or 128ms:

await accelerometer.StartReadingsTaskAsync(SampleRate.Ms16);

When we want to stop listening, all we have to to is call one method:

await accelerometer.StopReadingsTaskAsync();

Creating App Tiles

Tiles allow us to create app specific experiences on the Band. The Band supports up to 13 separate tiles, and will allow us to create as many tiles as there is space for.

If we want to find out how many tiles we can still create, we can use the TileManager:

var capacity = await client.TileManager.GetRemainingTileCapacityTaskAsync();

The Band does not support color icons, but only white, alpha blended icons. Also, each tile icon must be be 46x46 pixels. Because the Band does not directly support the native Android bitmap, we use the BandIcon type:

var options = new BitmapFactory.Options();
options.InScaled = false;
var icon = await BitmapFactory.DecodeResourceAsync(Resources, Resource.Raw.tile, options)
var tileIcon = new BandIcon(icon);

If we want to create a new tile, we use the BandTile.Builder:

var uuid = Java.Util.UUID.RandomUUID();
var tileName = "My Tile";
var tile = new BandTile.Builder(uuid, tileName, tileIcon).Build();

We can add and remove tiles using the TileManager property:

// add a tile
var success = await client.TileManager.AddTileTaskAsync(this, tile);
// remove the tile
var success = await client.TileManager.RemoveTileTaskAsync(tile);

If we want to get a list of all the tiles on the device for our app, we can query the tile manager

var tiles = await client.TileManager.GetTilesTaskAsync();

Sending Messages

We can display a notification under a tile using the NotificationManager property. There are three types of notifications: dialogs, messages and various vibrations. Each dialog consists of a tile ID, a title and a body. Messages have an additional time stamp.

To display a dialog, we need the tile ID, the title and a body:

var title = "My Message";
var body = "My explanation for the message.";
await client.NotificationManager.ShowDialogTaskAsync(uuid, title, body);

Displaying messages is very similar, except there are a few extra parameters:

var date = DateTime.Now;
await client.NotificationManager.SendMessageTaskAsync(uuid, title, body, date);

Learn More

More information can be found on the official Microsoft Band for Developers page and on my website

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment