This guide is compatible with Health Connect version 1.1.0-alpha12.
Health Connect provides a skin temperature data type to measure peripheral body temperature. This measurement is a particularly useful signal for detecting sleep quality, reproductive health, and the potential onset of illness.
Required permissions
As with any data type in Health Connect, access to skin temperature is protected
by a pair of permissions: READ_SKIN_TEMPERATURE
and
WRITE_SKIN_TEMPERATURE
.
Request permissions from the user
After creating a client instance, your app needs to request permissions from the user. Users must be allowed to grant or deny permissions at any time.
To do so, create a set of permissions for the required data types. Make sure that the permissions in the set are declared in your Android manifest first.
// Create a set of permissions for required data types
val PERMISSIONS =
setOf(
HealthPermission.getReadPermission(SkinTemperatureRecord::class),
HealthPermission.getWritePermission(SkinTemperatureRecord::class)
)
Use getGrantedPermissions
to see if your app already has the
required permissions granted. If not, use
createRequestPermissionResultContract
to request
those permissions. This displays the Health Connect permissions screen.
// Create the permissions launcher
val requestPermissionActivityContract = PermissionController.createRequestPermissionResultContract()
val requestPermissions = registerForActivityResult(requestPermissionActivityContract) { granted ->
if (granted.containsAll(PERMISSIONS)) {
// Permissions successfully granted
} else {
// Lack of required permissions
}
}
suspend fun checkPermissionsAndRun(healthConnectClient: HealthConnectClient) {
val granted = healthConnectClient.permissionController.getGrantedPermissions()
if (granted.containsAll(PERMISSIONS)) {
// Permissions already granted; proceed with inserting or reading data
} else {
requestPermissions.launch(PERMISSIONS)
}
}
Because users can grant or revoke permissions at any time, your app needs to periodically check for granted permissions and handle scenarios where permission is lost.
Information included in a skin temperature record
Skin temperature measurements are organized into records. Each record consists of the following information:
- Baseline temperature, in degrees Celsius or degrees Fahrenheit. This is an optional value that is most useful for visualization in your app's UI.
- A list of deltas in skin temperature, each showing the change in skin temperature since the last measurement. If the baseline temperature is provided, these deltas should use the same temperature units.
- The location on the user's body where the measurement was taken: finger, toe, or wrist.
Supported aggregations
Health Connect lets you get the following aggregate values for a given list of deltas:
- Minimum value
- Maximum value
- Average value
Read skin temperature
The following code snippet shows how to read skin temperature measurements using the Jetpack library:
suspend fun readSkinTemperatures() {
// Error handling, permission check, and feature availability check
// aren't included.
// Record includes measurements during the past hour.
val recordEndTime = Instant.now()
val recordStartTime = recordEndTime.minusSeconds(60 * 60)
val response = healthConnectClient.readRecords(
ReadRecordsRequest<SkinTemperatureRecord>(
timeRangeFilter = TimeRangeFilter.between(
recordStartTime, recordEndTime
)
)
)
for (skinTemperatureRecord in response.records) {
// Process each skin temperature record here.
}
}
Write skin temperature
The following code snippet shows how to write skin temperature measurements using the Jetpack library:
suspend fun writeSkinTemperatures(): InsertRecordsResponse {
// Error handling, permission check, and feature availability check
// aren't included.
// Record includes measurements during the past hour.
val recordEndTime: ZonedDateTime = now()
val recordStartTime: ZonedDateTime = recordEndTime.minusHours(1)
healthConnectClient.insertRecords(
// For this example, there's only one skin temperature record.
listOf(
SkinTemperatureRecord(
baseline = Temperature.celsius(37.0),
startTime = recordStartTime.toInstant(),
startZoneOffset = recordStartTime.offset,
endTime = recordEndTime.toInstant(),
endZoneOffset = recordEndTime.offset,
deltas = listOf(
SkinTemperatureRecord.Delta(
recordEndTime.minusMinutes(50).toInstant(), celsius(0.5)
), SkinTemperatureRecord.Delta(
recordEndTime.minusMinutes(30).toInstant(), celsius(-0.7)
)
),
measurementLocation = SkinTemperatureRecord.MEASUREMENT_LOCATION_FINGER,
metadata = Metadata.autoRecorded(
device = Device(type = Device.TYPE_RING)
),
)
)
)
}