Get Heart Rate From Microsoft Band

5

I am trying to get the heart rate from a Microsoft Band. It should be updating whenever the value changes. I am then trying to display that value in a TextBlock. I first create an instance of IBandClient, and set its HeartRate.ReadingChanged method like this:

bandClient.SensorManager.HeartRate.ReadingChanged += HeartRate_ReadingChanged;

Then I try to update the value like this:

private void HeartRate_ReadingChanged(object sender, Microsoft.Band.Sensors.BandSensorReadingEventArgs<Microsoft.Band.Sensors.IBandHeartRateReading> e)
{
    HeartRate = e.SensorReading.HeartRate;
}

HeartRate is an int set like so:

public int HeartRate
{
    get { return (int)GetValue(HeartRateProperty); }
    set { SetValue(HeartRateProperty, value); }
}

// Using a DependencyProperty as the backing store for HeartRate.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeartRateProperty =
    DependencyProperty.Register("HeartRate", typeof(int), typeof(MainPage), new PropertyMetadata(0));

The TextBlock text is then bound to HeartRate. However, I keep getting this error when trying to set HeartRate:

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

My guess is that it's trying to set HeartRate while it is still being set from the call before.

c#
windows-phone-8.1
microsoft-band
asked on Stack Overflow Jul 4, 2015 by (unknown user) • edited Nov 12, 2015 by (unknown user)

1 Answer

3

Try to implement this and see how it goes, if you still want your int variable, then convert it back to string when displaying it in a text-block.

await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
               () => 
               {
                   Textblock.Text =  e.SensorReading.HeartRate.ToString())
               }).AsTask();
answered on Stack Overflow Jul 4, 2015 by Eugen Sunic • edited Jul 24, 2015 by Eugen Sunic

User contributions licensed under CC BY-SA 3.0