Fetch daily steps and calorie using Google Fit API

Google Fit

Anu S Pillai

The Admin at GadgetSaint.com

You may also like...

8 Responses

  1. Rohit Dhawade says:

    Very good.It’s help me a lot to integrate Google fit

  2. Ser says:

    Good tutorial, but i have a problem! When I run the application, just when I push the button to obtain the calories, the app closes without any error, what is the problem?

  3. gadgetsaint says:

    Can you post your Log?

  4. Lucian says:

    I have the same problem, the app is crashing when i open it, this is the log:

    05-23 15:29:47.350 15291-15758/com.ivan.lucian.fitbuddy31 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.ivan.lucian.fitbuddy31, PID: 15291
    java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:309)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
    Caused by: java.lang.IllegalStateException: Value is not in int format
    at com.google.android.gms.common.internal.zzac.zza(Unknown Source)
    at com.google.android.gms.fitness.data.Value.asInt(Unknown Source)
    at com.ivan.lucian.fitbuddy31.MainActivity$FetchCalorieAsync.doInBackground(MainActivity.java:380)
    at com.ivan.lucian.fitbuddy31.MainActivity$FetchCalorieAsync.doInBackground(MainActivity.java:370)
    at android.os.AsyncTask$2.call(AsyncTask.java:295)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
    at java.lang.Thread.run(Thread.java:818) 

    for what i understand this line is the problem:

    : totalSet.getDataPoints().get(0).getValue(Field.FIELD_CALORIES).asInt();
    **** from log (Caused by: java.lang.IllegalStateException: Value is not in int format)*****

  5. Khang Tran says:

    @Lucian: You right, problems came from there. I fixed by changing to asFloat() and change the get calories burned AsynTask to:

    private class FetchCalorieAsync extends AsyncTask {
    protected Float doInBackground(Object… params) {
    float total = 0;
    PendingResult result = Fitness.HistoryApi.readDailyTotal(mGoogleClient, DataType.TYPE_CALORIES_EXPENDED);
    DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
    if (totalResult.getStatus().isSuccess()) {
    DataSet totalSet = totalResult.getTotal();
    if (totalSet != null) {
    total = totalSet.isEmpty()
    ? 0
    : totalSet.getDataPoints().get(0).getValue(Field.FIELD_CALORIES).asFloat();
    }//1205.4213
    } else {
    Log.w(“GoogleFit”, “There was a problem getting the calories.”);
    }
    return total;
    }

    @Override
    protected void onPostExecute(Float aLong) {
    super.onPostExecute(aLong);

    //Total calories burned for that day
    Log.i(“GoogleFit”, “Total calories: ” + aLong);

    }
    }

  6. ANAND K says:

    Are all the Google Fit APIs available for free if I use them in android apps that I will monetize ? If not, where can I find the pricing info ?

  7. Anu S Pillai says:

    I think its free and no issues monetizing the app.

  8. Chaitanya says:

    Im currently facing a problem with the code similar to yours. There is a total mismatch between the actual data and the data this code loads up! I have opened Google Fit Web on my desktop and I have my android app running on my device. I have a service which reads todays step count and calories every minute. For example, the initial count is 50 steps. Then I walked for few minutes and then checked the log. There is no increment in the step count for about 30 minutes, it was still 50. Then I refreshed the google fit web page, which loaded up the new steps and it has shown 90 steps which is expected. After that refresh even my log data got new step count of 90!

    1) So, it means Google fit store doesn’t frequently refresh/load data from all the devices of the person? Or if it loads up, how frequently?
    2) Do you know if there is any way we refresh the Google fit store every time before reading History API data? (The thing which refreshing the google fit web page does)

    I actually doubt it Google fit store loads up data from all the fitness devices of a person only when we open the app or if we open/refresh the web page!

    Do you have any idea/suggestion regarding this?

Leave a Reply

Your email address will not be published.