react-native-health-connect icon indicating copy to clipboard operation
react-native-health-connect copied to clipboard

Getting an empty Array in every parameter request

Open asiflhr opened this issue 1 year ago • 7 comments

Describe the bug Getting an empty Array in every parameter request.

Expected behavior
Hi all, I tried more than 12 parameters I needed in my app one by one but every time I'm getting an empty Array [] in response. I've attached my function calls. Am I missing something or there is some other error?

useEffect(() => {
    const initializeHealthConnect = async () => {
      if (Platform.OS !== 'android') {
        return
      }

      try {
        const isInitialized = await initialize()
        console.log('is initialized: ', isInitialized)
        if (!isInitialized) {
          throw new Error('Health Connect initialization failed')
        }

        const grantedPermissions = await requestPermission([
          { accessType: 'read', recordType: 'Height' },
        ])

        console.log('granted permissions: ', grantedPermissions)

        if (!grantedPermissions) {
          throw new Error('Permissions not granted')
        }

        setPermissionsGranted(true)
      } catch (err) {
        console.error(
          'Error during initialization or permission request: ',
          err,
        )
        setError(err.message)
        setLoading(false)
      }
    }

    initializeHealthConnect()
  }, [])

  useEffect(() => {
    const fetchHealthData = async () => {
      if (permissionsGranted) {
        try {
          const result = await readRecords('Height', {
            timeRangeFilter: {
              operator: 'between',
              startTime: '2023-01-09T12:00:00.405Z',
              endTime: '2024-07-15T23:53:15.405Z',
            },
          })

          console.log('Retrieved record: ', JSON.stringify({ result }, null, 2))
          setHealthData(result)
        } catch (err) {
          console.error('Error fetching health data: ', err)
          setError(err.message)
        } finally {
          setLoading(false)
        }
      }
    }

    fetchHealthData()
  }, [permissionsGranted])

Environment:

  • Health Connect Version: 2.1.0
  • React Native Version: 0.74.3
  • New architecture enabled: No
  • Using Expo: Prebuild(Dev Client)

asiflhr avatar Jul 18 '24 19:07 asiflhr

Hey!

Have you added any entries to the health connect app or from any other source to the health connect? What do you expect to see?

matinzd avatar Jul 19 '24 17:07 matinzd

Hey @matinzd ,

Thanks for reply. Actually, I don't know how to add any entries through health connect app of from other sources. I just installed Health Connect app from playstore and allowed permissions to related entry I want to fetch data about, and getting an empty array.

asiflhr avatar Jul 22 '24 15:07 asiflhr

Other apps or your app should put data in the health connect so that you can retrieve it. Without that, you'll get an empty array.

matinzd avatar Jul 22 '24 15:07 matinzd

Hi @matinzd , Thanks for reply, If I want to track the Steps, do you have example for that how can we put data to the health connect.

asiflhr avatar Jul 23 '24 14:07 asiflhr

I saw a youtube video from Vadim while ago on youtube.

Here is the link: https://www.youtube.com/live/VVoXcr18mdo?si=B6jqo4ZzXNUYLJzy

He used my lib for the integration.

matinzd avatar Jul 23 '24 14:07 matinzd

Thanks @matinzd , let me check

asiflhr avatar Jul 23 '24 15:07 asiflhr

Maybe the docs should point out a bit more forcefully that you have to add permissions to the app.json manually within the "Getting Started" page. Initially I thought that the expo-config-plugin will take care of that as it's just mentioned as part of the React Native CLI installation part.

Image

I found out it's missing after inspecting the AndroidManifest.xml manually.

It's not super clear that you have to add permissions even with Expo but it makes sense that you have to.

MoritzCooks avatar Mar 13 '25 12:03 MoritzCooks