in health-services/PassiveData/app/src/main/java/com/example/passivedata/PassiveDataReceiver.kt [39:69]
override fun onReceive(context: Context, intent: Intent) {
val state = PassiveMonitoringUpdate.fromIntent(intent) ?: return
// Get the most recent heart rate measurement.
val latestDataPoint = state.dataPoints
// dataPoints can have multiple types (e.g. if the app registered for multiple types).
.filter { it.dataType == DataType.HEART_RATE_BPM }
// where accuracy information is available, only show readings that are of medium or
// high accuracy. (Where accuracy information isn't available, show the reading if it is
// a positive value).
.filter {
it.accuracy == null ||
setOf(
HrAccuracy.SensorStatus.ACCURACY_MEDIUM,
HrAccuracy.SensorStatus.ACCURACY_HIGH
).contains((it.accuracy as HrAccuracy).sensorStatus)
}
.filter {
it.value.asDouble() > 0
}
// HEART_RATE_BPM is a SAMPLE type, so start and end times are the same.
.maxByOrNull { it.endDurationFromBoot }
// If there were no data points, the previous function returns null.
?: return
val latestHeartRate = latestDataPoint.value.asDouble() // HEART_RATE_BPM is a Float type.
Log.d(TAG, "Received latest heart rate in background: $latestHeartRate")
runBlocking {
repository.storeLatestHeartRate(latestHeartRate)
}
}