agencyenterprise / react-native-health

A React Native package to interact with Apple HealthKit
MIT License
861 stars 233 forks source link

getHeartRateSamples returning empty / [] #300

Open pjsandwich opened 1 year ago

pjsandwich commented 1 year ago

Describe the bug AppleHealthKit.getHeartRateSamples is returning an empty array [] regardless of existing data and permissions.

To Reproduce Steps to reproduce the behavior:

  1. successfully initialize healthkit with:

    ...
       const permissions = {
          permissions: {
            read: [
              'HeartRate',
              'RestingHeartRate',
              'SleepAnalysis',
              'ActivitySummary',
            ],
          }
        };
    
        AppleHealthKit.initHealthKit(permissions ...

    In this case, I initialize this data from a useEffect hook which sets a useState hook value in a constant via setWearablesPermissionStatus(true) (or false in the case of error).

  2. From my context, I query the most recent record of heartRate from my database and set either [] if the user doesn't have any data, or the data is set as the most recent item.

  3. request AppleHealthKit like this:

    useEffect(() => {
    // get updated heart rate data from pertinent wearables and write to userheartrate table
    const getUpdatedHeartRateData = async () => {
      if (recentHeartRateItem && Platform.OS === "ios") {
        if (recentHeartRateItem.length === 0) {
          let options = {
            startDate: moment().startOf('day').toISOString(),
            endDate: moment().toISOString(),
          }
    
          AppleHealthKit.getHeartRateSamples(options, (err, results) => {
            if (err) {
              console.log('error getting healthkit heart rate data: ', err);
              return;
            }
    
            console.log('healthkit heart rate results: ', results, results.length)
            setNewHeartRateRecords(results)
          })
        }
    
        if (recentHeartRateItem.length > 0) {
          let options = {
            startDate: moment(recentHeartRateItem[0].HeartRateEndDate).toISOString(),
            endDate: moment().toISOString(),
          }
    
          AppleHealthKit.getHeartRateSamples(options, (err, results) => {
            if (err) {
              console.log('error getting healthkit heart rate data: ', err);
              return;
            }
    
            console.log('healthkit heart rate results: ', results, results.length)
            setNewHeartRateRecords(results)
          })
        }
      }
    }
    
    getUpdatedHeartRateData();
    }, [recentHeartRateItem])
  4. Getting this response: healthkit heart rate results: [] 0

Expected behavior AppleHealthKit.getHeartRateSamples should return an array of the data within the time frame as opposed to an empty array.

Screenshots Screenshots from Apple Health showing existing data and the existence of heart rate values: IMG_2701 IMG_2702

Smartphone (please complete the following information):

Additional context I have a getSleepSamples query which seems to work fine with the same setup in my code. However, getHeartRateSamples and getSamples does not work as expected. Maybe it's as simple as a permission issue? Do I need more permissions for this data?

pjsandwich commented 1 year ago

I think this is related to https://github.com/agencyenterprise/react-native-health/issues/284

pjsandwich commented 1 year ago

Update: After changing my query startDate to the meginning of the month, I retrieve the heart rate data. Unsure as to why the start of the day does not return anything.