Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getHeartRateSamples returning empty / [] #300

Open
pjsandwich opened this issue Mar 16, 2023 · 2 comments
Open

getHeartRateSamples returning empty / [] #300

pjsandwich opened this issue Mar 16, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@pjsandwich
Copy link

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).

  1. 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.

  2. 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])
  1. 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):

  • Device: iPhone 13 mini
  • OS: iOS16.1.1
  • Version ?

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 pjsandwich added the bug Something isn't working label Mar 16, 2023
@pjsandwich
Copy link
Author

I think this is related to #284

@pjsandwich
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant