Guides
Plan-enterprise-icon
Expo Application Services
API Reference

Pedometer

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.

Platform Compatibility

Android DeviceAndroid EmulatoriOS DeviceiOS SimulatorWeb
Status-success-iconStatus-success-iconStatus-success-iconStatus-success-iconStatus-failed-icon

Installation

Terminal
→ npx expo install expo-sensors

If you're installing this in a bare React Native app, you should also follow these additional installation instructions.

Usage

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

API

import { Pedometer } from 'expo-sensors';

Methods

Pedometer.getPermissionsAsync()

Checks user's permissions for accessing pedometer.

Returns

  • Undo-iconPromise<PermissionResponse>

Pedometer.getStepCountAsync(start, end)

NameTypeDescription
startDate

A date indicating the start of the range over which to measure steps.

endDate

A date indicating the end of the range over which to measure steps.

Get the step count between two dates.

Returns a promise that fulfills with a PedometerResult.

As Apple documentation states:

Info-icon

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.

Pedometer.isAvailableAsync()

Returns whether the pedometer is enabled on the device.

Returns a promise that fulfills with a boolean, indicating whether the pedometer is available on this device.

Pedometer.requestPermissionsAsync()

Asks the user to grant permissions for accessing pedometer.

Pedometer.watchStepCount(callback)

NameTypeDescription
callbackPedometerUpdateCallback

A callback that is invoked when new step count data is available. The callback is provided with a single argument that is PedometerResult.

Subscribe to pedometer updates.

Returns a Subscription that enables you to call remove() when you would like to unsubscribe the listener.

Interfaces

PermissionResponse

An object obtained by permissions get and request functions.

NameTypeDescription
canAskAgainbooleanIndicates if user can be asked again for specific permission. If not, one should be directed to the Settings app in order to enable/disable the permission.
expiresPermissionExpirationDetermines time when the permission expires.
grantedbooleanA convenience boolean that indicates if the permission is granted.
statusPermissionStatusDetermines the status of the permission.

Types

PedometerResult

NameTypeDescription
stepsnumberNumber of steps taken between the given dates.

PedometerUpdateCallback()

Callback function providing event result as an argument.

NameTypeDescription
resultPedometerResult-

PermissionExpiration

Permission expiration time. Currently, all permissions are granted permanently.

Acceptable values are: 'never', number.

Subscription

NameTypeDescription
remove() => voidA method to unsubscribe the listener.

Enums

PermissionStatus

PermissionStatus.DENIED = "denied"

User has denied the permission.

PermissionStatus.GRANTED = "granted"

User has granted the permission.

PermissionStatus.UNDETERMINED = "undetermined"

User hasn't granted or denied the permission yet.

Standalone Applications

You'll need to configure an Android OAuth client for your app on the Google Play console for it to work as a standalone application on the Android platform. See https://developers.google.com/fit/android/get-api-key
  • Message-iconAsk a question on the forums about Pedometer
  • Github-iconView open bug reports for Pedometer
  • Code-iconView source code for Pedometer
  • Build-iconView package in npm Registry
  • Edit-iconEdit this page

Was this doc helpful?