Skip to main content

Steps Extraction

ROOK offers the ability to extract steps from Apple Health, Health Connect, and the Android Steps System. This section describes how to achieve this.

Extract steps

It is important to note that the steps are included into the background sync in a specific event that you will receive through your webhook. If you need to extract steps on demand, please follow the next example.

tip

It is important to note that you will receive the steps as a result of the call, and they will also be sent to your webhook.

import React, { Platform } from "react";
import { Text } from "react-native";
import { TouchableWithoutFeedback } from "react-native";
import { StyleSheet, View } from "react-native";
import { useRookVariables, SDKDataSource } from "react-native-rook-sdk";

export const VariableScreen = () => {
const { getTodaySteps } = useRookVariables();

const handleSteps = async (): Promise<void> => {
try {
// If you need to check for Samsung Health use
// useRookPermissions().checkSamsungAvailability()
// This will let you know if the android device supports samsung health.

const source = Platform.OS === 'Android'
? SDKDataSource.HEALTH_CONNECT
: SDKDataSource.APPLE_HEALTH

const count = await getTodaySteps(source);

console.log("steps", count);

} catch (error) {
console.log(error);
}
};

return (
<View>
<TouchableWithoutFeedback onPress={handleSteps}>
<View style={styles.buttonTouch}>
<Text style={styles.buttonText}>Get Today Steps</Text>
</View>
</TouchableWithoutFeedback>
</View>
);
};

const styles = StyleSheet.create({
buttonTouch: {
backgroundColor: "#383A4E",
paddingVertical: 10,
paddingHorizontal: 20,
borderRadius: 8,
marginHorizontal: "5%",
marginTop: 5,
},
buttonText: {
color: "white",
fontSize: 14,
fontWeight: "bold",
textAlign: "center",
},
});

Extract steps from Android Steps System

For Android, we can extract steps from the Android step system. This means the SDK can retrieve step data directly from the device’s accelerometer. It’s important to note that this value is different from the one provided by Health Connect, and that this solution requires a sticky notification to properly track all the user’s steps.

info

This feature requires permissions from the user, so please include a component to request the necessary permissions.

By default the hook start the recollection of steps if you want to stop use disableBackgroundAndroidSteps

Lets follow the next example to include the steps into you app:

/* eslint-disable react-hooks/exhaustive-deps */
import React, { useEffect } from "react";
import { View, Button } from "react-native";
import {
useRookAndroidBackgroundSteps,
useRookPermissions,
} from "react-native-rook-sdk";

export const StepsExample = () => {
const {
ready,
enableBackgroundAndroidSteps,
isStepsAvailable,
syncTodayAndroidStepsCount,
} = useRookAndroidBackgroundSteps();

const {
hasAndroidBackgroundPermissions,
requestAndroidBackgroundPermissions,
} = useRookPermissions();

useEffect(() => {
if (ready) initSteps();
}, [ready]);

const initSteps = async () => {
try {
const isAvailable = await isStepsAvailable();
const hasPermissions = await hasAndroidBackgroundPermissions();

if (isAvailable && !hasPermissions) {
await requestAndroidBackgroundPermissions();
}

await enableBackgroundAndroidSteps();
} catch (error) {
console.log(error);
}
};

const handlePress = async () => {
try {
const hasPermissions = await hasStepsPermissions();

if (!hasPermissions) return;

const androidSteps = await syncTodayAndroidStepsCount();

console.log(androidSteps);
} catch (error) {
console.log(error);
}
};

return (
<View>
<Button title="Get Today Steps Count" onPress={handlePress} />
</View>
);
};

Additional information

Auto start

After a call to enableBackgroundAndroidSteps if the device is restarted the Foreground service will start after the user unlocks their device for the first time (This may vary depending on device brand). This behavior will be stopped when calling disableBackgroundAndroidSteps.

Considerations

The steps service is designed to always be active but there are certain scenarios where the service could not behave as intended:

  • If the user force closes the application from settings, and then restarts their device the service may not be able to restart.
  • The steps are scheduled to be uploaded every hour from the time enableBackgroundAndroidSteps was called, however it's not possible to guarantee the exact execution time as this depends on how the Android System manages the device resources.