1

Taking into account the approaching Google Fit changes on their policies and APIs I am trying to adapt my code getting rid of any read scope (as they will be declared restrictive).

I need to record steps and also read them periodically.

I'm declaring Scopes with these FitnessOptions

FitnessOptions.builder()
    .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_WRITE)
    .build()

I'm recording the steps without any problems but when trying to read them from the history client it doesn't work unless I add the READ Scope to the FitnessOptions with this line:

 .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)

But according to the update documentation I should be able to read data obtained by my app without requiring any read scope:

Now, Write scopes allow your app to have write-access and read-access only to data written by your app.

Here a code snipped on how is how I am trying to recover the steps using the history client:

        Fitness.getHistoryClient(this, getGoogleAccount())
        .readData(
            DataReadRequest.Builder().read(DataType.TYPE_STEP_COUNT_DELTA)
                .bucketByActivitySegment(1, TimeUnit.MINUTES)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .enableServerQueries()
                .build()
        ).addOnSuccessListener {
            Log.i(TAG, "Data obtained")
        }.addOnFailureListener {
            Log.e(TAG, "Error obtaining Google Fit History data", it);
        }

UPDATE:

Following @AndyTurner advice I tried recording and reading the data from my own DataSource but when I read it from history client I am not receiving any DataPoint.

This is my DataSource:

val dataSource =  DataSource.Builder()
        .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
        .setAppPackageName(BuildConfig.APPLICATION_ID)
        .setType(DataSource.TYPE_DERIVED) // Also tried with DataSource.TYPE_RAW
        .build()

This is how I am recording steps now:

Fitness.getRecordingClient(this, getGoogleAccount())
    .subscribe(dataSource)
    .addOnSuccessListener {
        logger.d(TAG, "Google Fit recording steps");
    }

The code for retrieving the history data is the same posted previously but using the dataSource instead of the data type.

Note that my app doesn't rely on any hardware for reading steps. It tries to read it from Google Fit step count implementation (using the device sensors)

franmontiel
  • 1,860
  • 1
  • 16
  • 20
  • As the quote says, you can read data *written by your app* using the write scope. Your data read request isn't limited to data written by your app. – Andy Turner Apr 21 '21 at 17:52
  • @AndyTurner I double-checked both DataReadRequest(https://developers.google.com/android/reference/com/google/android/gms/fitness/request/DataReadRequest) and its Builder (https://developers.google.com/android/reference/com/google/android/gms/fitness/request/DataReadRequest.Builder) class documentation and I don't see the way of limiting the request to the data of your app. Maybe am I missing something? – franmontiel Apr 22 '21 at 11:33
  • 1
    Read the [data source](https://developers.google.com/android/reference/com/google/android/gms/fitness/request/DataReadRequest.Builder#read(com.google.android.gms.fitness.data.DataSource)) for your application, not the data type. – Andy Turner Apr 22 '21 at 12:10
  • @AndyTurner thank you very much for your guidance. Now I don't need any external permission using the DataSource but I am having a hard time trying to record steps that way. It seems that recording the step into my DataSource doesn't work at all. I am updating the question with more details about the issue. – franmontiel Apr 22 '21 at 18:27

0 Answers0