public bool RequestPermissions()
        {
            var stepsKey          = HKQuantityTypeIdentifierKey.StepCount;
            var stepsQuantityType = HKObjectType.GetQuantityType(stepsKey);

            _healthStore.RequestAuthorizationToShare(
                new NSSet(),
                new NSSet(new[] {
                stepsQuantityType
            }),
                (success, error) =>
            {
                if (success)
                {
                    StepQuery();
                }
                else
                {
                    Console.WriteLine(error);
                }
            }
                );

            return(true);
        }
示例#2
0
        protected override void Initialize()
        {
            base.Initialize();

            // all HealthKit permissions were requested in a single batch when the protocol was started; however, HealthKit probes
            // can also be started individually after the protocol is started by toggling Enabled on the probe page. in such cases
            // we want to request permission for the HealthKit probe that was just started. if the permission associated with this
            // probe was already requested, these calls will have no effect. even if the user previously denied access for this probe
            // these calls will not do anything by design by iOS. however, if the user has never been prompted for the permission
            // associated with this probe (i.e., the user is starting the probe by toggling Enabled), then the user will be prompted.
            if (HKHealthStore.IsHealthDataAvailable)
            {
                NSSet            objectTypesToRead = NSSet.MakeNSObjectSet <HKObjectType>(new HKObjectType[] { ObjectType });
                ManualResetEvent authorizationWait = new ManualResetEvent(false);
                _healthStore.RequestAuthorizationToShare(new NSSet(), objectTypesToRead,
                                                         (success, error) =>
                {
                    if (error != null)
                    {
                        SensusServiceHelper.Get().Logger.Log("Error while requesting HealthKit authorization:  " + error.Description, LoggingLevel.Normal, GetType());
                    }

                    authorizationWait.Set();
                });

                authorizationWait.WaitOne();
            }
        }
示例#3
0
        private void ValidateAuthorization()
        {
            var typesToShare = new NSSet(HKQuantityType.Create(HKQuantityTypeIdentifier.HeartRate), HKQuantityType.Create(HKQuantityTypeIdentifier.ActiveEnergyBurned), HKObjectType.GetWorkoutType());
            var typesToRead  = new NSSet(HKQuantityType.Create(HKQuantityTypeIdentifier.HeartRate), HKQuantityType.Create(HKQuantityTypeIdentifier.ActiveEnergyBurned));

            _healthStore.RequestAuthorizationToShare(
                typesToShare,
                typesToRead,
                ReactToHealthCarePermissions);
        }
示例#4
0
        private void ValidateAuthorization()
        {
            var bodyMassType = HKQuantityType.Create(HKQuantityTypeIdentifier.BodyMass);
            var typesToWrite = new NSSet(new[] { bodyMassType });
            var typesToRead  = new NSSet(new[] { bodyMassType });

            healthKitStore.RequestAuthorizationToShare(
                typesToWrite,
                typesToRead,
                ReactToHealthCarePermissions);
        }
示例#5
0
        private void ValidateAuthorization()
        {
            //Request / Validate that the app has permission to store heart-rate data
            var heartRateId   = HKQuantityTypeIdentifierKey.HeartRate;
            var heartRateType = HKObjectType.GetQuantityType(heartRateId);
            var typesToWrite  = new NSSet(new [] { heartRateType });
            //We aren't reading any data for this sample
            var typesToRead = new NSSet();

            healthKitStore.RequestAuthorizationToShare(
                typesToWrite,
                typesToRead,
                ReactToHealthCarePermissions);
        }
 public void GetHealthPermissionAsync(Action <bool> completion)
 {
     if (HKHealthStore.IsHealthDataAvailable)
     {
         HealthStore = new HKHealthStore();
         HealthStore.RequestAuthorizationToShare(DataTypesToWrite, DataTypesToRead, (bool authorized, NSError error) =>
         {
             completion(authorized);
         });
     }
     else
     {
         completion(false);
     }
 }
示例#7
0
        public bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            var read = new NSSet(HKQuantityType.Create(HKQuantityTypeIdentifier.HeartRate));

            var healthstore = new HKHealthStore();

            healthstore.RequestAuthorizationToShare(new NSSet(), read, (f, error) => {
                if (error != null)
                {
                    Console.WriteLine(@"{0} error", error);
                }
            });


            return(true);
        }
示例#8
0
        private void RequestAccessToHealthKit()
        {
            var healthStore = new HKHealthStore();

            var types = new NSSet(HKObjectType.GetWorkoutType(),
                                  HKSeriesType.WorkoutRouteType,
                                  HKQuantityType.Create(HKQuantityTypeIdentifier.ActiveEnergyBurned),
                                  HKQuantityType.Create(HKQuantityTypeIdentifier.DistanceWalkingRunning));

            healthStore.RequestAuthorizationToShare(types, types, (isSuccess, error) =>
            {
                if (!isSuccess)
                {
                    Console.WriteLine(error?.LocalizedDescription ?? "");
                }
            });
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad ();

            var massKey = HKQuantityTypeIdentifierKey.BodyMass;
            var massQuantityType = HKObjectType.GetQuantityType (massKey);

            var hks = new HKHealthStore ();
            hks.RequestAuthorizationToShare (new NSSet (new [] { massQuantityType }), new NSSet (), (success, error) => {
                Console.WriteLine ("Authorized:" + success);
                if (error != null) {
                    Console.WriteLine ("Authorization error: " + error);
                }
            });

            this.SaveWeight (hks);
        }
