android - Expo.Pedometer.getStepCountAsync 的奇怪属性

标签 android ios react-native expo

我不确定这是 Expo 还是 iOS 计步器的问题,但我注意到下面描述的奇怪行为。

假设我们有时间间隔 t1 -t2t0 分隔,即t1 < t0 < t2 。现在,如果我们在每个间隔中计算步数:

import { Pedometer } from 'expo';

const steps1 = await Pedometer.getStepCountAsync(t1, t0);
const steps2 = await Pedometer.getStepCountAsync(t0, t2);
const steps3 = await Pedometer.getStepCountAsync(t1, t2);

我预计以下差异接近 0(如果不是始终为 0):

difference = steps1 + steps2 - steps3

但我注意到,在实践中,这通常是不正确的。差异可能是 100 , 200甚至1000脚步。更糟糕的是,如果你尝试“移动”t0在示例中,这种差异也会发生变化。

更糟糕的是,这种情况似乎并不总是发生。我的 iPhone 8 也有这个问题,一个月可能有 2 天。尽管事实上它看起来很愚蠢,但我注意到当白天出现一些连接问题时就会发生这种情况。

问题:为什么每个区间中的步数不等于并集区间中的步数?

最佳答案

我想我找到了解决方案。

当您想要查找从 t1 到 t2 的间隔中的步骤时,而不是:

const steps = await Pedometer.getStepCountAsync(t1, t2);

执行以下操作:

const t0 = new Date(0);
const steps1 = await Pedometer.getStepCountAsync(t0, t1);
const steps2 = await Pedometer.getStepCountAsync(t0, t2);
const steps = steps2 - steps1; // THIS IS THE ANSWER!

这并不能解决计步器/Expo本身的问题,但提供了稳定的解决方案:子间隔中的所有步数现在加起来等于整个间隔中的步数。

重要更新

上述解决方案适用于 iOS,但在 Android 上会导致计步器崩溃。所以我提出了另一个解决方案(使用更短的间隔):

const t0 = startOfDay(t1); // beginning of the same day works fine for me
const steps1 = await Pedometer.getStepCountAsync(t0, t1);
const steps2 = await Pedometer.getStepCountAsync(t0, t2);
const steps = steps2 - steps1; // THIS IS THE ANSWER!

还有一个重要更新

我还注意到,如果你这样做:

await Pedometer.getStepCountAsync(t0, t0); // when both parameters are equal

这也会在 Android 上崩溃。

更重要的更新

请注意,t0 应该是一天的开始!我在尝试不同的设备时了解到了这一点。另外,为了避免在相同时间过去时出现 Android 错误,只需添加一个包装函数来检查日期相等性并在这种情况下返回 0

关于android - Expo.Pedometer.getStepCountAsync 的奇怪属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56463013/

相关文章:

react-native - 任务 ':app:processReleaseResources' 执行失败。

android - React Native - 未优化的 APK 将我的应用程序提交到 Google Play

android - 在 CardView 的父级时设计 CardView 是 ConstraintLayout?

iphone - 关于 App 提交到 Apple iTunes 商店

ios - 从 iOS 中的 UILabel 中删除括号

ios - UITextView - 设置字体不适用于 XCode 5 上的 iOS 6

node.js - 在 React Native 中存储 key 最安全的方式是什么

android - 直接在assets文件夹中打开只读数据库

android - Android监听mysql数据库变化的方法

javascript - 基于多个 ScrollView(s) 为单个 View 设置动画