androidgoogle-fitgoogle-fit-sdkgoogle-fit-api

Google Fit API returns no distance for biking activity


Version:

implementation "com.google.android.gms:play-services-fitness:21.1.0"

Client definition:

GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestScopes(Scope("https://www.googleapis.com/auth/fitness.activity.read"), Scope("https://www.googleapis.com/auth/fitness.location.read"))
            .requestIdToken(BuildConfig.GOOGLE_FIT_CLIENT_ID)
            .requestServerAuthCode(BuildConfig.GOOGLE_FIT_CLIENT_ID, true)
            .build()

val readRequest = DataReadRequest.Builder()
                .read(DataType.TYPE_ACTIVITY_SEGMENT)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build()

This gives me the list of dataSets -> dataPoints -> biking activities but no data about the distance. Only start/end time and the activity type.

If I open same activity in Google Fit app, it shows the distance of same activities. Any ideas?


Solution

  • After you parse activity and location, you need to parse distance delta in a separate query.

    This data type captures distance travelled by the user since the last reading, in meters. The total distance over an interval can be calculated by adding together all the values during the interval. The start time of each data point should represent the start of the interval in which the distance was covered. The start time must be equal to or greater than the end time of the previous data point.

    https://developers.google.com/fit/datatypes/location#android_2 https://developers.google.com/fit/datatypes/location#distance_delta