最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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
}
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>