Search code examples
androidgoogle-fit

nutrition data google fit


Is there a delay to read nutrition data ? I inserted nutrition data and I'm trying to read them but the size of the DataPoint is still equal to 0...

I inserted the data like this:

long now = System.currentTimeMillis();

DataSource nutritionSource = new DataSource.Builder()
            .setAppPackageName(getApplicationContext().getPackageName())
            .setType(DataSource.TYPE_RAW)
            .setDataType(DataType.TYPE_NUTRITION)
            .build();

DataSet dataSet = DataSet.create(nutritionSource);

DataPoint dataPoint = DataPoint.create(nutritionSource);
dataPoint.setTimestamp(now, TimeUnit.MILLISECONDS);
dataPoint.getValue(Field.FIELD_FOOD_ITEM).setString(name_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_CALORIES, calorie_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_SUGAR,sugar_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_TOTAL_FAT,fat_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_PROTEIN,protein_food);
dataPoint.getValue(Field.FIELD_MEAL_TYPE).setInt(Field.MEAL_TYPE_UNKNOWN);


dataSet.add(dataPoint);

Then, I'm trying to read them like this:

DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(DataType.TYPE_NUTRITION, DataType.AGGREGATE_NUTRITION_SUMMARY)
            .bucketByTime(1, TimeUnit.DAYS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

    // Invoke the History API to fetch the data with the query and await the result of
    // the read request.
    DataReadResult dataReadResult =
            Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES);

    if(dataReadResult.getStatus().isSuccess()){
        Log.i("TAG","isSuccess to read nutrition data");
        if(dataReadResult.getDataSet(DataType.TYPE_NUTRITION).getDataPoints().size() > 0){
            Log.i("TAG","calorie : "+dataReadResult.getDataSet(DataType.TYPE_NUTRITION).getDataPoints().get(0).getValue(Field.FIELD_CALORIES));
        }
    }

I would like to get all datapoints of the day and work with them but I don't know how ??

Thanks,

Kamel


Solution

  • Not sure if this is still an issue for you but try using this to get each "bucket" of aggregated data.

            List<Bucket> buckets = dataReadResult.getBuckets();
            for (int a = buckets.size() - 1; a > -1; a--) {
                Bucket bucket = buckets.get(a);
                for (DataSet dataSet : bucket.getDataSets()) {
                    if(dataSet.getDataType().getName().equals(DataType.TYPE_NUTRITION.getName())){
                        for (DataPoint dp : dataSet.getDataPoints()) {
                            Log.i(TAG, "Data point:");
                            Log.i(TAG, "\tType: " + dp.getDataType().getName());
                            for(Field field : dp.getDataType().getFields()) {
                                Log.i(TAG, "\tField: " + field.getName() +
                                        " Value: " + dp.getValue(field));
                            }
                        }
                    }
                }
            }