lucaspbordignon / rn-apple-healthkit

A React Native package for interacting with Apple HealthKit
https://www.npmjs.com/package/react-native-health
MIT License
520 stars 298 forks source link

getSleepSamples() and getBloodPressureSamples() Not working #38

Open XaviSmith opened 6 years ago

XaviSmith commented 6 years ago

getSleepSamples() and getBloodPressureSamples() never return any data. I've triple checked that my app has permission to read data from HealthKit and that data is present however both functions never return any data. I think it's those functions particularly because calling getStepCount() with the same setup/options works fine

CODE:

//Pedometer permissions stuff const HKPERMISSIONS = AppleHealthKit.Constants.Permissions;

//Options for all Apple Healthkit features let OPTIONS = { permissions: { read: [HKPERMISSIONS.StepCount, HKPERMISSIONS.SleepAnalysis, HKPERMISSIONS.BloodPressureDiastolic, HKPERMISSIONS.BloodPressureSystolic], write: [HKPERMISSIONS.StepCount] } };

let BLOODOPTIONS = { unit: 'mmhg', // optional; default 'mmhg' startDate: (new Date(2018,5,20)).toISOString(), // required endDate: (new Date()).toISOString(), // optional; default now ascending: false, // optional; default false limit:10, // optional; default no limit }

let SLEEPOPTIONS = { startDate: (new Date(2018,5,20)).toISOString(), // required endDate: (new Date()).toISOString(), // optional; default now //limit:10, // optional; default no limit };

...

AppleHealthKit.initHealthKit(OPTIONS, (err, res) => { if(this._handleHealthKitError(err, 'initHealthKit')) { return; } // healthkit initialized...

          //Blood Pressure Example

          AppleHealthKit.getBloodPressureSamples(BLOODOPTIONS, (err: Object, results: Array<Object>) => {
            if (err) {
              return;
            }
            console.warn("BLOOD: " + results);
          });

      //Sleep Analysis

      AppleHealthKit.getSleepSamples(SLEEPOPTIONS, (err: Object, res:Array<Object>) => {
        if (err) {
          console.warn("ERROR: " + err)
        return;
        }
        console.warn("SLEEP: " + res)
      });

...

}

XaviSmith commented 6 years ago

I looked more into it and it seems whatever StartDate you enter is pushed forward by 1 month in the obj-c (endDate was always whatever date i entered). Working around it by just entering a start date of 1 month earlier than it should be but looks like something to fix in the next release