3

我正在尝试从 google fit API 获取过去 24 小时内的用户步数和位置边界框。

我的步数没有问题,但我收到了位置错误。

代码:

@Override
protected void onStart() {
    super.onStart();
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                // Optionally, add additional APIs and scopes if required.
                .addApi(Fitness.API)
                .addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
                .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    mGoogleApiClient.connect();
} 

private class getFitnessTask extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... params) {

        Log.e("getFitnessTask", "getFitnessTask");
        getStepsToday();
        getLocation();
        return null;
    }

    @Override
    protected void onPostExecute(String s) {

    }
}

private void getStepsToday() {
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    long startTime = cal.getTimeInMillis();

    final DataReadRequest readRequest = new DataReadRequest.Builder()
            .read(DataType.TYPE_STEP_COUNT_DELTA)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

    DataReadResult dataReadResult =
            Fitness.HistoryApi.readData(mGoogleApiClient, readRequest).await(1, TimeUnit.MINUTES);

    DataSet stepData = dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);

    int totalSteps = 0;

    for (DataPoint dp : stepData.getDataPoints()) {
        for(Field field : dp.getDataType().getFields()) {

            int steps = dp.getValue(field).asInt();
            totalSteps += steps;

        }
    }

    Log.e("FITNES", "total steps: " + totalSteps);
}

private void getLocation() {
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    long startTime = cal.getTimeInMillis();

    final DataReadRequest readRequest = new DataReadRequest.Builder()
            .read(DataType.TYPE_STEP_COUNT_DELTA)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

    DataReadResult dataReadResult =
            Fitness.HistoryApi.readData(mGoogleApiClient, readRequest).await(1, TimeUnit.MINUTES);

    DataSet locationData = dataReadResult.getDataSet(DataType.AGGREGATE_LOCATION_BOUNDING_BOX);

    Log.e("FIT", locationData.toString());
    int totalSteps = 0;

    for (DataPoint dp : locationData.getDataPoints()) {
        for(Field field : dp.getDataType().getFields()) {

            Log.e("FITNES", field.toString());

        }
    }

}

我得到了这个错误:

DataSet locationData = dataReadResult.getDataSet(DataType.AGGREGATE_LOCATION_BOUNDING_BOX);

原因:java.lang.IllegalArgumentException:尝试读取 com.google.location.bounding_box 的数据,但未请求

那么缺少什么?我怎样才能得到位置边界框?谢谢

4

0 回答 0