운동 이벤트 처리

건강 관리 서비스는 ExerciseEvents를 지원합니다. 이를 통해 운동 중에 이벤트가 발생하면 앱에 알리고 연결된 메타데이터를 제공합니다.

종속 항목 추가

운동 이벤트를 사용하려면 최신 버전의 건강 관리 서비스 SDK가 필요합니다.

건강 관리 서비스에 종속 항목을 추가하려면 프로젝트에 Google Maven 저장소를 추가해야 합니다. 자세한 내용은 Google의 Maven 저장소를 참고하세요.

그런 다음, 모듈 수준의 build.gradle 파일에 다음 종속 항목을 추가합니다.

Groovy

dependencies {
    implementation "androidx.health:health-services-client:1.1.0-alpha02"
}

Kotlin

dependencies {
    implementation("androidx.health:health-services-client:1.1.0-alpha02")
}

기능 확인

건강 관리 서비스의 모든 운동 및 데이터 유형과 마찬가지로 시작 시 기능을 확인합니다. 특히 ExerciseEvents의 경우 ExerciseCapabilities 요청 외에 ExerciseTypeCapabilities.supportedExerciseEvents를 사용하여 특정 운동에서 지원되는 운동 이벤트를 확인합니다. 특정 ExerciseEvent가 지원되는지 확인한 후에는 getExerciseEventCapabilityDetails를 사용하여 운동 이벤트의 기능도 쿼리해야 합니다.

다음 예에서는 기능을 쿼리하여 GOLF_SHOT_EVENT가 지원되는지 확인하고 GOLF_SHOT_EVENT가 스윙 유형 분류를 지원하는지 확인하는 방법을 보여줍니다.

fun handleCapabilities(capabilities: ExerciseCapabilities) {
  val golfCapabilities = capabilities.typeToCapabilities[ExerciseType.GOLF]
  val golfShotEventSupported =
    golfCapabilities
      ?.supportedExerciseEvents
      ?.contains(ExerciseEventType.GOLF_SHOT_EVENT)
  val golfSwingTypeClassificationSupported =
    golfCapabilities
      ?.getExerciseEventCapabilityDetails(ExerciseEventType.GOLF_SHOT_EVENT)
      ?.isSwingTypeClassificationSupported ?: false
}

운동에서 운동 이벤트 요청

운동을 시작하고 운동의 일부로 운동 이벤트를 요청하려면 운동의 ExerciseConfig를 선언하고 exerciseEventType 필드를 추가하세요.

다음 예는 GOLF 운동의 일부로 GOLF_SHOT_EVENT를 요청합니다.

val config = ExerciseConfig(
  exerciseType = ExerciseType.GOLF,
  dataTypes = setOf(....),
  // ...
  exerciseEventTypes = setOf(ExerciseEventType.GOLF_SHOT_EVENT),
)

운동 이벤트 업데이트 등록

운동 업데이트를 수신하기 위해 앱에 있는 기존 인프라의 일부로 ExerciseEvent 업데이트를 받을 수 있습니다. 다음 예는 GolfShotEvent 업데이트 지원을 통합하는 방법을 보여줍니다.

val callback = object : ExerciseUpdateCallback {
  override fun onExerciseUpdateReceived(update: ExerciseUpdate) {
      ...
  }
  // [ExerciseEvent] intended to come through with low latency and out of
  // band of onExerciseUpdateReceived()
  override fun onExerciseEventReceived(event: ExerciseEvent) {
    when (event) {
      is GolfShotEvent -> {
        if (it.swingType == GolfShotSwingType.PUTT) {
          println("Putt detected!")
        }
      }
    }
  }
}