示例#10
0
        public bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            // Override point for customization after application launch.
            // If not required for your application you can safely delete this method

            var read = new NSSet(HKQuantityType.Create(HKQuantityTypeIdentifier.HeartRate));

            var healthstore = new HKHealthStore();

            healthstore.RequestAuthorizationToShare(new NSSet(), read, (f, error) => {
                if (error != null)
                {
                    Console.WriteLine(@"{0} error", error);
                }
            });

            return(true);
        }
示例#11
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            var massKey          = HKQuantityTypeIdentifierKey.BodyMass;
            var massQuantityType = HKObjectType.GetQuantityType(massKey);

            var hks = new HKHealthStore();

            hks.RequestAuthorizationToShare(new NSSet(new [] { massQuantityType }), new NSSet(), (success, error) => {
                Console.WriteLine("Authorized:" + success);
                if (error != null)
                {
                    Console.WriteLine("Authorization error: " + error);
                }
            });

            this.SaveWeight(hks);
        }
示例#12
0
        public void SetUpPermissions()
        {
            var distanceQuantityType          = HKObjectType.GetQuantityType(HKQuantityTypeIdentifierKey.DistanceWalkingRunning);
            var stepsQuantityType             = HKObjectType.GetQuantityType(HKQuantityTypeIdentifierKey.StepCount);
            var flightsQuantityType           = HKObjectType.GetQuantityType(HKQuantityTypeIdentifierKey.FlightsClimbed);
            var heightQuantityType            = HKObjectType.GetQuantityType(HKQuantityTypeIdentifierKey.Height);
            var heartRateQuantityType         = HKObjectType.GetQuantityType(HKQuantityTypeIdentifierKey.HeartRate);
            var nikeFuelQuantityType          = HKObjectType.GetQuantityType(HKQuantityTypeIdentifierKey.NikeFuel);
            var dateOfBirthCharacteristicType = HKObjectType.GetCharacteristicType(HKCharacteristicTypeIdentifierKey.DateOfBirth);
            var sexCharacteristicType         = HKObjectType.GetCharacteristicType(HKCharacteristicTypeIdentifierKey.BiologicalSex);
            var bloodTypeCharacteristicType   = HKObjectType.GetCharacteristicType(HKCharacteristicTypeIdentifierKey.BloodType);

            if (m_healthKitStore == null)
            {
                HealthKitStore = new HKHealthStore();
                m_healthKitStore.RequestAuthorizationToShare(new NSSet(new [] { distanceQuantityType, stepsQuantityType, flightsQuantityType, heartRateQuantityType }), new NSSet(new [] { (NSObject)distanceQuantityType, (NSObject)stepsQuantityType, (NSObject)flightsQuantityType, (NSObject)heightQuantityType, (NSObject)dateOfBirthCharacteristicType, (NSObject)sexCharacteristicType, (NSObject)bloodTypeCharacteristicType, (NSObject)nikeFuelQuantityType, (NSObject)bloodTypeCharacteristicType, (NSObject)heartRateQuantityType }), (success, error) => {
                    Console.WriteLine("Authorized:" + success);
                    if (error != null)
                    {
                        Console.WriteLine("Authorization error: " + error);
                    }
                });
            }
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad ();

            #region UI controls
            statusLabel = new UILabel (new RectangleF(10, 30, 300, 30));
            statusLabel.Text = "waiting...";

            heartRateLabel = new UILabel(new RectangleF(10, 70, 150, 30));
            heartRateLabel.Font = UIFont.BoldSystemFontOfSize (36);
            heartRateLabel.TextColor = UIColor.Red;

            heartRateUnitLabel = new UILabel(new RectangleF(160, 70, 150, 30));

            deviceNameLabel = new UILabel(new RectangleF(10, 120, 300, 30));

            connectButton = UIButton.FromType (UIButtonType.System);
            connectButton.SetTitle ("Connect", UIControlState.Normal);
            connectButton.SetTitle ("searching...", UIControlState.Disabled);
            connectButton.Enabled = false;
            connectButton.Frame = new RectangleF (10, 160, 300, 30);
            connectButton.TouchUpInside += ConnectToSelectedDevice;

            permissionsLabel = new UILabel (new RectangleF (10, 200, 300, 60));

            storeData = UIButton.FromType (UIButtonType.System);
            storeData.Frame = new RectangleF (10, 250, 300, 30);
            storeData.SetTitle ("requires permission", UIControlState.Disabled);
            storeData.SetTitle ("Store in HealthKit", UIControlState.Normal);
            storeData.Enabled = false;
            storeData.TouchUpInside += (sender, e) => {
                UpdateHealthKit(heartRateLabel.Text); // pretty hacky :)
            };

            Add (statusLabel);
            Add (heartRateLabel);
            Add (heartRateUnitLabel);
            Add (deviceNameLabel);
            Add (connectButton);
            Add (permissionsLabel);
            Add (storeData);
            #endregion

            InitializeCoreBluetooth ();

            #region HealthKit
            // https://gist.github.com/lobrien/1217d3cff7b29716c0d3
            // http://www.knowing.net/index.php/2014/07/11/exploring-healthkit-with-xamarin-provisioning-and-permissions-illustrated-walkthrough/

            healthKitStore = new HKHealthStore();
            //Permissions
            //Request HealthKit authorization
            var heartRateId = HKQuantityTypeIdentifierKey.HeartRate;
            var heartRateType = HKObjectType.GetQuantityType (heartRateId);
            //Request to write heart rate, read nothing...
            healthKitStore.RequestAuthorizationToShare (new NSSet (new [] { heartRateType }), new NSSet (), (success, error) =>
                InvokeOnMainThread (() => {
                    if (success) {
                        //Whatever...
                        Console.WriteLine ("RequestAuthorizationToShare: success");
                        permissionsLabel.Text = "HealthKit access is enabled!";
                        storeData.Enabled = true;
                    } else {
                        //Whatever...
                        Console.WriteLine ("RequestAuthorizationToShare:  failed");
                        permissionsLabel.Text = "No permission to access HealthKit :-(";
                        storeData.Enabled = false;
                    }
                    if (error != null) {
                        Console.WriteLine ("HealthKit authorization error: " + error);
                    }
                }));
            #endregion
        }
