question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pedometer.watchStepCount is not working on Android

See original GitHub issue

Summary

Pedometer.watchStepCount is not working on Android 11, tested on Samsung A71 and A51. Pedometer.isAvailableAsync() is returning true But the callback of Pedometer.watchStepCount never triggers, no matter how many steps I walked. It worked well in iOS.

Managed or bare workflow? If you have ios/ or android/ directories in your project, the answer is bare!

managed

What platform(s) does this occur on?

Android

SDK Version (managed workflow only)

41

Environment

Expo CLI 4.5.2 environment info: System: OS: macOS 11.4 Shell: 5.8 - /bin/zsh Binaries: Node: 14.17.0 - /usr/local/opt/node@14/bin/node Yarn: 1.22.10 - /usr/local/bin/yarn npm: 6.14.13 - /usr/local/opt/node@14/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman Managers: CocoaPods: 1.10.1 - /usr/local/bin/pod SDKs: Android SDK: API Levels: 16, 21, 23, 28, 29, 30 Build Tools: 28.0.3, 29.0.2, 30.0.3 System Images: android-29 | Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom, android-30 | Google APIs Intel x86 Atom, android-30 | Google Play Intel x86 Atom IDEs: Android Studio: 4.2 AI-202.7660.26.42.7351085 Xcode: /undefined - /usr/bin/xcodebuild npmPackages: expo: ~41.0.1 => 41.0.1 react: 16.13.1 => 16.13.1 react-dom: 16.13.1 => 16.13.1 react-native: https://github.com/expo/react-native/archive/sdk-41.0.0.tar.gz => 0.63.2 react-native-web: ~0.13.12 => 0.13.18 npmGlobalPackages: expo-cli: 4.5.2 Expo Workflow: managed

Reproducible demo or steps to reproduce from a blank project

You can test with the pedometer demo on the https://docs.expo.io/versions/latest/sdk/pedometer/ Or you can use my code


import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Pedometer } from 'expo-sensors';

export default class App extends React.Component {
  state = {
    isPedometerAvailable: 'checking',
    pastStepCount: 0,
    currentStepCount: 0,
  };

  componentDidMount() {
    this._subscribe();
  }

  componentWillUnmount() {
    this._unsubscribe();
  }

  _subscribe = () => {
    this._subscription = Pedometer.watchStepCount(result => {
      this.setState({
        currentStepCount: result.steps,
      });
    });

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

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

  _unsubscribe = () => {
    this._subscription && this._subscription.remove();
    this._subscription = null;
  };

  render() {
    return (
      <View style={styles.container}>
        <Text>Pedometer.isAvailableAsync(): {this.state.isPedometerAvailable}</Text>
        <Text>Steps taken in the last 24 hours: {this.state.pastStepCount}</Text>
        <Text>Walk! And watch this go up: {this.state.currentStepCount}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: 15,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lclrobert2020commented, Jul 15, 2021

Same on Google Pixel 5 with Android 11 and latest sdk 42 (with either my own code or from the snack on the official doc). This is probably a Permission issue as Expo App have not the permission to access my fitness data. Calling getPermissionsAsync or requestPermissionAsync returns Authorized but does not request for permission at all.

I confirmed that this is a permission issue, we need a fitness permission in order to use the fitness data. I solve this by using a react native module https://github.com/zoontek/react-native-permissions. This is the best permission requesting module as it includes the latest permission. Even the official rn permission module from react native doesn’t include activity permission. expo please update the permission module

1reaction
felicienfrancoiscommented, Jul 15, 2021

Same on Google Pixel 5 with Android 11 and latest sdk 42 (with either my own code or with the snack from the official doc). This is probably a Permission issue as Expo App have not the permission to access my fitness data. Calling getPermissionsAsync or requestPermissionAsync returns Authorized but does not request for permission at all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pedometer not working on Android · Issue #9463 · expo/expo
This method retrieves step count between two past dates from operating system (on iOS up to 7 days past). However, you can count...
Read more >
Android - My step counter is not working - what should I do?
If it still doesn't work, your phone's internal pedometer may have stopped working - restart your device. It seems that the WeWard step...
Read more >
I have a trouble counting steps with expo pedometer
I am using expo pedometer but I have two problems: It doesn't seem to count the steps,the steps count is always zero. The...
Read more >
Expo Pedometer not returning step count in Android
The problem is, the function Pedometer.getStepCountAsync does not work in Android, the function returns "Getting step count for date range is ...
Read more >
My pedometer isn't working properly (Android only)
My pedometer isn't working properly (Android only) · 1. Go to mobile Settings → Battery & Performance → find "Battery Saver" → choose...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found