本指南与 健康数据共享 1.1.0-beta02 或更高版本兼容。
如需使用健康数据共享中的医疗记录写入医疗数据,请按照以下步骤操作:
- 查看功能可用性
- 请求写入权限
- 创建数据源 (
MedicalDataSource) - 写入医疗资源 (
MedicalResource)
功能的适用范围
如需确定用户的设备是否支持 Health Connect 上的医疗记录,请在客户端上检查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
在 Play 管理中心内为您的应用声明这些权限,并在应用的清单中声明这些权限:
<application>
<uses-permission
android:name="android.permission.health.WRITE_MEDICAL_DATA" />
</application>
您有责任声明您打算在设备和应用中使用的所有适当权限。您还应在使用前检查用户是否已授予每项权限。
向用户请求权限
创建客户端实例后,您的应用需要向用户请求权限。必须允许用户随时授予或拒绝权限。
为此,请为所需的数据类型创建一个权限集。 首先,请确保权限集中的权限已在 Android 清单中声明。
// Create a set of permissions for required data types
import androidx.health.connect.client.permission.HealthPermission.Companion.PERMISSION_WRITE_MEDICAL_DATA
val PERMISSIONS =
setOf(
PERMISSION_WRITE_MEDICAL_DATA
)
使用 getGrantedPermissions 查看您的应用是否已获得所需的权限。如不具备,请使用 createRequestPermissionResultContract 请求这些权限。系统会显示“健康数据共享”权限界面。
// 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)
}
}
由于用户可以随时授予或撤消权限,因此您的应用每次在使用权限之前都需要检查权限,并处理权限丢失的情况。
数据源
健康数据共享 中的 MedicalDataSource 表示面向用户的数据源,例如医疗保健组织、医院或 API。
存储在“健康数据共享”中的医疗记录会整理到 MedicalDataSource 中。这样可以将来自不同来源(例如 API 或医疗保健系统)的同一人的医疗记录分开。
创建 MedicalDataSource 记录时,请考虑以下事项:
- 如果所有记录都来自同一来源,请创建一个
MedicalDataSource。 - 如果记录来自多个来源,您可以在以下情况下创建单个
MedicalDataSource:- 数据已对账。
- 所有记录都具有 FHIR 资源类型和 FHIR 资源 ID 的唯一组合。在
MedicalDataSource中,只能有一条具有任何 FHIR 资源 ID 的 FHIR 资源类型记录。
- 否则,为防止 ID 重复,请为每个数据源创建一个
MedicalDataSource。
所有医疗记录都必须与 MedicalDataSource 相关联,因此必须先创建此资源,然后才能写入其他资源。
MedicalDataSource 的属性:
- 显示名称(必填)- 数据源面向用户的显示名称,由每个写入应用唯一标识。
FHIR 基础 URI(必需)- 对于来自 FHIR 服务器的数据,此值应为 FHIR 基础网址(例如
https://example.com/fhir/)。多个数据源可以与同一个 FHIR 基础网址相关联。如果数据是由没有 FHIR 网址的应用生成的,则此字段应为应用定义的唯一且易于理解的 URI(例如
myapp://..),指向数据来源。例如,如果客户端应用支持深层链接,则此深层链接可用作 FHIR 基本 URI。URI 的最大长度为 2000 个字符。
软件包名称(自动填充)- 写入数据的应用。
FHIR 版本(必填)- FHIR 版本。必须是受支持的版本。
创建 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。如果您需要删除 MedicalDataSource 记录,请引用同一 id:
// Delete the `MedicalDataSource` that has the specified `id`
healthConnectClient.deleteMedicalDataSourceWithData(medicalDataSource.id)
医疗资源
“健康数据共享”中的 MedicalResource 表示 FHIR 资源(包含医疗记录)以及元数据。
MedicalResource 的属性:
- DataSourceId(必填)-
MedicalDataSource中所述的数据源。 - FHIR 版本(必填)- FHIR 版本。必须是受支持的版本。
- FHIR 资源(必需):采用 JSON 编码的 FHIR 资源实例。
- 医疗资源类型(自动填充)- 资源的面向用户的类别,映射到面向用户的权限。
准备 JSON 格式的 FHIR 资源
在将医疗资源写入健康数据共享之前,请先以 JSON 格式准备好 FHIR 资源记录。将每个 JSON 存储在自己的变量中,以便作为医疗资源插入。
如果您需要 FHIR JSON 格式方面的帮助,请参阅 HL7 组织提供的示例数据。
插入或更新 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
)
)
)
FHIR 资源示例
在前面的示例中,变量 medicationJsonToInsert 表示有效的 FHIR JSON 字符串。
以下是该 JSON 的示例,其中使用 AllergyIntolerance 作为 FHIR 资源类型,该类型会映射到医疗记录中的医疗资源类型 FHIR_RESOURCE_TYPE_ALLERGY_INTOLERANCE:
{
"resourceType": "AllergyIntolerance",
"id": "allergyintolerance-1",
"criticality": "high",
"code": {
"coding": [
{
"system": "http://snomed.info/sct",
"code": "91936005",
"display": "Penicillin allergy"
}
],
"text": "Penicillin allergy"
},
"recordedDate": "2020-10-09T14:58:00+00:00",
"asserter": {
"reference": "Patient/patient-1"
},
"lastOccurrence": "2020-10-09",
"patient": {
"reference": "Patient/patient-1",
"display": "B., Alex"
}
...
}
删除 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)
)
)