matinzd / react-native-health-connect

React native library for health connect (Android only)
http://matinzd.github.io/react-native-health-connect
MIT License
197 stars 41 forks source link

Getting an empty Array in every parameter request #118

Open asiflhr opened 1 month ago

asiflhr commented 1 month ago

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:

matinzd commented 1 month ago

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?

asiflhr commented 1 month ago

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.

matinzd commented 1 month ago

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.

asiflhr commented 1 month ago

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.

matinzd commented 1 month ago

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.

asiflhr commented 1 month ago

Thanks @matinzd , let me check