expo: Pedometer not working on Android
🐛 Bug Report
Summary of Issue
Pedometer is not working on android (I’ve literally copied the code from the docs) and says “step count for date range is not supported on Android yet”
Environment - output of expo diagnostics & the platform(s) you’re targeting
Expo CLI 3.22.3 environment info: System: OS: Windows 10 10.0.18363 Binaries: Node: 12.18.2 npm: 6.14.5 IDEs: Android Studio: Version 4.0.0.0 AI-193.6911.18.40.6514223 npmPackages: expo: ^38.0.0 => 38.0.8 react: 16.11.0 => 16.11.0 react-dom: 16.11.0 => 16.11.0 react-native: https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz => 0.62.2 react-native-web: ~0.11.7 => 0.11.7 react-navigation: ^4.3.9 => 4.4.0
Reproducible Demo
import React from 'react';
import { Pedometer } from 'expo-sensors';
import { StyleSheet, Text, View } from 'react-native';
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',
},
});
Steps to Reproduce
Simply run the app and show this view.
Expected Behavior vs Actual Behavior
I’m expecting to get step count, but I’m getting an error (previously mentioned on summary of issue).
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 16 (6 by maintainers)
any news
There are no plans to implement this for Android (unless something changes and Android e.g. 13 starts supporting it natively through OS). The implementation from ancient SDK 33 used Google Fit, but we don’t want this included in Expo Go anymore. As being said above, GoogleFit is not supported by all Android phones, so it’s not worth the extra size if it’s not going to work on all devices.
A better news is that now, instead of Expo Go, you can build your own development client and customize its native code to support GoogleFit / Samsung Fit / whatever. This is not as convenient as having it directly in Expo Go, but this is the official and recommended way when you need any native APIs that are not included in Expo Go.
In the future, a community-driven package might appear like
expo-fitness, but it’s a matter of year or so and I’m just speculating (just gossip). If somehow it really happens, I’ll let you know 😄Yes you’re right - According to https://github.com/expo/expo/pull/4047#discussion_r277892367, before migrating to unimodules, it used to get data from Google Fitness API, now it uses Android hardware sensors API. We’re going to discuss restoring Fit API internally. However, you can still use Fitness REST API in conjunction with Expo AuthSession to get the data you need.
Expo is focusing on supporting 3rd party native libraries rather than putting everything in Expo Go. The reason for this - sensitive information such as health information (step counting is considered in this category) usually needs either special permissions/entitlements or API keys. It makes difficult both for Expo team to publish Expo Go to app stores and for developers to configure all that stuff in release apps. It also comes with a burden to maintain it all. This is why we’re deprecating Google-sign-in, Amplitude, Segment, FB ads etc. in favour of official or 3rd-party RN libraries. This is why we introduced Dev Clients.
For pedometer, there is e.g. (first result in Google) react-native-fitness - but you’ll need to create a config plugin for it (here you can find many examples). You can also look for fitness/pedometer libs in React Native Directory.