public IBandClient BandClient { get; set; } private async Task ConnectToBand() { if (this.BandClient == null) { var bandInfo = (await BandClientManager.Instance.GetBandsAsync()).FirstOrDefault(); try { this.BandClient = await BandClientManager.Instance.ConnectAsync(bandInfo); } catch (BandException) { // The Microsoft Band failed due to either being too far for the Bluetooth to connect or it is turned off. } } } private void DisconnectFromBand() { if (this.BandClient != null) { this.BandClient.Dispose(); this.BandClient = null; } }
These two methods you can use 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 SensorsThere 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
Accessing these sensors in your app is relatively easy as the sensors themselves are exposed through 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.
namespace MicrosoftBand.Models.Sensors { public class Motion { public double AccelX { get; set; } public double AccelY { get; set; } public double AccelZ { get; set; } public double GyroX { get; set; } public double GyroY { get; set; } public double GyroZ { get; set; } } }
You’ll then implement the functionality to connect to the sensor and start reading from it as follows:
public Motion MotionModel { get; set; } private async Task StartMotion() { if (this.BandClient.SensorManager.Gyroscope.IsSupported) { this.MotionModel = new Motion(); this.BandClient.SensorManager.Gyroscope.ReportingInterval = TimeSpan.FromMilliseconds(128.0); this.BandClient.SensorManager.Gyroscope.ReadingChanged += this.OnMotionReadingChanged; await this.BandClient.SensorManager.Gyroscope.StartReadingsAsync(); } } private async Task StopMotion() { this.BandClient.SensorManager.Gyroscope.ReadingChanged -= this.OnMotionReadingChanged; await this.BandClient.SensorManager.Gyroscope.StopReadingsAsync(); } private void OnMotionReadingChanged(object sender, BandSensorReadingEventArgs<IBandGyroscopeReading> e) { this.MotionModel.AccelX = e.SensorReading.AccelerationX; this.MotionModel.AccelY = e.SensorReading.AccelerationY; this.MotionModel.AccelZ = e.SensorReading.AccelerationZ; this.MotionModel.GyroX = e.SensorReading.AngularVelocityX; this.MotionModel.GyroY = e.SensorReading.AngularVelocityY; this.MotionModel.GyroZ = e.SensorReading.AngularVelocityZ; }
If you’re wanting to keep track of the times of these readings, your BandSensorReadingEventArgs exposes a property on the SensorReading which is a timestamp of the reading. This will be useful if you’re wanting to do any analytics on the information you’re receiving from the Band in your application.
References: http://jamescroft.co.uk/blog/wearable/developing-for-microsoft-band-with-winrt-sensors/