I'm using react-native-health to create an app to read daily step counts. I have manually added steps on the simulator Health app and the data source is showing my app and all the Health permissions are on but when I try to getDailyStepCountSamples, I get an empty array.
Also, another issue I have is that I need to initiate HealthKit each time before getting the empty array otherwise I get 'undefined'. Once I initiate HealthKit again, it will return 'undefined' AND log 'Steps: []'. Is there something wrong with my code?
import AppleHealthKit from 'react-native-health'; export function useHealthKit({ init = false, steps = false, }) { const PERMS = AppleHealthKit.Constants.Permissions; const initiate = () => { let permissions = { permissions: { read: [ PERMS.StepCount, ], }, }; AppleHealthKit.initHealthKit(permissions, (err, results) => { if (err) { console.log(err); } else { console.log('Initialized!', results), } }); }; const getSteps = () => { let stepOptions = { startDate: new Date(2021, 1, 1).toISOString(), }; AppleHealthKit.getDailyStepCountSamples( stepOptions, (err, results) => { if (err) { return; } console.log('Steps: ', results); }, ); }; init && initiate(); steps && getSteps(); }
I call this by doing the following:
const SomeView = () => { <View> <Button onPress={() => useHealthKit({init: true})> <Text>Initiate HealthKit</Text> </Button> <Button onPress={() => console.log(useHealthKit({steps: true}))> <Text>Console.log steps</Text> </Button> </View> };
https://stackoverflow.com/questions/65802888/react-native-healthkit-getdailystepcountsamples-returning-undefined-or-empty-arr January 20, 2021 at 11:58AM
没有评论:
发表评论