I have a trouble counting steps with expo pedometer

1.8k Views Asked by At

I am using expo pedometer but I have two problems:

  1. It doesn't seem to count the steps,the steps count is always zero.

  2. The Pedometer.getStepCountAsync function isn't supported in android.

I am using

  • "expo": "~44.0.0"
  • "expo-sensors": "~11.1.0"
  • "react": "17.0.1"
  • "react-dom": "17.0.1"
  • "react-native": "0.64.3"
  • my mobile android version is 11

Here's my code

import { Pedometer } from "expo-sensors";
export default function HomeScreen() {
  const [pedometerAvailability, setPedometerAvailability] = useState("");
  const [stepsCount, setStepsCount] = useState(0);

  useEffect(() => {
    subscribe();
    return () => {};
  }, []);

  const subscribe = () => {
    const subscription = Pedometer.watchStepCount((result) => {
      setStepsCount(result.steps);
    });

    Pedometer.isAvailableAsync().then(
      (result) => {
        setPedometerAvailability(String(result));
      },
      (error) => {
        setPedometerAvailability("Could not get isPedometerAvailable: " + error);
      }
    );
  };

  return (
   <Text>{stepsCount}</Text>
  )
}
1

There are 1 best solutions below

1
On

just give permission to track steps

    const requestActivityPermission = async () => {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACTIVITY_RECOGNITION,
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      Alert.alert("Start walking");
    } else {
      Alert.alert("permission denied");
    }
  } catch (err) {
    console.warn(err);
  }
};