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 のデータを読み取ろうとしています
それで、何が欠けていますか?位置境界ボックスを取得するにはどうすればよいですか? ありがとう