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)