GoogleFit sample not working

I am trying to execute a BasicHistory sample that stores data and then reads it, but in my case the code gets stuck in the call to insert.await does not return anything that I tried to use asynchronously, here is the code

com.google.android.gms.common.api.Status insertStatus = Fitness.HistoryApi.insert(mClient, insertRequest).await(1, TimeUnit.MINUTES); 
0
source share
2 answers

Did you request the correct permissions?

According to this Google Fit sample, only permission for Activity is required. If you change the data type used in this example for others, make sure that you set permissions. See the Google Fit Doc authorization section for more details.

You can add multiple permissions by simply adding a new scope for the 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)) ... 
+1
source

To insert a DataType in google fit, you must create a DataSet of the same type and make sure you create a GoogleApiClient with the appropriate fields as described above. Below is an 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(); } 
0
source

Source: https://habr.com/ru/post/1206871/


All Articles