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 from Apple Health

It is important to note that the steps are included into the include the background sync of iOS 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 from "react";
import { Text } from "react-native";
import { TouchableWithoutFeedback } from "react-native";
import { StyleSheet, View } from "react-native";
import { useRookAppleHealthVariables } from "react-native-rook-sdk";

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

const handleSteps = async (): Promise<void> => {
try {
const count = await getTodaySteps();
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 Health and Android Steps System

For Android, we have two options to extract steps: one is from Health Connect, and the other is from the Android Steps System. The main difference is that Health Connect collects steps from many sources that insert into it, while the Android Steps System is the default system on all devices that collects steps.

Another important consideration is that the Android Steps System is the only kit that allows background sync. If you are interested in having background functionality, please note that you will receive the step count from the Android Steps System, and the webhook will notify you every hour.

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 } from "react-native-rook-sdk";

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

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

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

if (hasPermissions) await enableBackgroundAndroidSteps();

if (isAvailable && !hasPermissions) {
await requestStepsPermissions();

// This step is only necessary if you to automatically start the background process
// You could enable too as a onPress function
await pollingPermissionsToEnableBackgroundSync();
}
} catch (error) {
console.log(error);
}
};

// The `pollingPermissionsToEnableBackgroundSync` function is continuously checking if the user has
// enabled background permissions for accessing steps data from the Android Steps System. It uses a
// while loop to repeatedly call the `hasStepsPermissions` function until the `hasEnabledBackground`
// variable becomes true, indicating that the user has granted the necessary permissions. Once the
// user has enabled background permissions, the function then calls `enableBackgroundAndroidSteps` to
// activate background sync for accessing step counts.
const pollingPermissionsToEnableBackgroundSync = async () => {
let hasEnabledBackground = false;

while (!hasEnabledBackground) {
hasEnabledBackground = await hasStepsPermissions();
}

await enableBackgroundAndroidSteps();
};

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

if (!hasPermissions) return;

const healthConnectSteps = await syncTodayHealthConnectStepsCount();
const androidSteps = await syncTodayAndroidStepsCount();

console.log({ healthConnectSteps, 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.