Usando el sensor de latidos en Microsoft Band


Microsoft Band nos ofrece diversos sensores con el cual podemos acceder mediante el SDK que nos ofrece Microsoft.

Esta aplicación es bastante sencilla y dejo todo el código en C# comentado (por lo menos lo más relevante) y dejo el proyecto publicado en Github.

        ///<summary>
        /// Making the connection with Band
        /// </summary>

        /// <returns></returns>
        private async Task ConnectToBand()
        {
            var pairedBands = await BandClientManager.Instance.GetBandsAsync();
            if (pairedBands.Any())
            {
                var band = pairedBands.First();
                bandClient = await BandClientManager.Instance.ConnectAsync(band);                
            }
        }

        ///<summary>
        /// Cheking if the app has permission to use the sensor
        /// </summary>

        /// <returns></returns>
        private async Task<bool> HasConsentToUse()
        {
            if (bandClient != null)
            {
                if (bandClient.SensorManager.HeartRate.GetCurrentUserConsent() != UserConsent.Granted)
                {
                    // user has not consented, request it
                    await bandClient.SensorManager.HeartRate.RequestUserConsentAsync();
                    return false;
                }
                else
                {
                    return true;
                }
            }
            Debug.WriteLine("Fail when trying to connect to Microsoft Band");
            return false;
        }

        public async void StartListening()
        {
            statusBar.ProgressIndicator.Text = "acquiring data...";
            await statusBar.ProgressIndicator.ShowAsync();

            await ConnectToBand();
            if (await HasConsentToUse())
            {
                
                if (bandClient != null)
                {
                    var sensor = bandClient.SensorManager.HeartRate;
                    /// Triggering the sensor when changed
                    sensor.ReadingChanged += SensorReadingChanged;
                    /// Initiate the reading
                    await sensor.StartReadingsAsync();
                }
            }
            else
            {
                StartListening();
            }
        }

        public async void StopListening()
        {
            HeartbeatAnimation.Stop();
            var sensor = bandClient.SensorManager.HeartRate;
            sensor.ReadingChanged -= SensorReadingChanged;
            await sensor.StopReadingsAsync();
            bandClient.Dispose();
            StartAppear.Begin();
            HeartbeatOutput.Text = "0";
        }

        async void SensorReadingChanged(object sender, BandSensorReadingEventArgs<IBandHeartRateReading> e)
        {
            try
            {
                if (e.SensorReading != null)
                {
                    this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        HeartbeatAnimation.Begin();
                        HeartbeatOutput.Text = e.SensorReading.HeartRate.ToString();

                        StatusBar.GetForCurrentView().ProgressIndicator.Text = "";
                        StatusBar.GetForCurrentView().ProgressIndicator.HideAsync();
                    });
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error reading/sending data: {0}", ex);
            }
        }

Finalmente aplicando unos cuantos estilos el resultado es el siguiente:

Aquí en este enlace está el proyecto publicado: https://github.com/thEpisode/BandExperiments