AI-generated Key Takeaways
-
Learn how to read daily step count data using both the Fit Android API and the Fit REST API.
-
The Fit Android API allows reading daily steps using
HistoryClient.readDailyTotalor by aggregating data from thecom.google.android.gmsapp package to match the Fit app's count. -
The Fit REST API enables reading the daily step count total by making a POST request to the
dataset:aggregateendpoint and querying thecom.google.step_count.deltadata type.
This section demonstrates reading current daily step count data using the Fit Android API and Fit REST API.
Android
Your app can read the current daily step total by calling
HistoryClient.readDailyTotal,
as shown in the following example:
Fitness.getHistoryClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA) .addOnSuccessListener { result -> val totalSteps = result.dataPoints.firstOrNull()?.getValue(Field.FIELD_STEPS)?.asInt() ?: 0 // Do something with totalSteps } .addOnFailureListener { e -> Log.i(TAG, "There was a problem getting steps.", e) }
The daily total is computed from midnight of the current day on the device's current timezone.
To get the same daily step count as the Fit app, create a data source using
the com.google.android.gmsapp package, as shown in the following example:
val startTime = LocalDate.now().atStartOfDay(ZoneId.systemDefault()) val endTime = LocalDateTime.now().atZone(ZoneId.systemDefault()) val datasource = DataSource.Builder() .setAppPackageName("com.google.android.gms") .setDataType(DataType.TYPE_STEP_COUNT_DELTA) .setType(DataSource.TYPE_DERIVED) .setStreamName("estimated_steps") .build() val request = DataReadRequest.Builder() .aggregate(datasource) .bucketByTime(1, TimeUnit.DAYS) .setTimeRange(startTime.toEpochSecond(), endTime.toEpochSecond(), TimeUnit.SECONDS) .build() Fitness.getHistoryClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .readData(request) .addOnSuccessListener { response -> val totalSteps = response.buckets .flatMap { it.dataSets } .flatMap { it.dataPoints } .sumBy { it.getValue(Field.FIELD_STEPS).asInt() } Log.i(TAG, "Total steps: $totalSteps") }
For more information about working with aggregate data sources, see Work with the Fitness History.
REST
Your app can read the current daily step count total across all data
sources by making a POST request and querying the
com.google.step_count.delta data type for the specified time period.
HTTP method
POST
Request URL
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate
Request body
{
"aggregateBy": [{
"dataTypeName": "com.google.step_count.delta",
"dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
}],
"bucketByTime": { "durationMillis": 86400000 },
"startTimeMillis": 1438705622000,
"endTimeMillis": 1439310422000
}
Curl command
curl \
-X POST \
-H "Content-Type: application/json;encoding=utf-8" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d @aggregate.json \
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate