/// <summary>
        /// This is the tick handler for the Refresh timer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RefreshTimer_Tick(object sender, object e)
        {
            // Keeps checking until we have GPS coordinates locked, upon which we will update the device info with IoT Hub
            if (latitude == 0 || longitude == 0)
            {
                GetGeolocation();
            }

            if ((latitude > 0 || longitude > 0) && !IsDeviceInfoUpdated && !String.IsNullOrEmpty(FWVersion) && !String.IsNullOrEmpty(HWVersion))
            {
                IoTHubHttpServiceManager.UpdateDeviceInfo(latitude, longitude, FWVersion, HWVersion);
                IsDeviceInfoUpdated = true;
            }
        }
示例#2
0
        private async void StartButton_Click(object sender, RoutedEventArgs e)
        {
            string action = this.viewModel.ButtonContent;

            // Change UI captions
            if (action.Equals("Start"))
            {
                this.viewModel.ButtonContent = "Stop";
                this.viewModel.StatusMessage = "Searching for Band....";
            }
            else
            {
                if (bandClient != null)
                {
                    await bandClient.SensorManager.HeartRate.StopReadingsAsync();

                    await bandClient.SensorManager.SkinTemperature.StopReadingsAsync();

                    await bandClient.SensorManager.Calories.StopReadingsAsync();

                    await bandClient.SensorManager.Distance.StopReadingsAsync();

                    await bandClient.SensorManager.Pedometer.StopReadingsAsync();

                    if (bandClient.SensorManager.Barometer != null)
                    {
                        await bandClient.SensorManager.Barometer.StopReadingsAsync();
                    }
                    if (bandClient.SensorManager.Gsr != null)
                    {
                        await bandClient.SensorManager.Gsr.StopReadingsAsync();
                    }
                    if (bandClient.SensorManager.AmbientLight != null)
                    {
                        await bandClient.SensorManager.AmbientLight.StopReadingsAsync();
                    }
                    if (bandClient.SensorManager.Altimeter != null)
                    {
                        await bandClient.SensorManager.Altimeter.StopReadingsAsync();
                    }
                }

                this.viewModel.ButtonContent = "Start";
                this.viewModel.StatusMessage = string.Format("Done. {0} Microsoft Band telemetry were ingested.", samplesReceived);
                return;
            }

            if (IoTHubHttpServiceManager == null)
            {
                IoTHubHttpServiceManager = new IoTHubHttpServiceManager(AppSettings.IoTHubHostName, AppSettings.DeviceID, AppSettings.DeviceKey);
            }
            try
            {
                var bandClientManager = BandClientManager.Instance;
                // query the service for paired devices
                var pairedBands = await bandClientManager.GetPairedBandsAsync();

                // connect to the first device
                var bandInfo = pairedBands.FirstOrDefault();
                if (bandInfo == null)
                {
                    this.viewModel.StatusMessage = "This sample app requires a Microsoft Band paired to your device. Also make sure that you have the latest firmware installed on your Band, as provided by the latest Microsoft Health app.";
                    return;
                }

                bandClient = await bandClientManager.ConnectAsync(bandInfo);

                // do work after successful connect
                ConnectedBandName            = bandInfo.Name;
                this.viewModel.StatusMessage = "Connected: " + ConnectedBandName;

                FWVersion = await bandClient.GetFirmwareVersionAsync();

                HWVersion = await bandClient.GetHardwareVersionAsync();

                bool heartRateConsentGranted = false;

                // check current user heart rate consent
                if (bandClient.SensorManager.HeartRate.UserConsented == UserConsent.Unspecified)
                {
                    // user hasn’t consented, request consent
                    await bandClient.SensorManager.HeartRate.RequestUserConsent();
                }


                if (!heartRateConsentGranted)
                {
                    this.viewModel.StatusMessage = "Access to the heart rate sensor is denied.";
                    return;
                }

                // Subscribe to HeartRate data.
                bandClient.SensorManager.HeartRate.ReadingChanged += HeartRate_ReadingChanged;
                await bandClient.SensorManager.HeartRate.StartReadingsAsync();

                // hook up to the Skin temperature sensor ReadingChanged event
                bandClient.SensorManager.SkinTemperature.ReadingChanged += SkinTemperature_ReadingChanged;
                await bandClient.SensorManager.SkinTemperature.StartReadingsAsync();

                // hook up to the Distance sensor ReadingChanged event
                bandClient.SensorManager.Distance.ReadingChanged += Distance_ReadingChanged;
                await bandClient.SensorManager.Distance.StartReadingsAsync();

                // hook up to the Calories sensor ReadingChanged event
                bandClient.SensorManager.Calories.ReadingChanged += Calories_ReadingChanged;
                await bandClient.SensorManager.Calories.StartReadingsAsync();

                // Subscribe to Altimeter data.
                bandClient.SensorManager.Pedometer.ReadingChanged += Pedometer_ReadingChanged;
                await bandClient.SensorManager.Pedometer.StartReadingsAsync();

                if (bandClient.SensorManager.Barometer != null)
                {
                    // Subscribe to Barometer data. - (Microsoft Band 2 only) Provides the current raw air pressure in hPa (hectopascals) and raw temperature in degrees Celsius.
                    bandClient.SensorManager.Barometer.ReadingChanged += Barometer_ReadingChanged;
                    await bandClient.SensorManager.Barometer.StartReadingsAsync();
                }
                else
                {
                    this.viewModel.AirPressure = "Band2 only";
                }

                if (bandClient.SensorManager.Gsr != null)
                {
                    // Subscribe to Galvanic Skin Response. - (Microsoft Band 2 only) Provides the current skin resistance of the wearer in kohms.
                    bandClient.SensorManager.Gsr.ReadingChanged += Gsr_ReadingChanged;
                    await bandClient.SensorManager.Gsr.StartReadingsAsync();
                }
                else
                {
                    this.viewModel.GsrResistance = "Band2 only";
                }


                if (bandClient.SensorManager.AmbientLight != null)
                {
                    // Subscribe to Ambient Light - (Microsoft Band 2 only) Provides the current light intensity (illuminance) in lux (Lumes per sq. meter).
                    bandClient.SensorManager.AmbientLight.ReadingChanged += AmbientLight_ReadingChanged;
                    await bandClient.SensorManager.AmbientLight.StartReadingsAsync();
                }
                else
                {
                    this.viewModel.Brightness = "Band2 only";
                }


                if (bandClient.SensorManager.Altimeter != null)
                {
                    // Subscribe to Altimeter - (Microsoft Band 2 only) Provides current elevation data like total gain/loss, steps ascended/descended, flights ascended/descended, and elevation rate.
                    bandClient.SensorManager.Altimeter.ReadingChanged += Altimeter_ReadingChanged;
                    await bandClient.SensorManager.Altimeter.StartReadingsAsync();
                }
                else
                {
                    this.viewModel.AltimeterRate = "Band2 only";
                }

                //Receive HeartRate data for a duration picked by the user, then stop the subscription.
                int ingestDuration = 0;
                switch (this.viewModel.IngestDuration)
                {
                case 0:
                    ingestDuration = 1;
                    break;

                case 1:
                    ingestDuration = 5;
                    break;

                case 2:
                    ingestDuration = 10;
                    break;

                case 3:
                    ingestDuration = 15;
                    break;

                default:
                    ingestDuration = 1;
                    break;
                }

                await Task.Delay(TimeSpan.FromMinutes(ingestDuration));

                await bandClient.SensorManager.HeartRate.StopReadingsAsync();

                await bandClient.SensorManager.SkinTemperature.StopReadingsAsync();

                await bandClient.SensorManager.Calories.StopReadingsAsync();

                await bandClient.SensorManager.Distance.StopReadingsAsync();

                await bandClient.SensorManager.Pedometer.StopReadingsAsync();

                if (bandClient.SensorManager.Barometer != null)
                {
                    await bandClient.SensorManager.Barometer.StopReadingsAsync();
                }
                if (bandClient.SensorManager.Gsr != null)
                {
                    await bandClient.SensorManager.Gsr.StopReadingsAsync();
                }
                if (bandClient.SensorManager.AmbientLight != null)
                {
                    await bandClient.SensorManager.AmbientLight.StopReadingsAsync();
                }
                if (bandClient.SensorManager.Altimeter != null)
                {
                    await bandClient.SensorManager.Altimeter.StopReadingsAsync();
                }

                this.viewModel.ButtonContent = "Start";
                this.viewModel.StatusMessage = string.Format("Done. {0} Microsoft Band telemetry were ingested.", samplesReceived);
                return;
            }
            catch (Exception ex)
            {
                // handle a Band connection exception }
                System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("[hh:ss.fff]") + " " + ex.ToString());
                this.viewModel.StatusMessage = "Error: Unable to communicate with Microsoft Band.";
            }
            return;
        }