AFHealth Tracking
AFHealthTracking is an object responsible for managing health data interactions. It provides functionalities to request permissions, check permission status, sync health data, fetch health data by date, and revoke permissions.
This object utilizes Android's Health Connect API to read health data such as steps, calories, and distance. It maintains a set of required health permissions and manages them per ComponentActivity.
Historical health data can be fetched for a specific date using fetchHealthDataByDate.
Sample Usage:
1. Requesting Permissions (in an Android ComponentActivity):
class MyActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launch {
try {
val granted = AFHealthTracking.requestPermissions(this@MyActivity)
if (granted) {
// Permissions granted, proceed with health data operations
} else {
// Permissions denied
}
} catch (e: Throwable) {
// Handle exceptions, e.g., Health Connect not installed
Log.e("HealthTracking", "Error requesting permissions", e)
}
}
}
}2. Checking Permissions:
// In a coroutine scope
val hasPermissions = AFHealthTracking.hasAllPermissions(activityContext) // activityContext is your ComponentActivity
if (hasPermissions) {
// All required permissions are granted
}3. Fetching Historical Health Data:
// In a coroutine scope
try {
val healthDataForYesterday = AFHealthTracking.fetchHealthDataByDate(day = 25, month = 12, year = 2023)
println("Steps on 2023-12-25: ${healthDataForYesterday.steps}")
println("Calories on 2023-12-25: ${healthDataForYesterday.calories}")
} catch (e: Throwable) {Provides access to health data on iOS using HealthKit.
This object encapsulates the logic for requesting permissions, checking permissions, syncing data (though HealthKit provides real-time data, so explicit sync is often not needed), fetching health data by date, and attempting to revoke permissions (which is not programmatically possible with HealthKit).
It interacts with the HKHealthStore to perform these operations and uses a Logger for internal logging.
The primary health data types it works with are:
Step Count (
HKQuantityTypeIdentifierStepCount)Active Energy Burned (
HKQuantityTypeIdentifierActiveEnergyBurned)Distance Walking/Running (
HKQuantityTypeIdentifierDistanceWalkingRunning)
All public functions are suspending functions and can throw exceptions, typically related to HealthKit availability or authorization issues.
Provides access to health data on watchOS using HealthKit.
This object encapsulates the logic for requesting permissions, checking permissions, syncing data (though HealthKit provides real-time data, so explicit sync is often not needed), fetching health data by date, and attempting to revoke permissions (which is not programmatically possible with HealthKit).
It interacts with the HKHealthStore to perform these operations and uses a Logger for internal logging.
The primary health data types it works with are:
Step Count (
HKQuantityTypeIdentifierStepCount)Active Energy Burned (
HKQuantityTypeIdentifierActiveEnergyBurned)Distance Walking/Running (
HKQuantityTypeIdentifierDistanceWalkingRunning)
All public functions are suspending functions and can throw exceptions, typically related to HealthKit availability or authorization issues.
watchOS limitation: There is no openHealthApp() method on watchOS because the platform does not support launching external apps via URL schemes. Users should manage HealthKit permissions from the Health app on their paired iPhone.
Functions
Disables HealthKit background delivery for step-count data.
Disables HealthKit background delivery for step-count data.
Enables HealthKit background delivery for step-count data.
Enables HealthKit background delivery for step-count data.
Queries HealthKit for all apps and devices that have contributed step-count data.
Queries HealthKit for all apps and devices that have contributed step-count data.
Checks whether the HealthKit authorization dialog has already been presented.
Checks whether the HealthKit authorization dialog has already been presented.
Returns the set of Health Connect permission strings that have been granted to this app.
Returns a comprehensive HealthConnectDiagnostics snapshot that developers can log or display in a debug/settings screen.
Returns a comprehensive HealthKitDiagnostics snapshot that developers can log or display in a debug/settings screen.
Returns a comprehensive com.advantahealth.api.health.model.HealthKitDiagnostics snapshot that developers can log or display in a debug/settings screen.
Returns the current availability status of the Health Connect SDK.
Infers whether the user has granted read permission for step-count data.
Infers whether the user has granted read permission for step-count data.
Redirects the user to the Google Play Store to install or update Health Connect.
Convenience check — returns true when the Health Connect SDK is fully available and ready to use on this device.
Returns true when HealthKit is available on this device.
Returns true when HealthKit is available on this device.
Checks whether the background-read permission is granted.
Checks whether the history-read permission is granted.
Opens the Apple Health app.
Opens the Health Connect settings screen (the system Health Connect app).