A React Native module to interact with Apple Healthkit and Google Fit.
react-native-fitness
is a library that works on both iOS
and Android
with it you can interact with Apple Healthkit and Google Fit.
Currently the lib provides a set of API that you can use to read steps count or distance count for a given period of time.
Note: We're open to receive PRs that contains new features or improvements, so feel free to contribute to this repo.
npm install @ovalmoney/react-native-fitness --save
or
yarn add @ovalmoney/react-native-fitness
react-native link @ovalmoney/react-native-fitness
Podfile
.
pod 'react-native-fitness', :path => '../node_modules/@ovalmoney/react-native-fitness'`
pod install
in your iOS project directory.Build Phases
➜ Link Binary With Libraries
and add libreact-native-fitness.a
.Info.plist
in order to ask permissions.
<key>NSHealthShareUsageDescription</key>
<string>Read and understand health data.</string>
Libraries
➜ Add Files to [your project's name]
node_modules
➜ @ovalmoney
➜ react-native-fitness
and select RNFitness.xcodeproj
Build Phases
➜ Link Binary With Libraries
and add libRNFitness.a
.Cmd+R
)In order to make it run, it is necessary to turn on Health Kit
in the Capabilities
.
MainApplication.java
import com.ovalmoney.fitness.RNFitnessPackage;
to the imports at the top of the filenew RNFitnessPackage()
to the list returned by the getPackages()
methodandroid/settings.gradle
:
include ':@ovalmoney_react-native-fitness'
project(':@ovalmoney_react-native-fitness').projectDir = new File(rootProject.projectDir, '../node_modules/@ovalmoney/react-native-fitness/android')
android/app/build.gradle
:
compile project(':@ovalmoney_react-native-fitness')
android/build.gradle
:
// Other build versions
fitnessPlayServices: "<Your version>" // default: 17.0.0
authPlayServices: "<Your version>" // default: 17.0.0
}
import Fitness from '@ovalmoney/react-native-fitness';
const permissions = [
{ kind: Fitness.PermissionKinds.Steps, access: Fitness.PermissionAccesses.Write },
];
Fitness.isAuthorized(permissions)
.then((authorized) => {
// Do something
})
.catch((error) => {
// Do something
});
Fitness.isAuthorized([{ kind: int, access: int }])
Check if permissions are granted or not. It works on Android and iOS >= 12.0, while it returns an error when iOS < 12.
It requires an Array
of Object
with a mandatory key kind
and an optional key access
.
Possible values for the keys can be found in PermissionKinds
and PermissionAccesses
under Attributes
section.
On iOS at least one permissions with Read
access must be provided, otherwise an errorEmptyPermissions
will be thrown.
Fitness.requestPermissions([{ kind: int, access: int }])
Ask permission and return if user granted or not(Android), while, due to Apple's privacy model, always true is returned in iOS.
It requires an Array
of Object
with a mandatory key kind
and an optional key access
.
Possible values for the keys can be found in PermissionKinds
and PermissionAccesses
under Attributes
section.
On iOS at least one permissions with Read
access must be provided, otherwise an errorEmptyPermissions
will be thrown.
Fitness.logout()
Available only on android. It performs a logout from google account.
It returns true
for a successful logout, false
if user cancel action.
Fitness.disconnect()
Available only on android. It performs a disconnect action from Google Fit.
It returns true
for a successful logout, false
if user cancel action.
Fitness.getSteps({ startDate: string, endDate: string, interval: string })
Fetch steps on a given period of time. It requires an Object
with startDate
and endDate
attributes as string. If startDate is not provided an error will be thrown. Set interval
to decide how detailed the returned data is, set it to hour
or minute
otherwise it defaults to days
.
Fitness.getDistances({ startDate: string, endDate: string, interval: string })
Fetch distance in meters on a given period of time. It requires an Object
with startDate
and endDate
attributes as string. If startDate is not provided an error will be thrown. Set interval
to decide how detailed the returned data is, set it to hour
or minute
otherwise it defaults to days
.
Fitness.getCalories({ startDate: string, endDate: string, interval: string })
Fetch calories burnt in kilocalories on a given period of time. It requires an Object
with startDate
and endDate
attributes as string. If startDate is not provided an error will be thrown. Set interval
to decide how detailed the returned data is, set it to hour
or minute
otherwise it defaults to days
.
Fitness.getHeartRate({ startDate: string, endDate: string, interval: string })
Fetch heart rate bpm on a given period of time. It requires an Object
with startDate
and endDate
attributes as string. If startDate is not provided an error will be thrown. Set interval
to decide how detailed the returned data is, set it to hour
or minute
otherwise it defaults to days
.
Fitness.getSleepAnalysis({ startDate: string, endDate: string })
Fetch sleep analysis data on a given period of time. It requires an Object
with startDate
and endDate
attributes as string. If startDate is not provided an error will be thrown.
Fitness.subscribeToSteps()
Available only on android. Subscribe only to steps from the Google Fit store. It returns a promise with true
for a successful subscription and false
otherwise.
Call this function to get steps and eliminate the need to have Google Fit installed on the device.
Return the used provider.
Return the information of what kind of Permission can be asked. At the moment the list of possible kinds is:
Steps
Distances
Calories
Heart rate
Activity
(only Android)Sleep Analysis
Return the information of what kind of Access can be asked. The list of possible kinds is:
Read
Write
Return the list of meaningful errors that can be possible thrown.
Possible values are:
isAuthorized
is called on iOS < 12.0Possible values are:
getSleepAnalysis
is called on Android less than N