Another React Native pedometer bridge? Yes, another React Native pedometer bridge. However, this one is a little different. This one works with iOS and Android using the same API. In the case of iOS, it uses the CMPedometer module. For Android, it uses Google Fit. The API itself is based on the iOS CMPedometer module, so you're able to start tracking steps from a specified time in the past and receive live updates as the number of steps taken increases.
$ npm install react-native-dual-pedometer --save
$ react-native link react-native-dual-pedometer
For iOS, see Additional iOS Installation instructions below
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-dual-pedometer
and addRNDualPedometer.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNDualPedometer.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Run your project (
Cmd+R
)
Add an entry to your Info.plist
, either using XCode or editing the file manually.
Select your Info.plist
file from your application files and add a new entry:
- Property:
Privacy - Motion Usage Description
- Value: (reason for requiring Pedometer permissions)
Add the following to your Info.plist
file:
<dict>
....
<key>NSMotionUsageDescription</key>
<string>(reason for requiring Pedometer permissions)</string>
....
</dict>
- Open up
android/app/src/main/java/[...]/MainActivity.java
- Add
import com.wwdrew.reactnative.RNDualPedometerPackage;
to the imports at the top of the file - Add
new RNDualPedometerPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:include ':react-native-dual-pedometer' project(':react-native-dual-pedometer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-dual-pedometer/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-dual-pedometer')
import RNDualPedometer from 'react-native-dual-pedometer';
There are three functions available:
queryPedometerFromDate(startDate, endDate)
This function takes two parameters, startDate
and endDate
, both ISO8601 date strings, and returns a promise that resolves the following type:
{
startTime: ISO8601String
endTime: ISO8601String
steps: Integer
distance?: Float
averageActivePace?: Float
currentPace?: Float
currentCadence?: Float
}
The last four optional fields are only provided by iOS, if the device being used supports them. At some point I'd like to support these in Android as well, but that's for some point in the future.
startPedometerUpdatesFromDate(startDate)
Takes a single parameter, startDate
as a ISO8601 date string, and
This module would not have been possible without the various other React Native Healthkit and Google Fit bridges available. In particular:
-
react-native-pedometer by mathieudutour
For the initial inspiration that made me think creating my own pedometer bridge was possible.
-
react-native-google-fitness by YBRAIN Inc.
In particular, the GoogleSignInManager. The cleanest and easiest to understand method I've seen for signing in to Google services. Thank you!