0

I use HealthConnect to get data about user steps over a certain period of time.

For more security, I filter the activity source using the data originFilter field in AggregateRequest. Thus, the data comes to my application only from Google Fit.

However, Google Fit provides the ability to manually add step values and synchronize this data with HealthConnect. Accordingly, my application may receive incorrect data about the number of steps that the user actually took. Is it possible to avoid such a situation?

    override suspend fun steps(range: Pair<LocalDateTime, LocalDateTime>): Long { 
        val client = HealthConnectClient.getOrCreate(context)
        
        val aggregateRequest = AggregateRequest(
            setOf(StepsRecord.COUNT_TOTAL), 
            TimeRangeFilter.between(range.first, range.second), 
            supportedPackages
        ) 
        val aggregateData = client.aggregate(aggregateRequest)
        
        return aggregateData[StepsRecord.COUNT_TOTAL] ?: 0 
    }

Update: I found out that it is possible to filter such data using the metadata class.

        val response = client.readRecords(
           ReadRecordsRequest(
               StepsRecord::class,
               dataOriginFilter =  supportedPackages,
               timeRangeFilter = TimeRangeFilter.Companion.between(range.first, range.second)
           )
        )
        val steps = response.records.filter { it.metadata.recordingMethod != 3 }.sumOf { it.count }

But Google Fit doesn't fill this field when it syncs data with HealthConnect so recording Method for all records is RECORDING_METHOD_UNKNOWN.

0 Answers0