Thursday, 1 July 2021

How to get historic distance date from Google FIT in android?

I am trying to get historic distance data of a user by passing a start time and an end time to google FIT android API.

I am posting this query after checking all the related article available in stack overflow.

/**
 * Returns a {@link DataReadRequest}.
 */
public DataReadRequest queryFitnessStepData(String startTime, String endTime) {
    long startVal = convertDateStringtoEpoch(startTime, true);
    long endVal = convertDateStringtoEpoch(endTime, false);

    DataSource datasource = new DataSource.Builder()
            .setAppPackageName("com.google.android.gms")
            .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
            .setType(DataSource.TYPE_DERIVED)
            .setStreamName("distance_delta")
            .build();

    return new DataReadRequest.Builder()
            .aggregate(datasource)
            .bucketByTime(1, TimeUnit.DAYS)
            .setTimeRange(startVal, endVal, TimeUnit.MILLISECONDS)
            .build();
}

private void readHistoryDistanceData(String strDate, String endDate) {
readRequest = queryFitnessStepData(strDate, endDate);

FitnessOptions fitnessOptions =
            FitnessOptions.builder()
                    .addDataType(DataType.TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
                    .addDataType(DataType.AGGREGATE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
                    .build();

    if (GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(requireActivity()), fitnessOptions)) {
        Fitness.getHistoryClient(requireActivity(), Objects.requireNonNull(GoogleSignIn.getLastSignedInAccount(requireActivity())))
                .readData(readRequest)
                .addOnSuccessListener(
                        new OnSuccessListener<DataReadResponse>() {
                            @Override
                            public void onSuccess(DataReadResponse dataReadResponse){ 
                                  //parse data
                                }
                              }
                          }
                     }

However on running this code I am unable to get any data related to user's distance. I have all the permission like ACCESS_FINE_LOCATION and ACTIVITY_RECOGNITION in place.

Any help would be appreciated.



from How to get historic distance date from Google FIT in android?

No comments:

Post a Comment