HomeGuidesReferenceLearn

Expo Pedometer iconExpo Pedometer

GitHub

npm


Pedometer from expo-sensors uses the system hardware.Sensor on Android and Core Motion on iOS to get the user's step count, and also allows you to subscribe to pedometer updates.

Platform Compatibility

Android DeviceAndroid EmulatoriOS DeviceiOS SimulatorWeb

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 { useState, useEffect } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Pedometer } from 'expo-sensors';

export default function App() {
  const [isPedometerAvailable, setIsPedometerAvailable] = useState('checking');
  const [pastStepCount, setPastStepCount] = useState(0);
  const [currentStepCount, setCurrentStepCount] = useState(0);

  const subscribe = async () => {
    const isAvailable = await Pedometer.isAvailableAsync();
    setIsPedometerAvailable(String(isAvailable));

    if (isAvailable) {
      const end = new Date();
      const start = new Date();
      start.setDate(end.getDate() - 1);

      const pastStepCountResult = await Pedometer.getStepCountAsync(start, end);
      if (pastStepCountResult) {
        setPastStepCount(pastStepCountResult.steps);
      }

      return Pedometer.watchStepCount(result => {
        setCurrentStepCount(result.steps);
      });
    }
  };

  useEffect(() => {
    const subscription = subscribe();
    return () => subscription && subscription.remove();
  }, []);

  return (
    <View style={styles.container}>
      <Text>Pedometer.isAvailableAsync(): {isPedometerAvailable}</Text>
      <Text>Steps taken in the last 24 hours: {pastStepCount}</Text>
      <Text>Walk! And watch this go up: {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()

Returns

  • Promise<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


Returns a promise that fulfills with a PedometerResult.

As Apple documentation states:

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


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

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


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.

PermissionResponse Properties

NameTypeDescription
canAskAgainboolean

Indicates 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.

expiresPermissionExpiration

Determines time when the permission expires.

grantedboolean

A convenience boolean that indicates if the permission is granted.

statusPermissionStatus

Determines the status of the permission.


Types

PedometerResult

NameTypeDescription
stepsnumber

Number of steps taken between the given dates.

PermissionExpiration

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

Acceptable values are: 'never', number.

Subscription

NameTypeDescription
remove() => void-

Enums

PermissionStatus

DENIED

PermissionStatus.DENIED = "denied"

User has denied the permission.

GRANTED

PermissionStatus.GRANTED = "granted"

User has granted the permission.

UNDETERMINED

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.