androidandroid-studiokotlingoogle-fithealthconnect

health connect still provides data where there should be no data


i try to collecting data from 1 april 2023 to 30 april 2023, today is 20 april 2023 but health connect still provides data for > 20 april 2023 like this data from health connect

i don't know where the data is coming from, i just sync google fit data to health connect, when i check health connect, there is no data from > 20 april 2023, same case if for example i have no data in february but health connect still give a data, i guess that is default value? this is my code

override suspend fun getMonthlyHistoriesCalorie(parameter: GetMonthlyHistoriesCalorieParameter): GetMonthlyHistoriesCalorieResponse {
        val result = mutableListOf<Double>()

        if (isHealthConnectSDKAvailable(context)) {
            val healthConnectClient = HealthConnectClient.getOrCreate(context)
            val response = healthConnectClient.aggregateGroupByPeriod(
                AggregateGroupByPeriodRequest(
                    metrics = setOf(TotalCaloriesBurnedRecord.ENERGY_TOTAL),
                    timeRangeFilter = TimeRangeFilter.between(
                        startTime = LocalDateTime.ofInstant(parameter.startDate, ZoneId.systemDefault()),
                        endTime = LocalDateTime.ofInstant(parameter.endDate, ZoneId.systemDefault())
                    ),
                    timeRangeSlicer = Period.ofDays(1)
                )
            )


            for (monthlyResult in response) {
                val totalCalorieBurned =
                    monthlyResult.result[TotalCaloriesBurnedRecord.ENERGY_TOTAL]?.inKilocalories


                if (totalCalorieBurned != null) {
                    result.add(totalCalorieBurned)
                }
            }

        }

        return GetMonthlyHistoriesCalorieResponse(
            histories = result,
            period = DateTimeFormatter.ofPattern("MMMM yyyy").withZone(ZoneId.systemDefault())
                .format(parameter.endDate),
            total = result.sum()
        )
    }

this only happens with data type TotalCalorieBurned and using aggregate, another example is when i set the date into december 2022 (no data here) and i check the data in health connect app with the same date, there is no data

no data in health connect

but when i red in my app, health connect given data

health connect given data that must be no data

my health connect version androidx.health.connect:connect-client:1.0.0-alpha11


Solution

  • i found the problem, when i checked "dataOrigins" i found unknown data origin with package name __platform print data origin

    i solve this by filtered the data like this

    for (dataOrigins in weeklyResult.result.dataOrigins){
      if (dataOrigins.packageName == DataOrigins.GoogleFit.packageName)
       // code here
    }