From deb128d85d297b622d72f6408c251e4db7e05194 Mon Sep 17 00:00:00 2001 From: Aaron Dsilva Date: Sat, 15 Oct 2022 11:16:17 +0530 Subject: [PATCH] Added Resting Heart Rate Support --- README.md | 3 ++ .../googlefit/GoogleFitModule.java | 16 ++++++++ .../reactnative/googlefit/HealthHistory.java | 37 +++++++++++++++++++ index.android.d.ts | 9 +++++ index.android.js | 14 +++++++ 5 files changed, 79 insertions(+) diff --git a/README.md b/README.md index 56ac74da..604e3e24 100755 --- a/README.md +++ b/README.md @@ -288,6 +288,9 @@ async function fetchData() { const heartrate = await GoogleFit.getHeartRateSamples(opt); console.log(heartrate); + const restingheartrate = await GoogleFit.getRestingHeartRateSamples(opt); + console.log(restingheartrate); + const bloodpressure = await GoogleFit.getBloodPressureSamples(opt); console.log(bloodpressure); } diff --git a/android/src/main/java/com/reactnative/googlefit/GoogleFitModule.java b/android/src/main/java/com/reactnative/googlefit/GoogleFitModule.java index 816b09be..8c2cfe2c 100644 --- a/android/src/main/java/com/reactnative/googlefit/GoogleFitModule.java +++ b/android/src/main/java/com/reactnative/googlefit/GoogleFitModule.java @@ -466,6 +466,22 @@ public void getHeartRateSamples(double startDate, } } + @ReactMethod + public void getRestingHeartRateSamples(double startDate, + double endDate, + int bucketInterval, + String bucketUnit, + Promise promise) { + + try { + HealthHistory healthHistory = mGoogleFitManager.getHealthHistory(); + healthHistory.setDataType(DataType.TYPE_HEART_RATE_BPM); + promise.resolve(healthHistory.getRestingHeartRateHistory((long)startDate, (long)endDate, bucketInterval, bucketUnit)); + } catch (IllegalViewOperationException e) { + promise.reject(e); + } + } + @ReactMethod public void getHydrationSamples(double startDate, double endDate, diff --git a/android/src/main/java/com/reactnative/googlefit/HealthHistory.java b/android/src/main/java/com/reactnative/googlefit/HealthHistory.java index 3b4c5499..94698af0 100644 --- a/android/src/main/java/com/reactnative/googlefit/HealthHistory.java +++ b/android/src/main/java/com/reactnative/googlefit/HealthHistory.java @@ -102,6 +102,43 @@ else if (dataReadResult.getDataSets().size() > 0) { return map; } + public ReadableArray getRestingHeartRateHistory(long startTime, long endTime, int bucketInterval, String bucketUnit) { + DataReadRequest.Builder readRequestBuilder = new DataReadRequest.Builder() + .aggregate(new DataSource.Builder() + .setType(DataSource.TYPE_DERIVED) + .setDataType(DataType.TYPE_HEART_RATE_BPM) + .setAppPackageName("com.google.android.gms") + .setStreamName("resting_heart_rate<-merge_heart_rate_bpm") + .build()) + .read(this.dataType) + .bucketByTime(1, TimeUnit.DAYS) + .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS); + + + DataReadRequest readRequest = readRequestBuilder.build(); + + DataReadResult dataReadResult = Fitness.HistoryApi.readData(googleFitManager.getGoogleApiClient(), readRequest).await(1, TimeUnit.MINUTES); + + WritableArray map = Arguments.createArray(); + + //Used for aggregated data + if (dataReadResult.getBuckets().size() > 0) { + for (Bucket bucket : dataReadResult.getBuckets()) { + List dataSets = bucket.getDataSets(); + for (DataSet dataSet : dataSets) { + processDataSet(dataSet, map); + } + } + } + //Used for non-aggregated data + else if (dataReadResult.getDataSets().size() > 0) { + for (DataSet dataSet : dataReadResult.getDataSets()) { + processDataSet(dataSet, map); + } + } + return map; + } + public boolean saveBloodGlucose(ReadableMap sample) { this.Dataset = createDataForRequest( this.dataType, diff --git a/index.android.d.ts b/index.android.d.ts index e425c2fb..5163e03c 100644 --- a/index.android.d.ts +++ b/index.android.d.ts @@ -111,6 +111,15 @@ declare module 'react-native-google-fit' { options: StartAndEndDate & Partial ) => Promise; + /** + * Query for getting resting heart rate samples. the options object is used to setup a query to retrieve relevant samples. + * @param {Object} options getRestingHeartRateSamples accepts an options object startDate: ISO8601Timestamp and endDate: ISO8601Timestamp. + * + */ + getRestingHeartRateSamples: ( + options: StartAndEndDate & Partial + ) => Promise; + getBloodPressureSamples: ( options: StartAndEndDate & Partial ) => Promise; diff --git a/index.android.js b/index.android.js index eafcc6b7..de5eae69 100644 --- a/index.android.js +++ b/index.android.js @@ -567,6 +567,20 @@ class RNGoogleFit { return result; } + getRestingHeartRateSamples = async (options) => { + const { startDate, endDate, bucketInterval, bucketUnit } = prepareInput(options); + const result = await googleFit.getRestingHeartRateSamples( + startDate, + endDate, + bucketInterval, + bucketUnit + ); + if (result.length > 0) { + return prepareResponse(result, 'value'); + } + return result; + } + getBloodPressureSamples = async (options, callback) => { const { startDate, endDate, bucketInterval, bucketUnit } = prepareInput(options); const result = await googleFit.getBloodPressureSamples(