Android - HealthConnectClient.readRecords or aggregateGroupByPeriod takes forever

160 views Asked by At

Below codes takes forever to execute. Get it from official docs. HealthConnect is installed on the device.I believe there is an inner error that i cant see in logs of IDE. Code is called from java class.

What can cause this?

companion object {
    fun readStepsByTimeRange(
        healthConnectClient: HealthConnectClient,
        startTime: Instant,
        endTime: Instant
    ) : CompletableFuture<Long> = GlobalScope.future {
        innerReadStepsByTimeRange(healthConnectClient, startTime, endTime)
    }

    private suspend fun innerReadStepsByTimeRange (
        healthConnectClient: HealthConnectClient,
        startTime: Instant,
        endTime: Instant
    ) : Long {
        /*var totalStepsCount = 0L
        val response =
            healthConnectClient.readRecords(
                ReadRecordsRequest(
                    StepsRecord::class,
                    timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
                )
            )
        for (stepRecord in response.records) {
            totalStepsCount += stepRecord.count;
        }
        return totalStepsCount*/
        val startTime = LocalDateTime.now().minusMonths(1)
        val endTime = LocalDateTime.now()

        try {

            val response =
                healthConnectClient.aggregateGroupByPeriod(
                    AggregateGroupByPeriodRequest(
                        metrics = setOf(StepsRecord.COUNT_TOTAL),
                        timeRangeFilter = TimeRangeFilter.between(startTime, endTime),
                        timeRangeSlicer = Period.ofDays(1)
                    )
                )

            return response[0].result[StepsRecord.COUNT_TOTAL]!!
        }catch (e: Exception) {
            return -1
        }
    }
}

Java caller code below

CompletableFuture<Long> totalStepsCountCF = StepReaderUtils.Companion.readStepsByTimeRange(
        healthConnectClient, startDate, endDate);
return totalStepsCountCF.get();

build.gradle

implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.8.0'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0'
implementation "androidx.health.connect:connect-client:1.0.0-alpha08"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.6.4"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
0

There are 0 answers