Write data into Health Connect
This includes apps that do passive fitness or health tracking, like recording steps or heart rate continuously in the background.
Your app should periodically write data into Health Connect, and it is recommended that your app follow these best practices:
- On every sync, only write new data and update data that was modified since the last sync.
- Chunk requests to at most 1000 records per write request.
Use the WorkManager API to schedule periodic background tasks, with a time period of at least 15 minutes. Additionally, consider restricting the tasks to run only when the device is idle and is not low on battery.
val constraints = Constraints.Builder() .requiresBatteryNotLow() .requiresDeviceIdle(true) .build() val writeDataWork = PeriodicWorkRequestBuilder<WriteDataToHealthConnectWorker>( 15, TimeUnit.MINUTES, 5, TimeUnit.MINUTES) .setConstraints(constraints) .build()
This includes apps that do event-based tracking (like Exercise, Sleep) or manual user input (like Nutrition). These records are created when the app is in the foreground or are rare events (a few times a day).
Ensure that your app does not keep Health Connect running for the entire duration of the event. Data should be written into Health Connect in one of the following two ways:
- Sync data into Health Connect after the event is complete. For example: When the user ends a tracked exercise session.
- Schedule a one-off task using WorkManager to sync the data later.
When inserting data into Health Connect, consider using appropriate sample rates to help reduce storage load. For example, it's worth thinking about how frequently step count data needs to be recorded, or what kind of sample rate data types linked to an active workout (like speed) should require.
Not every data type requires the same sample rate. This is because there may be little added benefit to users having step count data updated every second as opposed to every 60 seconds, for example. However, higher sample rates may give users a more detailed and granular look at their health and fitness data. Balance detail and performance by adjusting sample rate frequencies.
In rare circumstances, Health Connect can rate limit requests from your app. This is done to limit impact on the battery and performance of the device on which Health Connect is running.
- Developers are strongly recommended to follow the best practices to prevent your app's requests from being rejected due to rate limiting.
- Apps should be resilient to rate limiting. For example: for apps writing data in the background, data should be written during the next periodic task in case of requests failing due to rate limiting.