gpt4 book ai didi

Android Google Fit 无法读取血压值

转载 作者:行者123 更新时间:2023-11-29 02:27:43 25 4
gpt4 key购买 nike

我尝试使用 Android 代码从 Google Fit 读取血压值,但无法读取值并返回以下日志

血压记录:

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 : []

我已经检查过 google fit,它有血压和心率的条目,并且能够读取心率值。

心率日志:

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.0Field: min, Value : 81.0

这是我的完整代码:

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();
}
}
}

如何解决这个问题?或者是除了 REST api 之外的任何其他读取血压数据的选项。

最佳答案

像这样使用构建器:

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();

关于Android Google Fit 无法读取血压值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51285651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com