react-native-rook-health-connect
v0.6.3
Published
test
Downloads
33
Maintainers
Readme
SDK Health Connect in React Native
Steps to integrate Health Connect Data extraction and transmission into your app.
Content
Installation
To build a project using the Rook Health Connect in React native you need to use at least react v16 and react native v65. This SDK is only available on Android this means that it won't work with iOS.
- Install the dependencies
npm
npm i react-native-rook-health-connect
yarn
yarn add react-native-rook-health-connect
Configuration
Add your client uuid in order to be authorized, follow the next example, add at the top level of your tree components the RookConnectProvider, password refers to the secret key.
import {RookConnectProvider} from 'rook_auth';
const App => () {
return (
<RookConnectProvider
keys={{
clientUUID: 'YOUR-CLIENT-UUID',
environment: "sandbox | production",
password: 'YOUR-PASSWORD',
}}>
<YourComponents />
</RookConnectProvider>
);
}
Then we need to configure the android project. open the android project inside android studio. We need to modify the AndroidManifest.xml
file at the top level paste the next permissions in order to access to the Health connection records.
We need to add inside your activity tag an intent filter to open Health Connect APP
<intent-filter>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
</intent-filter>
Your AndroidManifest.xml
file should look like this
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<application
...>
<activity
...>
<intent-filter>
...
</intent-filter>
<!-- For supported versions through Android 13, create an activity to show the rationale
of Health Connect permissions once users click the privacy policy link. -->
<intent-filter>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
</intent-filter>
</activity>
<!-- For versions starting Android 14, create an activity alias to show the rationale
of Health Connect permissions once users click the privacy policy link. -->
<activity-alias
android:name="ViewPermissionUsageActivity"
android:exported="true"
android:permission="android.permission.START_VIEW_PERMISSION_USAGE"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW_PERMISSION_USAGE" />
<category android:name="android.intent.category.HEALTH_PERMISSIONS" />
</intent-filter>
</activity-alias>
</application>
</manifest>
Into the same file we need to set the minSdkVersion
and targetSdkVerion
android {
...
minSdkVersion: 26
targetSdkVersion 33
...
}
Package usage
useRookHCBody
Definition
If you need more details about BodySummary please use right click an Go to definition to se the whole definition
type BodyProps = {
userID: string;
};
const useRookHCBody: ({ userID }: BodyProps) => RookHCBody;
interface RookHCBody {
getBodySummaryLastDate: () => Promise<string>;
hasBodyPermissions: () => Promise<boolean>;
requestBodyPermissions: () => Promise<void>;
getBodySummary: (date: string) => Promise<BodySummary>;
}
getBodySummaryLastDate
: Check the last date when you fetch data of body datahasBodyPermissions
: Return a boolean showing if body data are allowedrequestBodyPermissions
: Request permissions only for body datagetBodySummary
: Fetch body summary, the date should be in format YYYY-MM-DD
NOTE: The date should be formatted as YYYY-MM-DD
Example
import React, { useState } from 'react';
import { View, Text, Button, TextInput } from 'react-native';
import { useRookHCBody } from 'rook_health_connect';
export const BodyExample = () => {
const [date, setDate] = useState('');
const [data, setData] = useState('{}');
const {
getBodySummaryLastDate,
hasBodyPermissions,
requestBodyPermissions,
getBodySummary,
} = useRookHCBody({
userID: 'YOUR-USER-ID',
});
const handleLastDate = async (): Promise<void> => {
try {
const result = await getBodySummaryLastDate();
setData(result);
} catch (error) {
setData(`${error}`);
}
};
const handlePermissions = async (): Promise<void> => {
try {
const result = await hasBodyPermissions();
setData(`hasPermissions ${result}`);
} catch (error) {
setData(`${error}`);
}
};
const handleRequestPermissions = async (): Promise<void> => {
try {
await requestBodyPermissions();
} catch (error) {
setData(`${error}`);
}
};
const handleSummary = async (): Promise<void> => {
try {
// The date should be in the format YYYY-MM-DD
const r = await getBodySummary(date);
setData(JSON.stringify(r));
} catch (error) {
setData(`${error}`);
}
};
return (
<View>
<Text>body</Text>
<TextInput
placeholder="YYYY-MM-DD"
onChangeText={(text) => setDate(text)}
/>
<Button title="last Date" onPress={handleLastDate} />
<Button title="hasAllPermissions" onPress={handlePermissions} />
<Button
title="requestAllPermissions"
onPress={handleRequestPermissions}
/>
<Button title="get summary" onPress={handleSummary} />
<Text>{data}</Text>
</View>
);
};
useRookHCPermissions
Definition
type PermissionsProps = {
userID: string;
};
const useRookHCPermissions: ({ userID }: PermissionsProps) => RookHCPermissions;
interface RookHCPermissions {
checkAvailability: () => Promise<AvailabilityStatus>;
getUserTimeZone: () => Promise<UserTimeZone>;
hasAllPermissions: () => Promise<boolean>;
requestPermissions: () => Promise<void>;
openHealthConnectSettings: () => Promise<void>;
}
type AvailabilityStatus = 'INSTALLED' | 'NOT_INSTALLED' | 'NOT_SUPPORTED';
checkAvailability
: Checks if the health connect service is supported
Return
AvailabilityStatus
means if health connect services are available in the deviceINSTALLED
means health connect services are availableNOT_INSTALLED
means health connect services are available but is necessary to install the health connect app into the deviceNOT_SUPPORTED
means health connect services are not available
getUserTimeZone
: Get the user time zonehasAllPermissions
: Check if the you already request all health connect permissionsrequestPermissions
: Request all health connect permissionsopenHealthConnectSettings
: Open the health connect settings
Example
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';
import { useRookHCPermissions } from 'rook_health_connect';
export const PermissionsExample = () => {
const [data, setData] = useState('');
const {
checkAvailability,
hasAllPermissions,
requestPermissions,
openHealthConnectSettings,
} = useRookHCPermissions({ userID: 'YOUR-USER-ID' });
const handleAvailability = async (): Promise<void> => {
try {
const result = await checkAvailability();
setData(result);
} catch (error) {
setData(`${error}`);
}
};
const handlePermissions = async (): Promise<void> => {
try {
const result = await hasAllPermissions();
setData(`has permissions ${result}`);
} catch (error) {
setData(`${error}`);
}
};
const handleRequestPermissions = async (): Promise<void> => {
try {
await requestPermissions();
} catch (error) {
setData(`${error}`);
}
};
const handleOpen = async (): Promise<void> => {
try {
await openHealthConnectSettings();
} catch (error) {
setData(`${error}`);
}
};
return (
<View>
<Text>Hola</Text>
<Button title="Availability" onPress={handleAvailability} />
<Button title="hasAllPermissions" onPress={handlePermissions} />
<Button
title="requestAllPermissions"
onPress={handleRequestPermissions}
/>
<Button title="openHC" onPress={handleOpen} />
<Text>{data}</Text>
</View>
);
};
useRookHCPhysical
Definition
If you need more details about PhysicalSummary or PhysicalEvents please use right click an Go to definition to se the whole definition
type PhysicalProps = {
userID: string;
};
const useRookHCPhysical: ({ userID }: PhysicalProps) => RookHCPhysical;
interface RookHCPhysical {
getPhysicalSummaryLastDate: () => Promise<string>;
getPhysicalEventsLastDate: () => Promise<string>;
hasPhysicalPermissions: () => Promise<boolean>;
requestPhysicalPermissions: () => Promise<void>;
getPhysicalSummary: (date: string) => Promise<PhysicalSummary>;
getPhysicalEvents: (date: string) => Promise<PhysicalEvent[]>;
}
getPhysicalSummaryLastDate
: Check the last date when you fetch data of physical summarygetPhysicalEventsLastDate
: Check the last date when you fetch data of physical eventshasPhysicalPermissions
: Return a boolean showing if physical data are allowedrequestPhysicalPermissions
: Request permissions only for physical datagetPhysicalSummary
: Fetch physical summary, the date should be in format YYYY-MM-DDgetPhysicalEvents
: Fetch physical events, the date should be in format YYYY-MM-DD
NOTE: The date should be formatted as YYYY-MM-DD
Example
import React, { useState } from 'react';
import { View, Text, Button, TextInput } from 'react-native';
import { useRookHCPhysical } from 'rook_health_connect';
export const PhysicalView = () => {
const [data, setData] = useState('{}');
const [date, setDate] = useState('');
const {
getPhysicalSummaryLastDate,
getPhysicalEventsLastDate,
hasPhysicalPermissions,
requestPhysicalPermissions,
getPhysicalSummary,
getPhysicalEvents,
} = useRookHCPhysical({ userID: 'YOUR-USER-ID' });
const handleLastDate = async (): Promise<void> => {
try {
const result = await getPhysicalSummaryLastDate();
setData(result);
} catch (error) {
setData(`${error}`);
}
};
const handleLastDateEvent = async (): Promise<void> => {
try {
const result = await getPhysicalEventsLastDate();
setData(result);
} catch (error) {
setData(`${error}`);
}
};
const handlePermissions = async (): Promise<void> => {
try {
const result = await hasPhysicalPermissions();
setData(`hasPermissions ${result}`);
} catch (error) {
setData(`${error}`);
}
};
const handleRequestPermissions = async (): Promise<void> => {
try {
await requestPhysicalPermissions();
} catch (error) {
setData(`${error}`);
}
};
const handleSummary = async (): Promise<void> => {
try {
const r = await getPhysicalSummary(date);
setData(JSON.stringify(r));
} catch (error) {
setData(`${error}`);
}
};
const handleEventSummary = async (): Promise<void> => {
try {
const r = await getPhysicalEvents(date);
setData(JSON.stringify(r));
} catch (error) {
setData(`${error}`);
}
};
return (
<View>
<Text>Physical</Text>
<TextInput
placeholder="YYYY-MM-DD"
onChangeText={(text) => setDate(text)}
/>
<Button title="last Date" onPress={handleLastDate} />
<Button title="last Date event" onPress={handleLastDateEvent} />
<Button title="hasAllPermissions" onPress={handlePermissions} />
<Button
title="requestAllPermissions"
onPress={handleRequestPermissions}
/>
<Button title="get summary" onPress={handleSummary} />
<Button title="get summary event" onPress={handleEventSummary} />
<Text>{data}</Text>
</View>
);
};
useRookHCSleep
Definition
If you need more details about SleepSummary please use right click an Go to definition to se the whole definition
type SleepProps = {
userID: string;
};
const useRookHCSleep: ({ userID }: SleepProps) => RookHCSleep;
interface RookHCSleep {
getSleepSummaryLastDate: () => Promise<string>;
hasSleepPermissions: () => Promise<boolean>;
requestSleepPermissions: () => Promise<void>;
getSleepSummary: (date: string) => Promise<SleepSummary>;
}
getSleepSummaryLastDate
: Check the last date when you fetch data of sleep datahasSleepPermissions
: Return a boolean showing if sleep data are allowedrequestSleepPermissions
: Request permissions only for sleep datagetSleepSummary
: Fetch sleep summary, the date should be in format YYYY-MM-DD
NOTE: The date should be formatted as YYYY-MM-DD
Example
import React, { useState } from 'react';
import { View, Text, Button, TextInput } from 'react-native';
import { useRookHCSleep } from 'rook_health_connect';
export const SleepView = () => {
const [date, setDate] = useState('');
const [data, setData] = useState('{}');
const {
getSleepSummaryLastDate,
hasSleepPermissions,
requestSleepPermissions,
getSleepSummary,
} = useRookHCSleep({ userID: 'YOUR-USER-ID' });
const handleLastDate = async (): Promise<void> => {
try {
const result = await getSleepSummaryLastDate();
setData(result);
} catch (error) {
setData(`${error}`);
}
};
const handlePermissions = async (): Promise<void> => {
try {
const result = await hasSleepPermissions();
setData(`hasPermissions ${result}`);
} catch (error) {
setData(`${error}`);
}
};
const handleRequestPermissions = async (): Promise<void> => {
try {
await requestSleepPermissions();
} catch (error) {
setData(`${error}`);
}
};
const handleSummary = async (): Promise<void> => {
try {
const r = await getSleepSummary(date);
setData(JSON.stringify(r));
} catch (error) {
setData(`${error}`);
}
};
return (
<View>
<Text>sleep</Text>
<TextInput
placeholder="YYYY-MM-DD"
onChangeText={(text) => setDate(text)}
/>
<Button title="last Date" onPress={handleLastDate} />
<Button title="hasAllPermissions" onPress={handlePermissions} />
<Button
title="requestAllPermissions"
onPress={handleRequestPermissions}
/>
<Button title="get summary" onPress={handleSummary} />
<Text>{data}</Text>
</View>
);
};
useRookHCEvents
Definition
If you need more details about the data that returns each event please use right click an Go to definition to se the whole definition
type EventsProps = {
userID: string;
};
const useRookHCEvents: ({ userID }: EventsProps) => RookHCEvents;
interface RookHCEvents {
getBodyBloodGlucoseEvents: (date: string) => Promise<HCBloodGlucoseEvent>;
getBodyBloodPressureEvents: (date: string) => Promise<HCBloodPressureEvent>;
getBodyMetricsEvents: (date: string) => Promise<HCBodyMetricsEvent>;
getBodyHeartRateEvents: (date: string) => Promise<HCHeartRateEvent>;
getBodyHydrationEvents: (date: string) => Promise<HCHydrationEvent>;
getBodyNutritionEvents: (date: string) => Promise<HCNutritionEvent>;
getBodyOxygenationEvents: (date: string) => Promise<HCOxygenationEvent>;
getBodyTemperatureEvents: (date: string) => Promise<HCTemperatureEvent>;
}
getBodyBloodGlucoseEvents
: Fetch events of blood glucose, the date should be in format YYYY-MM-DDgetBodyBloodPressureEvents
: Fetch events of blood pressure, the date should be in format YYYY-MM-DDgetBodyMetricsEvents
: Fetch events of body metrics like height, weight, bmi and others, the date should be in format YYYY-MM-DDgetBodyHeartRateEvents
: Fetch events of heart rate, the date should be in format YYYY-MM-DDgetBodyHydrationEvents
: Fetch events of hydration, the date should be in format YYYY-MM-DDgetBodyNutritionEvents
: Fetch events of nutrition like calories, protein, fat, alcohol in take, the date should be in format YYYY-MM-DDgetBodyOxygenationEvents
: Fetch events of oxygenation, the date should be in format YYYY-MM-DDgetBodyTemperatureEvents
: Fetch events of body temperature, the date should be in format YYYY-MM-DD
NOTE: The date should be formatted as YYYY-MM-DD
Example
import React, { useState } from 'react';
import { Text, Button, ScrollView } from 'react-native';
import { useRookHCEvents } from 'react-native-rook-health-connect';
export const EventsView = () => {
const {
getBodyBloodGlucoseEvents,
getBodyBloodPressureEvents,
getBodyMetricsEvents,
getBodyHeartRateEvents,
getBodyHydrationEvents,
getBodyNutritionEvents,
getBodyOxygenationEvents,
getBodyTemperatureEvents,
} = useRookHCEvents({ userID: 'YOUR-USER-ID' });
const [date, setDate] = useState('');
const handlePress = async (): Promise<void> => {
try {
const result = await getBodyBloodGlucoseEvents(date);
console.log(result);
} catch (error) {
console.log(error);
}
};
const handlePressureEvent = async (): Promise<void> => {
try {
const result = await getBodyBloodPressureEvents(date);
console.log(result);
} catch (error) {
console.log(error);
}
};
const handleBodyMetricsEvent = async (): Promise<void> => {
try {
const result = await getBodyMetricsEvents(date);
console.log(result);
} catch (error) {
console.log(error);
}
};
const handleBodyHeartRateEvents = async (): Promise<void> => {
try {
const result = await getBodyHeartRateEvents(date);
console.log(result);
} catch (error) {
console.log(error);
}
};
const handleBodyHydrationEvents = async (): Promise<void> => {
try {
const result = await getBodyHydrationEvents(date);
console.log(result);
} catch (error) {
console.log(error);
}
};
const handleBodyNutritionEvents = async (): Promise<void> => {
try {
const result = await getBodyNutritionEvents(date);
console.log(result);
} catch (error) {
console.log(error);
}
};
const handleBodyOxygenationEvents = async (): Promise<void> => {
try {
const result = await getBodyOxygenationEvents(date);
console.log(result);
} catch (error) {
console.log(error);
}
};
const handlePhysicalTemperatureEvents = async (): Promise<void> => {
try {
const result = await getBodyTemperatureEvents(date);
console.log(result);
} catch (error) {
console.log(error);
}
};
return (
<ScrollView>
<Text>Events</Text>
<TextInput
placeholder="YYYY-MM-DD"
onChangeText={(text) => setDate(text)}
/>
<Button title="Blood Glucose Event" onPress={handlePress} />
<Button title="Blood Pressure Event" onPress={handlePressureEvent} />
<Button title="Body Metrics Event" onPress={handleBodyMetricsEvent} />
<Button
title="Body HeartRate Event"
onPress={handleBodyHeartRateEvents}
/>
<Button title="Hydration Events" onPress={handleBodyHydrationEvents} />
<Button title="Nutrition Events" onPress={handleBodyNutritionEvents} />
<Button
title="Oxygenation Events"
onPress={handleBodyOxygenationEvents}
/>
<Button
title="Temperature Events"
onPress={handlePhysicalTemperatureEvents}
/>
</ScrollView>
);
};