首页 > 解决方案 > Pedometer.watchStepCount 没有发出任何信号

问题描述

我正在使用 react-native / expo 并尝试访问计步器。我的代码在 iOS Expo Go 应用程序上一切正常,但在 Android Expo(诺基亚 7.2 / Android 10)上我没有收到来自Pedometer.watchStepCount.

这是我在进入“WalkScreen”之前正在做的事情:

  const alertIfMotionDisabledAsync = async () => {
    const { status } = await Pedometer.requestPermissionsAsync();
    if (status !== 'granted') {
      Toast.show({
        text: "Sorry! Your sensors aren't activated. Please check that you've allowed them for the Synchronisator.",
        buttonText: "Okay",
        position: "top",
        type: "danger",
      });
    } else {
      navigation.navigate("Walk");
    }
  }

这是我访问计步器的方式:

  const subscribeToPedometer = () => {
    let thisWalking = false;
    let currentStepCountBefore = 0;
    const subscription = Pedometer.watchStepCount(result => {
      if (currentStepCountBefore === result.steps && thisWalking) {
        setWalking(false);
        thisWalking = false;
      } else if (!thisWalking) {
        setWalking(true);
        thisWalking = true;
      }
      currentStepCountBefore = result.steps;
      setCurrentStepCount(result.steps);
      console.log("Pedometer.watchStepCount result.steps", result.steps);
    });
    setPedometerSubscription(subscription);
    Pedometer.isAvailableAsync().then(
      result => {
        console.log("isAvailableAsync", result);
        setIsPedometerAvailable(result);
      },
      error => {
        console.log('Could not get isPedometerAvailable: ' + error);
        setIsPedometerAvailable(false);
      }
    );
  };

我没有任何问题地进入步行屏幕,因此授予状态。在Pedometer.isAvailableAsync()方法中,我得到了真实的,所以它似乎是可用的。但是我的 console.log 中没有任何数据console.log("Pedometer.watchStepCount result.steps", result.steps);

标签: react-nativeexpoandroid-sensors

解决方案


推荐阅读