0
votes

I am building a fitness application with Google Fit and I have the issue that when I send data via the history api regardless if I use the insertion method as well as the update method. The data is only sometimes changed instantly in the Fit app from Google itself. Meaning when I insert data via the history api from my own app and then send it to Google. The Fit app from Google only sometimes changes the corresponding data instantly. Altough the data is changed instantly when I logout from my Google Account. My logcat shows me the data has sucessfully been sent and arrives properly it just takes time for Google or Google Services to update the data and show it.

What causes this issue is it the time interval? Here is the code I am getting data from a Numberpicker to determine the height and send it to Google. I've read the docs, yet can't come to a conclusion currently. Any ideas as to why this is the case?

private DataSet updateFitnessData() {
    Log.i(TAG, "Creating a new data update request.");
    // [START build_update_data_request]
    // Set a start and end time for the data that fits within the time range
    // of the original insertion.
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    cal.add(Calendar.MINUTE, 0);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.MINUTE, 0);
    long startTime = cal.getTimeInMillis();

    // Create a data source
    DataSource dataSource =
            new DataSource.Builder()
                    .setAppPackageName(this)
                    .setDataType(DataType.TYPE_HEIGHT)
                    .setStreamName(TAG + " - step count")
                    .setType(DataSource.TYPE_RAW)
                    .build();

    // Create a data set
    DataSet dataSet = DataSet.create(dataSource);
    // For each data point, specify a start time, end time, and the data value 
    
    BigDecimal bigDecimal = new BigDecimal(heightDelta);
    BigDecimal changedValue = bigDecimal.movePointLeft(2);
    Float heightDecimalMoved = changedValue.floatValue();

    DataPoint dataPoint =
            dataSet.createDataPoint().setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
    dataPoint.getValue(Field.FIELD_HEIGHT).setFloat(heightDecimalMoved);
    

    dataSet.add(dataPoint);
    // [END build_update_data_request]
    return dataSet;
}

private Task<Void> updateData() {
    // Create a new dataset and update request.
    DataSet dataSet = updateFitnessData();
    long startTime = 0;
    long endTime = 0;

    // Get the start and end times from the dataset.
    for (DataPoint dataPoint : dataSet.getDataPoints()) {
        startTime = dataPoint.getStartTime(TimeUnit.MILLISECONDS);
        endTime = dataPoint.getEndTime(TimeUnit.MILLISECONDS);
    }

    // [START update_data_request]
    Log.i(TAG, "Updating the dataset in the History API.");

    DataUpdateRequest request =
            new DataUpdateRequest.Builder()
                    .setDataSet(dataSet)
                    .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
                    .build();

    // Invoke the History API to update data.
    return Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
            .updateData(request)
            .addOnCompleteListener(
                    new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                // At this point the data has been updated and can be read.
                                Log.i(TAG, "Data update was successful.");
                            } else {
                                Log.e(TAG, "There was a problem updating the dataset.", task.getException());
                            }
                        }
                    });
}