示例#14
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            #region UI controls
            statusLabel      = new UILabel(new RectangleF(10, 30, 300, 30));
            statusLabel.Text = "waiting...";

            heartRateLabel           = new UILabel(new RectangleF(10, 70, 150, 30));
            heartRateLabel.Font      = UIFont.BoldSystemFontOfSize(36);
            heartRateLabel.TextColor = UIColor.Red;

            heartRateUnitLabel = new UILabel(new RectangleF(160, 70, 150, 30));

            deviceNameLabel = new UILabel(new RectangleF(10, 120, 300, 30));

            connectButton = UIButton.FromType(UIButtonType.System);
            connectButton.SetTitle("Connect", UIControlState.Normal);
            connectButton.SetTitle("searching...", UIControlState.Disabled);
            connectButton.Enabled        = false;
            connectButton.Frame          = new RectangleF(10, 160, 300, 30);
            connectButton.TouchUpInside += ConnectToSelectedDevice;

            permissionsLabel = new UILabel(new RectangleF(10, 200, 300, 60));

            storeData       = UIButton.FromType(UIButtonType.System);
            storeData.Frame = new RectangleF(10, 250, 300, 30);
            storeData.SetTitle("requires permission", UIControlState.Disabled);
            storeData.SetTitle("Store in HealthKit", UIControlState.Normal);
            storeData.Enabled        = false;
            storeData.TouchUpInside += (sender, e) => {
                UpdateHealthKit(heartRateLabel.Text);                 // pretty hacky :)
            };

            Add(statusLabel);
            Add(heartRateLabel);
            Add(heartRateUnitLabel);
            Add(deviceNameLabel);
            Add(connectButton);
            Add(permissionsLabel);
            Add(storeData);
            #endregion

            InitializeCoreBluetooth();

            #region HealthKit
            // https://gist.github.com/lobrien/1217d3cff7b29716c0d3
            // http://www.knowing.net/index.php/2014/07/11/exploring-healthkit-with-xamarin-provisioning-and-permissions-illustrated-walkthrough/

            healthKitStore = new HKHealthStore();
            //Permissions
            //Request HealthKit authorization
            var heartRateId   = HKQuantityTypeIdentifierKey.HeartRate;
            var heartRateType = HKObjectType.GetQuantityType(heartRateId);
            //Request to write heart rate, read nothing...
            healthKitStore.RequestAuthorizationToShare(new NSSet(new [] { heartRateType }), new NSSet(), (success, error) =>
                                                       InvokeOnMainThread(() => {
                if (success)
                {
                    //Whatever...
                    Console.WriteLine("RequestAuthorizationToShare: success");
                    permissionsLabel.Text = "HealthKit access is enabled!";
                    storeData.Enabled     = true;
                }
                else
                {
                    //Whatever...
                    Console.WriteLine("RequestAuthorizationToShare:  failed");
                    permissionsLabel.Text = "No permission to access HealthKit :-(";
                    storeData.Enabled     = false;
                }
                if (error != null)
                {
                    Console.WriteLine("HealthKit authorization error: " + error);
                }
            }));
            #endregion
        }