Get Your Hand Action From Microsoft Band Using Accelerometer Sensor

This article shows how to get the heart beat rate from Microsoft Band in Windows Phone.

Create a new Windows Phone project and add the Microsoft Band SDK and select Manage NuGet Packages as in the following screen:

Manage NuGet Packages

Now search for and install the Microsoft Band SDK as in the following image:

install Microsoft Band SDK

After installing the Microsoft SDK, ensure that the Proximity capability has been added to the manifest file as in the following image:

Proximity

Enable Proximity

Add the following code to the Package.appmanifest file. Right-click the appmainfest file and select View Code to add the code.

  1. <DeviceCapability Name="bluetooth.rfcomm" xmlns="http://schemas.microsoft.com/appx/2013/manifest">  
  2.    <Device Id="any">  
  3.       <Function Type="serviceId:A502CA9A-2BA5-413C-A4E0-13804E47B38F" />  
  4.       <Function Type="serviceId:C742E1A2-6320-5ABC-9643-D206C677E580" />  
  5.    </Device>  
  6. </DeviceCapability>  
Design the page as shown in the following image.

Design the page

The first step is to connect to the Band. To make a connection, the band and the device must be paired with Bluetooth to each other. To get the paired Band and establish a connection use the Band Client Manager. 
  1. Add the namespace.
    1. using Microsoft.Band;  
    2. using Microsoft.Band.Sensors;  
  2. Get the list of paired Bands using the following code.
    1. IBandInfo[] getPairedDevice=await BandClientManager.Instance.GetBandsAsync();  
  3. Connect to the band.
    1. using(IBandClient client=await BandClientManager.Instance.ConnectAsync(getPairedDevice[0]))  
    2. {  
    3.   
    4. }  
  4. Ensure the user has consented before getting the heart beat rate from the Band.
    1. if (client.SensorManager.Accelerometer.GetCurrentUserConsent() != UserConsent.Granted)  
    2. {   
    3. }  
  5. Start the sensor to read the device axis.
    1. await client.SensorManager.Accelerometer.StartReadingsAsync();  
  6. Create an event handler to read each and every change of the Band axis.
    1. client.SensorManager.Accelerometer.ReadingChanged += Accelerometer_ReadingChanged;  
    2. void Accelerometer_ReadingChanged(object sender, BandSensorReadingEventArgs<IBandAccelerometerReading> e)  
    3. {  
    4.   
    5. }  
  7. Write the following code to change the hand image axis.
    1. void Accelerometer_ReadingChanged(object sender, BandSensorReadingEventArgs < IBandAccelerometerReading > e)   
    2. {  
    3.     var xAxis = e.SensorReading.AccelerationX;  
    4.     var yAxis = e.SensorReading.AccelerationY;  
    5.     var zAxis = e.SensorReading.AccelerationZ;  
    6.     var actionRotation = yAxis * 90;  
    7.     this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () = > {  
    8.         myHand.Projection.SetValue(PlaneProjection.RotationYProperty, actionRotation);  
    9.     });  
    10. }  

Now start the Windows Phone app and connect with your Microsoft band and click the Start button to get the band action.


Similar Articles