how do we access the data generated from the pedometer API?


#1

hey, i’m just a beginner to react-native.
i was wondering if there’s any way to access the data generated by the pedometer API (from expo docs).
in my case, i need to access the number of steps.
i mean i am trying to get that data and send it to a database to store it and again retrieve it back to mobile.
is there any way? is it possible ? for the API data to hit to a database and get stored over there and re-hit to Application.
i just want the number of steps taken by the user to be stored.
thank you in advance


#2

You mean this? https://docs.expo.io/versions/latest/sdk/pedometer


#3

yes @buishi that’s the pedometer ive used and i want the step count generated by that API
ive created an APK file and able to run it on my device
thank you


#4

It looks pretty straightforward, something like this should work:

async getSteps() {
  const start = new Date();
  const end = start - 24 * 60 * 60 * 1000;
  const getSteps = await Expo.Pedometer.getStepCountAsync(start, end);
  return getSteps.steps;
}

(returns steps for the last day)


#5

im able to get the steps with this method.

 const end = new Date();
    const start = new Date();
    start.setDate(end.getDate() - 1);
    Pedometer.getStepCountAsync(start, end).then(
      result => {
        this.setState({ pastStepCount: result.steps });
      },
      error => {
        this.setState({
          pastStepCount: "Could not get stepCount: " + error
        });
      }
    );

but im asking if i can access the storage space on the mobile where its being stored, so that i can redirect that
to firebase database and again post it on my device.
the data is being reset if i clear my cache data of the app.
if theres any way to access that cache storage or any other ways to access that would be very helpful.
thank you.


#6

You need to use some sort of storage. I use redux, react-redux and redux persist, but if your needs are basic, you could use AsyncStorage (react native’s built in storage tool, here).

    const end = new Date();
    const start = new Date();
    start.setDate(end.getDate() - 1);
    Pedometer.getStepCountAsync(start, end).then(
      result => {
        this.setState({ pastStepCount: result.steps }, async () => {
           await AsyncStorage.setItem('steps', result.steps);
           sendStepsToFirebase(result.steps);
        });
      },
      error => {
        this.setState({
          pastStepCount: "Could not get stepCount: " + error
        });
      }
    );

#7

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.