Android Google Fit无法读取血压值

时间:2018-07-11 12:36:45

标签: android google-fit

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

血压记录:

  

返回的数据集存储桶数为:1

     

数据集:[DataSet {d:blood_pressure.summary:gms:aggregated []}]

     

名称:com.google.blood_pressure.summary

     

字段:   [blood_pressure_systolic_maxage(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)]

     

数据点值:[]

我检查了google fit,并输入了血压和心率,并能够读取心率值。

心率日志:

  

返回的数据集存储桶数为:1数据集:   [DataSet {d:heart_rate.summary:gms:聚合了[RawDataPoint {[81.0,   81.0,81.0] @ 1531305031000000000,1531305031000000000}]}]]名称:com.google.heart_rate.summary字段:[average(f),max(f),min(f)]

     

数据点值:[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速率计数)}]

     

数据点:类型:com.google.heart_rate.summary开始:4:00:31 PM结束:   4:00:31 PM字段:平均值,值:81.0字段:最大值,值:81.0   字段:min,值: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之外的其他任何读取血压数据的选项。

1 个答案:

答案 0 :(得分:0)

使用这样的构建器:

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