GoogleFit Sample Not Working

后端 未结 2 571
有刺的猬
有刺的猬 2021-01-29 02:36

I am trying the BasicHistory Sample whis stores a data and then read it but in my case the code get stuck at the insert call.await call does not return anything i have tried usi

2条回答
  •  伪装坚强ぢ
    2021-01-29 03:02

    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();
    }
    

提交回复
热议问题