expo-recording-steps
v0.1.0
Published
Expo compatible wrapper for Android's Recording API
Maintainers
Readme
expo-recording-api
An Expo module that provides access to Android's Recording API for fitness activity data. This module uses Google Fit's Recording API (not the Sensor API) to track fitness metrics even when your app is closed.
Installation
npx expo install expo-recording-apiRequirements
- Expo SDK 49 or higher
- Android Oreo (8.0) or higher
- iOS not supported (see alternatives below)
- Requires
ACTIVITY_RECOGNITIONpermission - Requires Google Play Services
API
checkSupport()
Checks if the Recording API is supported on the device. Always call this before using other methods.
import RecordingAPI, { ConnectionResult } from 'expo-recording-api';
const result = RecordingAPI.checkSupport();
if (result !== ConnectionResult.SUCCESS) {
console.log('Recording API not supported');
}requestPermission()
Requests the ACTIVITY_RECOGNITION permission required for accessing fitness data.
await RecordingAPI.requestPermission();subscribe(type)
Tells Android to record fitness data of the specified type, even when your app is closed or the device is restarted. You must subscribe before querying data.
import RecordingAPI, { LocalDataType } from 'expo-recording-api';
await RecordingAPI.subscribe(LocalDataType.TYPE_STEP_COUNT_DELTA);Important: Data is only available for the time period after subscribing. If you subscribe and immediately query, you'll get 0 results.
unsubscribe(type)
Stops recording the specified fitness data type. Call this when your app no longer needs to track this data.
await RecordingAPI.unsubscribe(LocalDataType.TYPE_STEP_COUNT_DELTA);getSamples(options)
Retrieves fitness data within a specified time range. Data is only available if previously subscribed and for the time period after subscription.
import RecordingAPI, { LocalDataType } from 'expo-recording-api';
const steps = await RecordingAPI.getSamples({
type: LocalDataType.TYPE_STEP_COUNT_DELTA,
startDate: '2023-01-01T00:00:00.000Z',
endDate: '2023-01-02T00:00:00.000Z',
bucketByTime: [1, 'days']
});Note: The Recording API has a maximum data retention limit of 10 days.
Data Types
TYPE_STEP_COUNT_DELTA: Step countsTYPE_DISTANCE_DELTA: Distance traveledTYPE_CALORIES_EXPENDED: Calories burned
Error Handling
import { RecordingApiError } from 'expo-recording-api';
try {
await RecordingAPI.getSamples({...});
} catch (error) {
if (error instanceof RecordingApiError) {
console.log('Error code:', error.code);
}
}iOS Alternative
For accessing step data on iOS, consider using:
Note: We plan to merge this functionality into expo-sensors in the future.
License
MIT
