Feature availability
To determine whether a user's device supports training plans on Health Connect,
check the availability of FEATURE_PERSONAL_HEALTH_RECORD
on the client:
if (healthConnectClient
.features
.getFeatureStatus(
HealthConnectFeatures.FEATURE_PERSONAL_HEALTH_RECORD
) == HealthConnectFeatures.FEATURE_STATUS_AVAILABLE) {
// Feature is available
} else {
// Feature isn't available
}
See Check for feature availability to learn more.
Required permissions
Access to Personal Health Records is protected by the following permissions:
android.permission.health.WRITE_MEDICAL_DATA
android.permission.health.READ_MEDICAL_DATA_ALLERGIES_INTOLERANCES
android.permission.health.READ_MEDICAL_DATA_CONDITIONS
android.permission.health.READ_MEDICAL_DATA_LABORATORY_RESULTS
android.permission.health.READ_MEDICAL_DATA_MEDICATIONS
android.permission.health.READ_MEDICAL_DATA_PERSONAL_DETAILS
android.permission.health.READ_MEDICAL_DATA_PRACTITIONER_DETAILS
android.permission.health.READ_MEDICAL_DATA_PREGNANCY
android.permission.health.READ_MEDICAL_DATA_PROCEDURES
android.permission.health.READ_MEDICAL_DATA_SOCIAL_HISTORY
android.permission.health.READ_MEDICAL_DATA_VACCINES
android.permission.health.READ_MEDICAL_DATA_VISITS
android.permission.health.READ_MEDICAL_DATA_VITAL_SIGNS
Declare these permissions in the Play Console for your app, as well as in your app's manifest:
<application>
<uses-permission
android:name="android.permission.health.WRITE_MEDICAL_DATA" />
<uses-permission
android:name="android.permission.health.READ_MEDICAL_DATA_ALLERGIES_INTOLERANCES" />
...
</application>
You are responsible for declaring all the appropriate permissions you intend to use in your devices and apps. You should also check that each permission has been granted by the user before use.
Example usage
This section features code samples of basic operations for the upcoming Jetpack SDK release with PHR support, and are subject to change.
Create a MedicalDataSource record
// Create a `MedicalDataSource`
// Note that `displayName` must be unique across `MedicalDataSource`s
// Each `MedicalDataSource` is assigned an `id` by the system on creation
val medicalDataSource: MedicalDataSource =
healthConnectClient.createMedicalDataSource(
CreateMedicalDataSourceRequest(
fhirBaseUri = Uri.parse("https://fhir.com/oauth/api/FHIR/R4/"),
displayName = "Test Data Source",
fhirVersion = FhirVersion(4, 0, 1)
)
)
Delete a MedicalDataSource record
The previous example returns an id
by the system on creation. To delete,
reference that same id
:
// Delete the `MedicalDataSource` that has the specified `id`
healthConnectClient.deleteMedicalDataSourceWithData(medicalDataSource.id)
Get a MedicalDataSource record by package name
Use GetMedicalDataSourcesRequest
to request by package name (app):
// Retrieve all `MedicalDataSource`s created by any of the specified package names
// Package names may be found in other `MedicalDataSource`s or from arbitrary input
val medicalDataSources: List<MedicalDataSource> =
healthConnectClient.getMedicalDataSources(
GetMedicalDataSourcesRequest(listOf(medicalDataSource.packageName, anotherPackageName))
)
Get a MedicalDataSource record by ID
Or, request by id
if you know it:
// Retrieve all `MedicalDataSource` with `id` matching any of the given ids
val medicalDataSources: List<MedicalDataSource> =
healthConnectClient.getMedicalDataSources(listOf(medicalDataSource.id, anotherId))
Insert or update MedicalResource records
Use UpsertMedicalResourceRequest
to insert new or update existing
MedicalResource
records for a MedicalDataSource
:
// Insert `MedicalResource`s into the `MedicalDataSource`
val medicalResources: List<MedicalResource> =
healthConnectClient.upsertMedicalResources(
listOf(
UpsertMedicalResourceRequest(
medicalDataSource.id,
medicalDataSource.fhirVersion,
medicationJsonToInsert // a valid FHIR json string
)
)
)
// Update `MedicalResource`s in the `MedicalDataSource`
val updatedMedicalResources: List<MedicalResource> =
healthConnectClient.upsertMedicalResources(
listOf(
UpsertMedicalResourceRequest(
medicalDataSource.id,
medicalDataSource.fhirVersion,
// a valid FHIR json string
// if this resource has the same type and ID as in `medicationJsonToInsert`,
// this `upsertMedicalResources()` call will update the previously inserted
// `MedicalResource`
updatedMedicationJsonToInsert
)
)
)
Get MedicalResource records
Filter a get request by specifying the medicalResourceType
. Make sure to use
paged requests and be mindful of rate limiting.
// Read `MedicalResource`s back from the `MedicalDataSource`
// Read 100 resources / page. See `pageSize` doc for defaults and limits.
val pageSize = 100
// Prepare the initial read request.
// All `MedicalResource`s in the given `MedicalDataSource`s and of given `medicalResourceType`
// will be retrieved.
val initialRequest: ReadMedicalResourcesRequest =
ReadMedicalResourcesInitialRequest(
MEDICAL_RESOURCE_TYPE_LABORATORY_RESULTS,
setOf(medicalDataSource.id),
pageSize = pageSize,
)
// Continue reading pages until all `MedicalResource`s are read
var pageToken: String? = null
do {
// Prepare paged request if needed
val request: ReadMedicalResourcesRequest =
if (pageToken == null) initialRequest
else ReadMedicalResourcesPageRequest(pageToken, pageSize = pageSize)
// Read `MedicalResource`s
val response: ReadMedicalResourcesResponse =
healthConnectClient.readMedicalResources(request)
// Process `MedicalResource`s
val resources: List<MedicalResource> = response.medicalResources
// Advance to next page
pageToken = response.nextPageToken
} while (pageToken != null)
Get MedicalResource records by ID
// Retrieve `fhirResourceType` type `MedicalResource`s with the specified `id`s from the
// provided `MedicalDataSource`
val retrievedMedicalResources: List<MedicalResource> =
healthConnectClient.readMedicalResources(
medicalResources.map { medicalResource: MedicalResource ->
MedicalResourceId(
dataSourceId = medicalDataSource.id,
fhirResourceType = medicalResource.id.fhirResourceType,
fhirResourceId = medicalResource.id.fhirResourceId
)
}
)
Delete a MedicalResource record by ID
MedicalResource
records may be deleted by ID:
// Delete `MedicalResource`s matching the specified `dataSourceId`, `type` and `fhirResourceId`
healthConnectClient.deleteMedicalResources(
medicalResources.map { medicalResource: MedicalResource ->
MedicalResourceId(
dataSourceId = medicalDataSource.id,
fhirResourceType = medicalResource.id.fhirResourceType,
fhirResourceId = medicalResource.id.fhirResourceId
)
}
)
Or they can be deleted by medicalResourceType
:
// Delete all `MedicalResource`s that are in any pair of provided `dataSourceIds` and
// `medicalResourceTypes`
healthConnectClient.deleteMedicalResources(
DeleteMedicalResourcesRequest(
dataSourceIds = setOf(medicalDataSource.id),
medicalResourceTypes = setOf(MEDICAL_RESOURCE_TYPE_MEDICATIONS)
)
)