Skip to content

Commit

Permalink
Merge pull request #325 from AaronDsilva97/master
Browse files Browse the repository at this point in the history
Added Resting Heart Rate Support
  • Loading branch information
aboveyunhai authored Nov 5, 2022
2 parents d16015c + deb128d commit 43f8d90
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions android/src/main/java/com/reactnative/googlefit/HealthHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataSet> 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,
Expand Down
9 changes: 9 additions & 0 deletions index.android.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ declare module 'react-native-google-fit' {
options: StartAndEndDate & Partial<BucketOptions>
) => Promise<HeartRateResponse[]>;

/**
* 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<BucketOptions>
) => Promise<HeartRateResponse[]>;

getBloodPressureSamples: (
options: StartAndEndDate & Partial<BucketOptions>
) => Promise<BloodPressureResponse[]>;
Expand Down
14 changes: 14 additions & 0 deletions index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 43f8d90

Please sign in to comment.