示例#1
0
        public void StoreHeartRate(HKQuantity quantity)
        {
            var bpm = HKUnit.Count.UnitDividedBy(HKUnit.Minute);

            //Confirm that the value passed in is of a valid type (can be converted to beats-per-minute)
            if (!quantity.IsCompatible(bpm))
            {
                InvokeOnMainThread(() => ErrorMessageChanged(this, new GenericEventArgs <string>("Units must be compatible with BPM")));
            }

            var heartRateQuantityType = HKQuantityType.Create(HKQuantityTypeIdentifier.HeartRate);
            var heartRateSample       = HKQuantitySample.FromType(heartRateQuantityType, quantity, new NSDate(), new NSDate(), new HKMetadata());

            using (var healthKitStore = new HKHealthStore())
            {
                healthKitStore.SaveObject(heartRateSample, (success, error) =>
                {
                    InvokeOnMainThread(() =>
                    {
                        if (success)
                        {
                            HeartRateStored?.Invoke(this, new GenericEventArgs <Double>(quantity.GetDoubleValue(bpm)));
                        }
                        else
                        {
                            ErrorMessageChanged(this, new GenericEventArgs <string>("Save failed"));
                        }
                        if (error != null)
                        {
                            //If there's some kind of error, disable
                            Enabled = false;
                            ErrorMessageChanged(this, new GenericEventArgs <string>(error.ToString()));
                        }
                    });
                });
            }
        }