기능 사용 가능 여부
사용자의 기기가 헬스 커넥트에서 운동 계획을 지원하는지 확인하려면 클라이언트에서 FEATURE_PERSONAL_HEALTH_RECORD
의 사용 가능 여부를 확인합니다.
if (healthConnectClient
.features
.getFeatureStatus(
HealthConnectFeatures.FEATURE_PERSONAL_HEALTH_RECORD
) == HealthConnectFeatures.FEATURE_STATUS_AVAILABLE) {
// Feature is available
} else {
// Feature isn't available
}
자세한 내용은 기능 사용 가능 여부 확인하기를 참고하세요.
필수 권한
개인 건강 기록에 대한 액세스는 다음 권한으로 보호됩니다.
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
앱의 Play Console 및 앱 매니페스트에서 다음 권한을 선언합니다.
<application>
<uses-permission
android:name="android.permission.health.WRITE_MEDICAL_DATA" />
<uses-permission
android:name="android.permission.health.READ_MEDICAL_DATA_ALLERGIES_INTOLERANCES" />
...
</application>
기기와 앱에서 사용할 모든 적절한 권한을 선언해야 할 책임은 개발자에게 있습니다. 또한 사용하기 전에 각 권한이 사용자에 의해 부여되었는지 확인해야 합니다.
사용 예
이 섹션에서는 PHR 지원이 포함된 향후 Jetpack SDK 출시의 기본 작업에 관한 코드 샘플을 제공하며, 이는 변경될 수 있습니다.
MedicalDataSource 레코드 만들기
// 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)
)
)
MedicalDataSource 레코드 삭제
이전 예에서는 생성 시 시스템에 의해 id
를 반환합니다. 삭제하려면 동일한 id
를 참조합니다.
// Delete the `MedicalDataSource` that has the specified `id`
healthConnectClient.deleteMedicalDataSourceWithData(medicalDataSource.id)
패키지 이름으로 MedicalDataSource 레코드 가져오기
GetMedicalDataSourcesRequest
를 사용하여 패키지 이름 (앱)별로 요청합니다.
// 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))
)
ID로 MedicalDataSource 레코드 가져오기
또는 id
를 알고 있다면 다음과 같이 요청합니다.
// Retrieve all `MedicalDataSource` with `id` matching any of the given ids
val medicalDataSources: List<MedicalDataSource> =
healthConnectClient.getMedicalDataSources(listOf(medicalDataSource.id, anotherId))
MedicalResource 레코드 삽입 또는 업데이트
UpsertMedicalResourceRequest
를 사용하여 MedicalDataSource
의 새 MedicalResource
레코드를 삽입하거나 기존 MedicalResource
레코드를 업데이트합니다.
// 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
)
)
)
MedicalResource 레코드 가져오기
medicalResourceType
를 지정하여 get 요청을 필터링합니다. 페이징된 요청을 사용하고 비율 제한에 유의하세요.
// 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)
ID별로 MedicalResource 레코드 가져오기
// 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
)
}
)
ID별로 MedicalResource 레코드 삭제
MedicalResource
레코드가 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
)
}
)
또는 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)
)
)