This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo 计步器 iconExpo 计步器

提供对设备计步器传感器的访问的库。

Android
iOS
Included in Expo Go
Recommended version:
~58.0.3

expo-sensors 的 Pedometer 在 Android 上使用 hardware.Sensor 系统,在 iOS 上使用 Core Motion 来获取用户的步数,同时还允许你订阅步数更新。

安装

🌐 Installation

Terminal
- npx expo install expo-sensors
- yarn expo install expo-sensors
- pnpm expo install expo-sensors
- bun expo install expo-sensors

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

🌐 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 subscriptionPromise = subscribe(); return () => { subscriptionPromise.then(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> ); } const styles = StyleSheet.create({ container: { flex: 1, marginTop: 15, alignItems: 'center', justifyContent: 'center', }, });

应用接口

🌐 API

import { Pedometer } from 'expo-sensors';

No API data file found, sorry!