Health service

cluelin·2021년 11월 26일
0

WearOS

목록 보기
1/3

1.Passive Experiences

2.Active Experiences

Active - 즉각적인 피드백을 받을수있다.

passive - 즉각적이지 않아도 되는 데이터를 사용할때 사용.

*기본적인 환경 설정.

모듈의 build.gradle에 아래와 같이 선언해준다.

dependencies {
  implementation 'androidx.health:health-services-client:1.0.0-alpha02'
  // For Kotlin, this library helps bridge between Futures and coroutines.
  implementation "com.google.guava:guava:30.1.1-android"
  implementation "androidx.concurrent:concurrent-futures-ktx:1.1.0"
}

manifest에 아래와 같이 선언해준다.

<queries>
    <package android:name="com.google.android.wearable.healthservices" />
</queries>

해당 기기에서 원하는 기능을 제공하는지 capability를 확인해야한다.

val healthClient = HealthServices.getClient(this /*context*/)
val passiveMonitoringClient = healthClient.passiveMonitoringClient
lifecycleScope.launchWhenCreated {
    val capabilities = passiveMonitoringClient.capabilities.await()
    // Supported types for passive data collection
    supportsHeartRate =
        DataType.HEART_RATE_BPM in capabilities.supportedDataTypesPassiveMonitoring
    // Supported types for PassiveGoals
    supportsStepsGoal =
        DataType.STEPS in capabilities.supportedDataTypesEvents
}

이경우에는 HEART_RATE_BPM과 STEPS를 제공하는지 확인한다.

*capabilities를 출력해보면 해당 기기가 어떤 기능을 제공하는지 알수있다.

Health 데이터의 변화에 따라 데이터를 전달받기위해

아래와 같이 BroadcastReceiver를 구현하고 manifest에 선언해준다

class BackgroundDataReceiver : BroadcastReceiver() {
   override fun onReceive(context: Context, intent: Intent) {
       // Check that the Intent is for passive data
       if (intent?.action != PassiveMonitoringUpdate.ACTION_DATA) {
           return
       }
       val update = PassiveMonitoringUpdate.fromIntent(intent) ?: return

       // List of available data points
       val dataPoints = update.dataPoints

       // List of available user state info
       val userActivityInfoList = update.userActivityInfoUpdates
   }
}
<receiver
   android:name=".BackgroundDataReceiver"
   android:exported="true">
   <intent-filter>
       <action android:name="hs.passivemonitoring.DATA" />
   </intent-filter>
</receiver>
 

구현한 Receiver를 아래와 같이 등록해주면

HEART_RATE_BPM, STEPS가 변경될때 receiver에서 데이터를 받을수있다. (passive니까 조금 시간차는 있음)

val dataTypes = setOf(DataType.HEART_RATE_BPM, DataType.STEPS)
val config = PassiveMonitoringConfig.builder()
   .setDataTypes(dataTypes)
   .setComponentName(ComponentName(context, BackgroundDataReceiver::class.java))
   // To receive UserActivityState updates, ACTIVITY_RECOGNITION permission is required.
   .setShouldIncludeUserActivityState(true)
   .build()
lifecycleScope.launch {
   HealthServices.getClient(context)
       .passiveMonitoringClient
       .registerDataCallback(config)
       .await()
}

참조
https://github.com/android/health-samples
https://developer.android.com/training/wearables/health-services

0개의 댓글