问题
I have tried to read Blood pressure value from Google Fit using Android code but unable to read value and returns the below logs
Blood Pressure Log:
Number of returned buckets of DataSets is: 1
Datasets: [DataSet{d:blood_pressure.summary:gms:aggregated []}]
Name: com.google.blood_pressure.summary
Fields: [blood_pressure_systolic_average(f),blood_pressure_systolic_max(f), blood_pressure_systolic_min(f), blood_pressure_diastolic_average(f), blood_pressure_diastolic_max(f), blood_pressure_diastolic_min(f), body_position(i), blood_pressure_measurement_location(i)]
Data Point Values : []
I have checked google fit and it has the entry of blood pressure and heart reate and able to read the Heart rate values.
Heart Rate Log:
Number of returned buckets of DataSets is: 1 Datasets: [DataSet{d:heart_rate.summary:gms:aggregated [RawDataPoint{[81.0, 81.0, 81.0]@1531305031000000000, 1531305031000000000}]}] Name: com.google.heart_rate.summary Fields: [average(f), max(f), min(f)]
Data Point Values :[DataPoint{[81.0, 81.0, 81.0]@[1531305031000000000, 1531305031000000000,raw=0,insert=0](d:heart_rate.summary:gms:aggregated r:heart_rate.bpm:iHealthMyVitals.V2:ContentValues - bp rate count)}]
Data Point: Type: com.google.heart_rate.summary Start: 4:00:31 PM End: 4:00:31 PM Field: average, Value : 81.0 Field: max, Value : 81.0 Field: min, Value : 81.0
Here is my full code :
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mButton;
private static final int REQUEST_OAUTH_REQUEST_CODE = 1;
public static final String TAG = "TEST";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.button_fetch);
mButton.setOnClickListener(this);
FitnessOptions fitnessOptions =
FitnessOptions.builder()
.addDataType(DataType.AGGREGATE_HEART_RATE_SUMMARY, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_HEART_RATE_BPM, FitnessOptions.ACCESS_READ)
.addDataType(HealthDataTypes.TYPE_BLOOD_PRESSURE, FitnessOptions.ACCESS_READ)
.addDataType(HealthDataTypes.AGGREGATE_BLOOD_PRESSURE_SUMMARY, FitnessOptions.ACCESS_READ)
.build();
if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
GoogleSignIn.requestPermissions(
this,
REQUEST_OAUTH_REQUEST_CODE,
GoogleSignIn.getLastSignedInAccount(this),
fitnessOptions);
Log.v(TAG, "Permission failed");
} else {
Log.v(TAG, "Permission granted");
}
}
private Task<DataReadResponse> readHRHistoryData() {
DataReadRequest readRequest = queryHRFitnessData();
// Invoke thex History API to fetch the data with the query
return Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.readData(readRequest)
.addOnSuccessListener(
new OnSuccessListener<DataReadResponse>() {
@Override
public void onSuccess(DataReadResponse dataReadResponse) {
Log.v(TAG, "Success" + dataReadResponse);
printData(dataReadResponse);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.v(TAG, "Failture");
}
});
}
public static DataReadRequest queryHRFitnessData() {
// [START build_read_data_request]
// Setting a start and end date using a range of 1 week before this moment.
Calendar cal = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
Date now = new Date();
cal1.setTime(now);
long endTime = cal1.getTimeInMillis();
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, cal1.get(Calendar.YEAR));
c.set(Calendar.MONTH, cal1.get(Calendar.MONTH));
c.set(Calendar.DAY_OF_MONTH, cal1.get(Calendar.DAY_OF_MONTH));
c.set(Calendar.AM_PM, 00);
c.set(Calendar.HOUR, 00);
c.set(Calendar.MINUTE, 00);
c.set(Calendar.SECOND, 00);
c.set(Calendar.MILLISECOND, 00);
long startTime = c.getTimeInMillis();
java.text.DateFormat dateFormat = getDateInstance();
DataReadRequest readRequest =
new DataReadRequest.Builder()
//.aggregate(DataType.TYPE_HEART_RATE_BPM, DataType.AGGREGATE_HEART_RATE_SUMMARY)
.aggregate(HealthDataTypes.TYPE_BLOOD_PRESSURE, HealthDataTypes.AGGREGATE_BLOOD_PRESSURE_SUMMARY)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
return readRequest;
}
public static void printData(DataReadResponse dataReadResult) {
// If the DataReadRequest object specified aggregated data, dataReadResult will be returned
// as buckets containing DataSets, instead of just DataSets.
Log.v(TAG, "Number of returned buckets of DataSets is: " + dataReadResult.getBuckets().size());
if (dataReadResult.getBuckets().size() > 0) {
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
Log.v(TAG, "Datasets: " + dataSets);
for (DataSet dataSet : dataSets) {
dumpDataSet(dataSet);
}
}
} else if (dataReadResult.getDataSets().size() > 0) {
System.out.print("Number of returned DataSets is: " + dataReadResult.getDataSets().size());
for (DataSet dataSet : dataReadResult.getDataSets()) {
dumpDataSet(dataSet);
}
}
}
// [START parse_dataset]
private static void dumpDataSet(DataSet dataSet) {
Log.v(TAG, "Name: " + dataSet.getDataType().getName());
DateFormat dateFormat = getTimeInstance();
Log.v(TAG, "Fields: " + dataSet.getDataSource().getDataType().getFields());
Log.v(TAG, "Data Point Values :" + dataSet.getDataPoints());
for (DataPoint dp : dataSet.getDataPoints()) {
Log.v(TAG, "Data Point:");
Log.v(TAG, "Type: " + dataSet.getDataType().getName());
Log.v(TAG, "Start: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
Log.v(TAG, "End: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
for (Field field : dp.getDataType().getFields()) {
Log.v(TAG, "Field: " + field.getName() + ", Value : " + dp.getValue(field).asFloat());
}
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_fetch:
readHRHistoryData();
}
}
}
How to resolve this issue? or Is any other options to read blood pressure data except REST api.
回答1:
Use builder like this:
return new DataReadRequest.Builder()
.aggregate(HealthDataTypes.TYPE_BLOOD_PRESSURE,HealthDataTypes.AGGREGATE_BLOOD_PRESSURE_SUMMARY)
.bucketByTime(1, TimeUnit.DAYS) //important thing
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.enableServerQueries()
.build();
来源:https://stackoverflow.com/questions/51285651/android-google-fit-unable-to-read-blood-pressure-value