android - How to filterout manual step count from health connect - Stack Overflow

admin2025-04-15  2

Hi i am having functionality to get user steps in app using health connect in my application. However my functionality breaks because user's are entering huge manual steps through google fit. I am fetching data from health connect and health connect is synced with google fit so huge steps are coming which are not possible to user to walk in a day.

    suspend fun getTodayTotalSteps(healthConnectClient: HealthConnectClient): Long {
    Timber.d("counting_to")
    val now = LocalDateTime.now().with(LocalTime.MAX)
    val startOfDay = now.toLocalDate().atStartOfDay()
    var totalSteps = 0L
    try {
        Timber.d("start_time:::$startOfDay")
        Timber.d("end_time:::$now")
        val response =
            healthConnectClient.readRecords(
                ReadRecordsRequest(
                    StepsRecord::class,
                    timeRangeFilter = TimeRangeFilter.between(startOfDay, now)
                )
            )

        for (record in response.records) {

            if (record.metadata.recordingMethod == RECORDING_METHOD_AUTOMATICALLY_RECORDED) {
                totalSteps += record.count
            }
            Timber.d("total_steps_after_Wallet_creation:::$totalSteps")
        }
        return totalSteps

    } catch (e: Exception) {
        // Run error handling here
        Timber.d("error in getting steps today $e")
    }

    return totalSteps
}

Now ecord.metadata.recordingMethod == RECORDING_METHOD_AUTOMATICALLY_RECORDED recording method always unknown ie, 0

I have entered 2500 steps as manual steps here are follow response :

StepsRecord(startTime=2025-02-04T10:24:12.681Z, startZoneOffset=null, endTime=2025-02-04T10:54:12.682Z, endZoneOffset=null, count=2500, metadata=Metadata(id='c9196ccf-9fa5-4557-9651-7cdcb953ebeb', dataOrigin=DataOrigin(packageName='com.google.android.apps.fitness'), lastModifiedTime=2025-02-04T10:54:34.455Z, clientRecordId=null, clientRecordVersion=0, device=null, recordingMethod=0))

Now my honest entry automatic one are as follows

StepsRecord(startTime=2025-02-04T02:13:42.690Z, startZoneOffset=null, endTime=2025-02-04T02:14:42.691Z, endZoneOffset=null, count=20, metadata=Metadata(id='9752d916-b8f3-4c21-9a19-0e9471098346', dataOrigin=DataOrigin(packageName='com.google.android.apps.fitness'), lastModifiedTime=2025-02-04T02:31:05.696Z, clientRecordId=null, clientRecordVersion=0, device=null, recordingMethod=0))

Any Expert Android Dev Please help. Thanks in advance.

Hi i am having functionality to get user steps in app using health connect in my application. However my functionality breaks because user's are entering huge manual steps through google fit. I am fetching data from health connect and health connect is synced with google fit so huge steps are coming which are not possible to user to walk in a day.

    suspend fun getTodayTotalSteps(healthConnectClient: HealthConnectClient): Long {
    Timber.d("counting_to")
    val now = LocalDateTime.now().with(LocalTime.MAX)
    val startOfDay = now.toLocalDate().atStartOfDay()
    var totalSteps = 0L
    try {
        Timber.d("start_time:::$startOfDay")
        Timber.d("end_time:::$now")
        val response =
            healthConnectClient.readRecords(
                ReadRecordsRequest(
                    StepsRecord::class,
                    timeRangeFilter = TimeRangeFilter.between(startOfDay, now)
                )
            )

        for (record in response.records) {

            if (record.metadata.recordingMethod == RECORDING_METHOD_AUTOMATICALLY_RECORDED) {
                totalSteps += record.count
            }
            Timber.d("total_steps_after_Wallet_creation:::$totalSteps")
        }
        return totalSteps

    } catch (e: Exception) {
        // Run error handling here
        Timber.d("error in getting steps today $e")
    }

    return totalSteps
}

Now ecord.metadata.recordingMethod == RECORDING_METHOD_AUTOMATICALLY_RECORDED recording method always unknown ie, 0

I have entered 2500 steps as manual steps here are follow response :

StepsRecord(startTime=2025-02-04T10:24:12.681Z, startZoneOffset=null, endTime=2025-02-04T10:54:12.682Z, endZoneOffset=null, count=2500, metadata=Metadata(id='c9196ccf-9fa5-4557-9651-7cdcb953ebeb', dataOrigin=DataOrigin(packageName='com.google.android.apps.fitness'), lastModifiedTime=2025-02-04T10:54:34.455Z, clientRecordId=null, clientRecordVersion=0, device=null, recordingMethod=0))

Now my honest entry automatic one are as follows

StepsRecord(startTime=2025-02-04T02:13:42.690Z, startZoneOffset=null, endTime=2025-02-04T02:14:42.691Z, endZoneOffset=null, count=20, metadata=Metadata(id='9752d916-b8f3-4c21-9a19-0e9471098346', dataOrigin=DataOrigin(packageName='com.google.android.apps.fitness'), lastModifiedTime=2025-02-04T02:31:05.696Z, clientRecordId=null, clientRecordVersion=0, device=null, recordingMethod=0))

Any Expert Android Dev Please help. Thanks in advance.

Share Improve this question asked Feb 4 at 11:07 Gaurav KumarGaurav Kumar 1466 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Since the data can be obtained, we must further judge and set a threshold to judge whether the user's behavior is compliant, but we need to consider how to set it, because there are many factors, I only give my opinion, you can calculate the number of steps per minute and determine whether the average number of steps per minute is within a reasonable range. If the average number of steps is abnormal, the record is skipped.

const val MIN_STEPS_PER_MINUTE = 0
const val MAX_STEPS_PER_MINUTE = 200

fun processStepsRecords(response: Response): Int {
var totalSteps = 0

for (record in response.records) {
    if (record.metadata.recordingMethod == RECORDING_METHOD_AUTOMATICALLY_RECORDED) {
        val startTime = Instant.parse(record.startTime)
        val endTime = Instant.parse(record.endTime)
        val durationInMinutes = ChronoUnit.MINUTES.between(startTime, endTime)

        if (durationInMinutes > 0) {
            val stepsPerMinute = record.count / durationInMinutes
            if (stepsPerMinute in MIN_STEPS_PER_MINUTE..MAX_STEPS_PER_MINUTE) {
                totalSteps += record.count
            } else {
                Timber.w("Invalid steps per minute detected: $stepsPerMinute. Skipping this record.")
            }
        } else {
            Timber.w("Zero duration detected between start and end time. Skipping this record.")
        }
    }
}

    Timber.d("total_steps_after_Wallet_creation:::$totalSteps")
    return totalSteps
}
转载请注明原文地址:http://www.anycun.com/QandA/1744724684a86748.html