GoogleFit Sample Not Working

对着背影说爱祢 提交于 2019-12-02 07:41:38

Have you requested the right permissions?

According to that Google Fit sample, it only requires permission for "Activity". If you change the data type used in that sample for other different, make sure you are setting right permissions. Read authorization section in Google Fit Doc for more information.

You can add several permissions just adding a new scope to client:

    // Create the Google API Client
    mClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
    ...

To insert a DataType into google fit you must create a DataSet of the same type and make sure you built GoogleApiClient with appropriate Scopes Like jose said above. Below is the example of inserting Height in google fit

public boolean saveUserHeight(int heightCentimiters) {
    // to post data
    float height = ((float) heightCentimiters) / 100.0f;
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.DAY_OF_YEAR, -1);
    long startTime = cal.getTimeInMillis();
    DataSet heightDataSet = createDataForRequest(DataType.TYPE_HEIGHT, // for
            DataSource.TYPE_RAW, height, // weight in kgs
            startTime, // start time
            endTime, // end time
            TimeUnit.MILLISECONDS // Time Unit, for example,
                                    // TimeUnit.MILLISECONDS
    );
    com.google.android.gms.common.api.Status heightInsertStatus = Fitness.HistoryApi
            .insertData(fitnessClient, heightDataSet).await(1,
                    TimeUnit.MINUTES);
    if (heightInsertStatus.isSuccess()) {
        //Log.e("Height", heightCentimiters+"Inserted");
    } else {
        //Log.e("Height", "inserted failed");
    }
    return heightInsertStatus.isSuccess();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!