0

I trying to count the step from user. I'm using google fit api but there is something that I'didn't get. First of all Here is my code:

    private void setUpGoogleApiClient() {
    apiClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.SENSORS_API)
            .addApi(ActivityRecognition.API)
            .addApi(Fitness.HISTORY_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

 @Override
public void onDataPoint(DataPoint dataPoint) {
    for (final Field field : dataPoint.getDataType().getFields()) {
        final Value value = dataPoint.getValue(field);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stepCount.setText(value.toString());
                if (DetectManager.getInstance(MainActivity.this).getMovementAsJson() != null)
                    fieldName.setText(DetectManager.getInstance(MainActivity.this).getMovementAsJson().getMovement());
                totalKmsDone = (float) value.asInt();
                Log.e("Steps: ", "Value " + value.toString());

                totalCaloriesBurned.setText(calories.calculateCalories((int) totalKmsDone, MainActivity.this));
                distance.setText(calories.calculateDistance((int) totalKmsDone, MainActivity.this));
                historyManager.animateTheView((int) (totalKmsDone / 10000 * 100), value.asInt(), Constant.AT_THE_MOMENT_STEP_COLOR, goal_indicator_view, null);
            }
        });
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Intent intent = new Intent(this, ActivityRecognizedService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, 3000, pendingIntent);

    DataSourcesRequest dataSourceRequest = new DataSourcesRequest.Builder()
            .setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)
            .setDataSourceTypes(DataSource.TYPE_DERIVED)
            .build();

    ResultCallback<DataSourcesResult> dataSourcesResultCallback = new ResultCallback<DataSourcesResult>() {
        @Override
        public void onResult(DataSourcesResult dataSourcesResult) {
            for (DataSource dataSource : dataSourcesResult.getDataSources()) {
                if (DataType.TYPE_STEP_COUNT_CUMULATIVE.equals(dataSource.getDataType())) {
                    registerFitnessDataListener(dataSource, DataType.TYPE_STEP_COUNT_CUMULATIVE);
                }
            }
        }
    };
    Fitness.SensorsApi.findDataSources(apiClient, dataSourceRequest).setResultCallback(dataSourcesResultCallback);
}

    private void registerFitnessDataListener(DataSource dataSource, DataType dataType) {

    SensorRequest request = new SensorRequest.Builder()
            .setDataSource(dataSource)
            .setDataType(dataType)
            .setSamplingRate(3, TimeUnit.SECONDS)
            .build();

    Fitness.SensorsApi.add(apiClient, request, this)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if (status.isSuccess()) {

                    }
                }
            });
}

My question is: 1. How to reset the value (dataPoint.getValue(field);) daily. I wanna just count the steps from user daily.

  1. I changed DataType.TYPE_STEP_COUNT_CUMULATIVE to DataType.TYPE_STEP_COUNT_DELTA but it didn't work. I tested again and again but it didn't work with the DataType.TYPE_STEP_COUNT_DELTA. I think that is the problem Can anyone tell me how can I invoke the DataType.TYPE_STEP_COUNT_DELTA ?

thank you for helping.

Olcay Sönmez
  • 602
  • 1
  • 7
  • 16
  • You might wanna check this [similar thread](http://stackoverflow.com/questions/22649324/step-counter-doesnt-reset-the-step-count). – ReyAnthonyRenacia Oct 29 '16 at 18:15
  • Hi @noogui actually I don't use the sensorapi for that. I use google fit api and count the stpes with onDataPoint func. which is override so I think the link above give me some idea but it didn't work. – Olcay Sönmez Oct 31 '16 at 11:50

0 Answers0