The Google Fit APIs, including the Google Fit REST API, will no longer be available after June 30, 2025. As of May 1, 2024, developers cannot sign up to use these APIs.
Stay organized with collections
Save and categorize content based on your preferences.
This example shows how to create a Fitness API client.
Create the API client as follows:
Create a FitnessOptions instance, declaring the
data types and access type (read and/or write) your app
needs:
val fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.build()
Get an instance of the Account object to use with the API:
val account = GoogleSignIn.getAccountForExtension(this, fitnessOptions)
Check if the user has previously granted the necessary data access, and if
not, initiate the authorization flow:
if (!GoogleSignIn.hasPermissions(account, fitnessOptions)) {
GoogleSignIn.requestPermissions(
this, // your activity
GOOGLE_FIT_PERMISSIONS_REQUEST_CODE, // e.g. 1
account,
fitnessOptions)
} else {
accessGoogleFit()
}
If the authorization flow is required, handle the user's response:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (resultCode) {
Activity.RESULT_OK -> when (requestCode) {
GOOGLE_FIT_PERMISSIONS_REQUEST_CODE -> accessGoogleFit()
else -> {
// Result wasn't from Google Fit
}
}
else -> {
// Permission not granted
}
}
}
After the user has authorized access to the data requested, create a fitness
client (for example, a HistoryClient to read and/or write historic fitness
data) based on your app's purpose and needs:
private fun accessGoogleFit() {
val end = LocalDateTime.now()
val start = end.minusYears(1)
val endSeconds = end.atZone(ZoneId.systemDefault()).toEpochSecond()
val startSeconds = start.atZone(ZoneId.systemDefault()).toEpochSecond()
val readRequest = DataReadRequest.Builder()
.aggregate(DataType.AGGREGATE_STEP_COUNT_DELTA)
.setTimeRange(startSeconds, endSeconds, TimeUnit.SECONDS)
.bucketByTime(1, TimeUnit.DAYS)
.build()
val account = GoogleSignIn.getAccountForExtension(this, fitnessOptions)
Fitness.getHistoryClient(this, account)
.readData(readRequest)
.addOnSuccessListener({ response ->
// Use response data here
Log.i(TAG, "OnSuccess()")
})
.addOnFailureListener({ e -> Log.d(TAG, "OnFailure()", e) })
}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-11-20 UTC."],[[["This guide demonstrates how to build a Fitness API client in Android to read aggregate step count data."],["It outlines the process of creating a `FitnessOptions` instance to define the data types and access permissions required."],["Users will be prompted to grant necessary permissions during the Google Sign-In authorization flow."],["Upon authorization, the code demonstrates how to access the Google Fit HistoryClient to retrieve and utilize the step count data."],["The example showcases requesting aggregate step data over the past year, bucketed by day, without needing specific Android permissions."]]],[]]