This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 55).
Expo 计步器
一个提供访问设备计步器传感器的库。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
Pedometer 来自 expo-sensors,在 Android 上使用系统 hardware.Sensor,在 iOS 上使用 Core Motion 来获取用户的步数,同时也允许你订阅计步器更新。
安装
- npx expo install expo-sensorsIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
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>过去 24 小时内走了多少步:{pastStepCount}</Text> <Text>走起来!看看这个数字上升:{currentStepCount}</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, marginTop: 15, alignItems: 'center', justifyContent: 'center', }, });
API
import { Pedometer } from 'expo-sensors';
Methods
Checks user's permissions for accessing pedometer.
Promise<PermissionResponse>| Parameter | Type | Description |
|---|---|---|
| start | Date | A date indicating the start of the range over which to measure steps. |
| end | Date | A date indicating the end of the range over which to measure steps. |
Get the step count between two dates.
Promise<PedometerResult>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.
Returns whether the pedometer is enabled on the device.
Promise<boolean>Returns a promise that fulfills with a boolean, indicating whether the pedometer is
available on this device.
Asks the user to grant permissions for accessing pedometer.
Promise<PermissionResponse>| Parameter | Type | Description |
|---|---|---|
| callback | PedometerUpdateCallback | A callback that is invoked when new step count data is available. The callback is
provided with a single argument that is |
Subscribe to pedometer updates.
EventSubscriptionReturns a Subscription that enables you to call
remove() when you would like to unsubscribe the listener.
Pedometer updates will not be delivered while the app is in the background. As an alternative, on Android, use another solution based on
Health Connect API. On iOS, thegetStepCountAsyncmethod can be used to get the step count between two dates.
Interfaces
A subscription object that allows to conveniently remove an event listener from the emitter.
Types
| Property | Type | Description |
|---|---|---|
| steps | number | Number of steps taken between the given dates. |
Callback function providing event result as an argument.
| Parameter | Type |
|---|---|
| result | PedometerResult |
void
Literal Type: union
Permission expiration time. Currently, all permissions are granted permanently.
Acceptable values are: 'never' | number
An object obtained by permissions get and request functions.
| Property | Type | Description |
|---|---|---|
| canAskAgain | boolean | 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. |
| expires | PermissionExpiration | Determines time when the permission expires. |
| granted | boolean | A convenience boolean that indicates if the permission is granted. |
| status | PermissionStatus | Determines the status of the permission. |