我在使用 Expo 计步器计算步数时遇到问题

发布于 2025-01-15 06:09:19 字数 1164 浏览 4 评论 0原文

我正在使用 expo 计步器,但有两个问题:

  1. 它似乎不计算步数,步数计数始终为零。

  2. Android 不支持 Pedometer.getStepCountAsync 函数。

我正在使用

  • "expo": "~44.0.0"
  • "expo-sensors": "~11.1.0"
  • "react": "17.0.1"
  • "react-dom": "17.0.1"
  • "react-native" :“0.64.3”
  • 我的手机android版本是11

这是我的代码

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>
  )
}

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>
  )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拒绝两难 2025-01-22 06:09:19

只需授予跟踪步数的权限

    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);
  }
};

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);
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文