I used expo-sensors
in my project but Pedometer.watchStepCount does not return any value.
- SDK Version: 40.0.1
- Platforms(Android/iOS/web/all): Android
Environment:
"dependencies": {
"expo": "~40.0.0",
"expo-sensors": "~9.2.0",
"expo-status-bar": "~1.0.3",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-40.0.1.tar.gz",
"react-native-unimodules": "^0.12.0",
"react-native-web": "~0.13.12"
}
App.js
import { StatusBar } from 'expo-status-bar';
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,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});