valheartRateCallback=object:MeasureCallback{overridefunonAvailabilityChanged(dataType:DeltaDataType<*,*>,availability:Availability){if(availabilityisDataTypeAvailability){// Handle availability change.}}overridefunonDataReceived(data:DataPointContainer){// Inspect data points.}}valhealthClient=HealthServices.getClient(this/*context*/)valmeasureClient=healthClient.measureClient// Register the callback.measureClient.registerMeasureCallback(DataType.Companion.HEART_RATE_BPM,heartRateCallback)// Unregister the callback.awaitClose{runBlocking{measureClient.unregisterMeasureCallbackAsync(DataType.Companion.HEART_RATE_BPM,heartRateCallback)}}
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Take spot health measurements with MeasureClient\n\nWith the\n[`MeasureClient`](/reference/androidx/health/services/client/MeasureClient)\nAPI, your app registers callbacks to receive data for a short amount of time.\nThis is meant for situations in which your app is in use and requires rapid data\nupdates. If possible, create this with a foreground UI so that the user is\naware.\n| **Note:** `MeasureClient` is not suitable for workout tracking. Instead, [record an\n| exercise](/training/wearables/health-services/active-data/exercise-client) using `ExerciseClient`.\n\nAdd dependencies\n----------------\n\nTo add a dependency on Health Services, you must add the Google Maven repository\nto your project. For more information, see\n[Google's Maven repository](/studio/build/dependencies#google-maven).\n\nThen, in your module-level `build.gradle` file, add the following dependency: \n\n### Groovy\n\n```groovy\ndependencies {\n implementation \"androidx.health:health-services-client:1.1.0-alpha05\"\n}\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n implementation(\"androidx.health:health-services-client:1.1.0-alpha05\")\n}\n```\n| **Note:** This API is asynchronous and relies on `ListenableFuture` extensively. See [Using a ListenableFuture](/guide/background/listenablefuture) for more information about this concept.\n\nCheck capabilities\n------------------\n\nBefore registering for data updates, check that the device can provide the type\nof data your app needs. By checking capabilities first, you can enable or\ndisable certain features or modify your app's UI to compensate for capabilities\nthat are not available.\n\nThe following example shows how to check whether a device can provide the\n`HEART_RATE_BPM` data type: \n\n val healthClient = HealthServices.getClient(this /*context*/)\n val measureClient = healthClient.measureClient\n lifecycleScope.launch {\n val capabilities = measureClient.getCapabilitiesAsync().await()\n supportsHeartRate = DataType.HEART_RATE_BPM in capabilities.supportedDataTypesMeasure\n }\n\nRegister for data\n-----------------\n\n| **Note:** Request necessary permissions before registering to receive data that requires a permission.\n\nEach callback you register is for a single data type. Note that some data types\nmight have varying states of availability. For example, heart rate data might not\nbe available when the device is not properly attached to the wrist.\n\nIt's important to minimize the amount of time that your callback is registered,\nas callbacks cause an increase in sensor sampling rates, which in turn increases\npower consumption.\n\nThe following example shows how to register and unregister a callback to receive\n`HEART_RATE_BPM` data: \n\n val heartRateCallback = object : MeasureCallback {\n override fun onAvailabilityChanged(dataType: DeltaDataType\u003c*, *\u003e, availability: Availability) {\n if (availability is DataTypeAvailability) {\n // Handle availability change.\n }\n }\n\n override fun onDataReceived(data: DataPointContainer) {\n // Inspect data points.\n }\n }\n val healthClient = HealthServices.getClient(this /*context*/)\n val measureClient = healthClient.measureClient\n\n // Register the callback.\n measureClient.registerMeasureCallback(DataType.Companion.HEART_RATE_BPM, heartRateCallback)\n\n // Unregister the callback.\n awaitClose {\n runBlocking {\n measureClient.unregisterMeasureCallbackAsync(DataType.Companion.HEART_RATE_BPM, heartRateCallback)\n }\n }\n\n| **Note:** Kotlin developers can use `callbackFlow` to take advantage of coroutines and lifecycle. See the [Measure Data sample](https://github.com/android/health-samples/tree/main/health-services/MeasureDataCompose) on GitHub for an example."]]