How to differentiate between manual added steps and sensor recorded steps in Google Fit Api

前端 未结 2 721
终归单人心
终归单人心 2021-01-24 00:35

I am using Google Fit Api in my project to get user\'s daily steps. But the problem is, user can enter the steps manually by adding the activities. And when i retrieve the daily

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-24 01:06

    This is how i solved this issue.

       final DataReadRequest readRequest = new DataReadRequest.Builder()
                .read(googleFitUtils.getEstimatedSteps())
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();
    
        DataReadResult dataReadResult =
                Fitness.HistoryApi.readData(mGoogleApiFitnessClient, readRequest).await(1, TimeUnit.MINUTES);
    
        DataSet stepData = dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);
    
        int totalSteps = 0;
    
        for (DataPoint dp : stepData.getDataPoints()) {
            for(Field field : dp.getDataType().getFields()) {
                int steps = dp.getValue(field).asInt();
                if (!"user_input".equals(dp.getOriginalDataSource().getStreamName()))
                    totalSteps += steps;
            }
        }
    

    First Point - > Before i was getting the total steps using

    Fitness.HistoryApi.readDailyTotal
    

    which returns one data point with total daily steps. Second Point - > Then i changed the way to get the daily steps using

    Fitness.HistoryApi.readData
    

    It returns array of data points with the chunks of daily steps. Each data point has a property of

    dp.getOriginalDataSource().getStreamName()
    

    which returns you the type, either steps were recorded by sensor or it was a use input. That's how you can filter the user input steps to avoid steps hack in your application.

提交回复
热议问题