Pedometer
from expo-sensors
uses Core Motion on iOS and the system hardware.Sensor
on Android to get the user's step count, and also allows you to subscribe to pedometer updates.Android Device | Android Emulator | iOS Device | iOS Simulator | Web |
---|---|---|---|---|
expo install expo-sensors
If you're installing this in a bare React Native app, you should also follow these additional installation instructions.
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> ); } } %%placeholder-start%%const styles = StyleSheet.create({ ... }); %%placeholder-end%%const styles = StyleSheet.create({ container: { flex: 1, marginTop: 15, alignItems: 'center', justifyContent: 'center', }, });
import { Pedometer } from 'expo-sensors';
Boolean
, indicating whether the pedometer is available on this device.Object
with a steps
key, which is a Number
indicating the number of steps taken between the given dates.Only the past seven days worth of data is stored and available for you to retrieve. Specifying a start date that is more than seven days in the past returns only the available data.
steps
key.