4

I am getting this error from google fit api. It doesnt happens every time but if i get this error then i couldnt get my step data until i uninstall the app. I configured OAuth 2.0 client auth for my app correctly. But sometimes still I am getting this error

"com.google.android.gms.common.api.ApiException: 8: The connection to Google Play services was lost"

when I investigate it from google document it says me that:

https://developers.google.com/android/reference/com/google/android/gms/common/api/CommonStatusCodes public static final int INTERNAL_ERROR

As the document says when i try to connect googlefit on the onfailure method then nothimg happens and still facing this problem.

An internal error occurred. Retrying should resolve the problem. Constant Value: 8

here is the code block and sometimes the onfailure method is triggering

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

            Fitness.getHistoryClient(MainActivity.activity, GoogleSignIn.getLastSignedInAccount(MainActivity.activity))
                    .readData(readRequest)
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Crashlytics.log(Log.ASSERT, "fail  :", e.toString());
                        }
                    })
                    .addOnSuccessListener(new OnSuccessListener<DataReadResponse>()
                    {
                        @Override
                        public void onSuccess(DataReadResponse dataReadResponse)
                        {
                            for (Bucket bucket : dataReadResponse.getBuckets())
                            {

                                for (DataSet dataSet : bucket.getDataSets())
                                {
                                    for (DataPoint dataPoint : dataSet.getDataPoints())
                                    {

                                        User.current().userFitnessData.totalDistance = dataPoint.getValue(dataPoint.getDataType().getFields().get(0)).asFloat();
                                    }
                                }
                            }

                            Crashlytics.log(Log.ASSERT, "Todays Steps", User.current().userFitnessData.getTodaysSteps() + "");
                            if (callBack != null)
                                callBack.onDistanceDataReceived();
                        }
                    });
aligur
  • 3,387
  • 3
  • 34
  • 51
  • API's errors are the worst ones... I would try to investigate into which HTTP request is being posted and what response you get. – deathangel908 May 21 '18 at 07:52

2 Answers2

1

I'm not sure if it helps, but I always check this function before calling every Fitness Api.

public boolean isGoogleAccountSignIn(int requestCode){
    // permission you need
    FitnessOptions fitnessOptions = FitnessOptions.builder()
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_WRITE)
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_WRITE)
            .addDataType(DataType.TYPE_ACTIVITY_SEGMENT, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.TYPE_ACTIVITY_SEGMENT, FitnessOptions.ACCESS_WRITE)
            .build();

    if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(context), fitnessOptions)) {
        GoogleSignIn.requestPermissions((Activity) context, requestCode, GoogleSignIn.getLastSignedInAccount(context), fitnessOptions);
        return false;
    } else {
        return true;
    }
}

In MainActivity I'll do like this every action:

if (fitnessManager.isGoogleAccountSignIn(FitnessManager.REQUEST_CODE_ASK_ACCOUNT)) {
    // use Fitness Api you need
    fitnessManager.getDailyDataFromMobileInSegment(getLastWeekTime(), getTodayEndTime(), false);
}

The version of play-services-fitness and play-services-auth I use are both over 11.6.0.

ginnyhuang
  • 63
  • 6
0

Got the same Exception for user with lot of data - so my idea it is some timeout in Google API. Will try to get data by daily chunks, not weekly chunks

mobiledev Alex
  • 2,228
  • 2
  • 28
  • 30