Vishnudev K
Vishnudev K

Reputation: 2934

Adding manual inputs to Google Fit

Is it possible to send manual inputs(distance walked/run) to Google Fit?
My intention is to make a simple app which can pass the the user entered number of steps or miles walked into Google Fit.
So that user can enter his own readings from other fitness devices which did not have Google Fit integration.

Upvotes: 1

Views: 2922

Answers (2)

Maulik
Maulik

Reputation: 379

Yes you can do that by following code. Detail description you will find here

 Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    cal.add(Calendar.MINUTE, 0);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.MINUTE, -50);
    long startTime = cal.getTimeInMillis();

    // Create a data source
    DataSource dataSource = new DataSource.Builder()
            .setAppPackageName(activity)
            .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
            .setStreamName(TAG + " - step count")
            .setType(DataSource.TYPE_RAW)
            .build();

    // Create a data set
    DataSet dataSet = DataSet.create(dataSource);
    DataPoint dataPoint = dataSet.createDataPoint()
            .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
    dataPoint.getValue(Field.FIELD_STEPS).setInt((int) steps);
    dataSet.add(dataPoint);

    DataUpdateRequest request = new DataUpdateRequest.Builder()
            .setDataSet(dataSet)
            .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

    com.google.android.gms.common.api.Status updateStatus =
            Fitness.HistoryApi.updateData(mApiClient, request)
                    .await(1, TimeUnit.MINUTES);

Upvotes: 1

Chris D
Chris D

Reputation: 820

Yes look at working with Fitness History, you can manually intput data using the History API

Upvotes: 2

Related Questions