0

We are struggling to integrate Google Fit on native Android devices. We are able to authenticate the user via OAuth popup provided by capacitor-plugin-health but we run into the following error when trying to send data to Google Fit.

com.google.android.gms.common.api.ApiException 5015

When running a query to grab activity data from Google Fit for past 3 days, the query results are empty. Google Fit is installed on my device and it seems Google Fit automatically adds data in there as the day goes on.

Our Android manifest contains <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" /> at the bottom. We also have classpath 'com.google.gms:google-services:4.3.13' in our project build.gradle, apply plugin: 'com.google.gms.google-services' in our app build.gradle, and implementation "com.google.android.gms:play-services-fitness:20.0.0" implementation "com.google.android.gms:play-services-auth:19.0.0" in our capacitor.build.gradle`

Here's an example implementation:

DataSource datasrc = new DataSource.Builder()
      .setAppPackageName(sourceBundleId)
      .setDataType(dt)
      .setType(DataSource.TYPE_RAW)
      .build();

DataSet.Builder dataSetBuilder = DataSet.builder(datasrc);
DataPoint.Builder datapointBuilder = DataPoint.builder(datasrc);
datapointBuilder.setTimeInterval(st, et, TimeUnit.MILLISECONDS);

String value = args.getJSONObject(0).getString("value");
datapointBuilder.setActivityField(Field.FIELD_ACTIVITY, value);

dataSetBuilder.add(datapointBuilder.build());

Fitness.getHistoryClient(this.cordova.getContext(), this.account)
      .insertData(dataSetBuilder.build())
      .addOnSuccessListener(r -> {
        callbackContext.success();
      })
      .addOnFailureListener(err -> {
          String message = "";
          if (err != null) {
            message = err.getMessage();
            if (err.getCause() != null) {
              err.getCause().printStackTrace();
            }
          }
          callbackContext.error("Error getting history client" + err);
      });

I put this error in ChatGPT and it gave the following:

"The com.google.android.gms.common.api.ApiException with error code 5015 can occur when using the Google Fitness API. Error code 5015 corresponds to FITNESS_API_NOT_CONNECTED, which indicates that the connection to the Fitness API has been lost.

This error can occur if the connection to the Google Fit API is interrupted or if the user revokes the required permissions for the app. To resolve this issue, you can try the following steps:

  1. Reconnect to the Google Fit API by calling GoogleApiClient.connect() method.
  2. Ensure that the user has granted the required permissions to access the Fitness API.
  3. Check the internet connectivity to ensure that the connection to the Google servers is not interrupted.
  4. Verify that the app's configuration is correct, including the OAuth 2.0 client ID, package name, and signing certificate.

If none of the above steps resolve the issue, you may want to check the Google Developer documentation and forums to see if there are any known issues or workarounds for this error. Additionally, you can try to catch the ApiException and handle it in your code by displaying an error message to the user or attempting to reconnect to the API."

No luck with those steps... We double checked that the app has permission to write/read by going to my account and looking at permissions...

Any insight would be greatly appreciated!

0 Answers0