예제 #1
0
        private async Task RegisterBand()
        {
            bool backgroundRegistered = false;

            var bandInfo = (await BandClientManager.Instance.GetBandsAsync()).FirstOrDefault();

            var consentGranted = await GetConsentForHeartRate(bandInfo);

            if (consentGranted.HasValue && consentGranted.Value)
            {
                // The Guid used for the RfcommServiceId is from the Package.appxmanifest.
                var device =
                    (await
                     DeviceInformation.FindAllAsync(
                         RfcommDeviceService.GetDeviceSelector(
                             RfcommServiceId.FromUuid(new Guid("A502CA9A-2BA5-413C-A4E0-13804E47B38F")))))
                    .FirstOrDefault(x => x.Name == bandInfo.Name);

                backgroundRegistered = device != null &&
                                       await
                                       BackgroundTaskProvider.RegisterBandDataTask(
                    typeof(BandDataTask).FullName,
                    device.Id);
            }

            if (backgroundRegistered)
            {
                var success =
                    new MessageDialog(
                        "The BandDataTask has been registered successfully and the app can be closed.",
                        "Background task registered");

                success.Commands.Add(new UICommand("Ok", command => { this.IsBandRegistered = true; }));

                await success.ShowAsync();
            }
            else
            {
                MessageDialog error;

                if (consentGranted.HasValue && !consentGranted.Value)
                {
                    error =
                        new MessageDialog(
                            "The BandDataTask was not registered as you have rejected consent to access the heart rate sensor. Please try again.",
                            "Background task not registered");
                }
                else
                {
                    error =
                        new MessageDialog(
                            "The BandDataTask was not registered successfully. Check your Microsoft Band is connected and try again.",
                            "Background task not registered");
                }

                error.Commands.Add(new UICommand("Ok", command => { }));

                await error.ShowAsync();
            }
        }
        private async Task CompleteDeferral()
        {
            try
            {
                await this.SaveRecordedHealth();
            }
            catch (Exception ex)
            {
                // Handle saving failure
            }

            if (this.bandClient != null)
            {
                await this.StopSensorsRunning();

                this.bandClient.Dispose();
                this.bandClient = null;
            }

            BackgroundTaskProvider.UnregisterBandDataTask();

            this.deferral.Complete();
        }