android / fit-samples

Multiple samples showing the best practices using the Fit APIs on Android.
Apache License 2.0
151 stars 113 forks source link

Google Fit History API returns empty dataset [ ] #36

Open LakshyasukhraliaHK opened 5 years ago

LakshyasukhraliaHK commented 5 years ago

Hi,

I ran the the sample to get steps using history Api as mentioned in the link: https://developers.google.com/fit/android/get-started

But I am getting empty [ ] response from the Api regardless of the interval time.

Here is my code:

private fun accessGoogleFit() { val cal = Calendar.getInstance() cal.setTime(Date()) val endTime = cal.getTimeInMillis() cal.add(Calendar.MONTH, -1) val startTime = cal.getTimeInMillis()

val readRequest = DataReadRequest.Builder()
  .aggregate(TYPE_STEP_COUNT_DELTA, AGGREGATE_STEP_COUNT_DELTA)
  .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
  .enableServerQueries()
  .bucketByTime(1, TimeUnit.DAYS)
  .build()

Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this)!!)
  .readData(readRequest)
  .addOnSuccessListener(OnSuccessListener<DataReadResponse> {

    Log.i("DATASET",it.dataSets.toString())
    textView35.text = it.dataSets.toString()
    Log.d(
      "SUCCESS_TAG",
      "onSuccess()"
    )
  })
  .addOnFailureListener(OnFailureListener { e -> Log.e("FAILED_TAG", "onFailure()", e) })
  .addOnCompleteListener(OnCompleteListener<DataReadResponse> {
    Log.d(

      "COMPLETE TAG",
      "onComplete()"
    )
  })

}

Any help would be appreciated.

froimsonm commented 5 years ago

I fell into the same trap. You are reading datasets from response. But according to the documentation Aggregate data is presented in buckets, while detailed data is returned as a single data set (https://developers.google.com/android/reference/com/google/android/gms/fitness/HistoryClient.html#public-taskdatareadresponse-readdata-datareadrequest-request).

So, try to read it.buckets instead, and you will see a data set in each bucket 👍

AniG007 commented 4 years ago

Hi, I am using buckets, but the steps don't appear inside the buckets. Even if I remove aggregate and replace it with read(TYPE_STEP_COUNT_DELTA), Time ranges are fine. Permission exists

Solved: Apparently I didn't have Gfit installed in the device and it seems that in virtual emulator, the step sync doesn't happen even if the user has another device using which steps are tracked

m-szczerba commented 4 years ago

yooo i have the same problem. For me, the data appears fine when i have google fit installed on the device. But on the same google account on other device (which doesn't have gfit installed) all the datasets are empty

AniG007 commented 4 years ago

yooo i have the same problem. For me, the data appears fine when i have google fit installed on the device. But on the same google account on other device (which doesn't have gfit installed) all the datasets are empty

Yup, you need gfit installed in order to get the steps from the bucket.

m-szczerba commented 4 years ago

Yup, you need gfit installed in order to get the steps from the bucket.

Nah but you need to subscribe to google fit Recording API with the appropriate dataTypes if GFit is not installed.

tapanbasak12 commented 4 years ago

How do I aggregate different Datatypes and print, for example if had to add steps and heartrate summary in the logcat, how would I do it?

ummeSuleyath commented 4 years ago

Hi Folks,

I'm retrieving total steps taken by the user and active mins for the last 7 days but the fit API does not retrieve any data for few days(the days where the user had not taken any steps or if the active mins are 0). I have been trying to use the sample java code provided by the googleFit sample but I'm not sure how do I set the datapoints values as zero for the day's total steps or active mins are zero.

rajatanand86 commented 4 years ago

image Hi I am getting a response like this . Can you help.

ummeSuleyath commented 4 years ago

Hi @rajatanand86, Download google fit on mobile and link the same account whatever you are using to check on console and see what is the data u r getting 0 or any values.

rajatanand86 commented 4 years ago

Hi Done that. And its working fine. Thanks.

shazabnku commented 4 years ago

Hi @rajatanand86 same issue for me . can you please describe me how you solve this problem. Thanks

rajatanand86 commented 4 years ago

Download google fit on mobile and link the same account whatever you are using to check on console and see what is the data u r getting 0 or any values.

Does this help?

shazabnku commented 4 years ago

hi @rajatanand86 i already install google fit app in my mobile and connected the same account in my google fit app it have count but when i hit this api i get response something like this {FA4E8591-C894-4618-8322-FC898EE94CF5} png

ummesulaith commented 4 years ago

You're getting empty dataset because you have not performed any activity. Start an walking activity and walk around with your device for 3-5 mins.

ummesulaith commented 4 years ago

After that try to check the data on the Android studio as well as on the console

shazabnku commented 4 years ago

Screenshot_20201109-144033 Screenshot_20201109-144052 @ummesulaith in my google fit app it shows data like total step count but when i hit google fit api for getting steps counts it show empty dataset

ummesulaith commented 4 years ago

You're using java or kotlin? I can share a sample code. You can log the response

shazabnku commented 4 years ago

@ummesulaith xamarin forms or hit api direct form https://developers.google.com/oauthplayground/

ummesulaith commented 4 years ago

Ok I will check the api and share

shazabnku commented 4 years ago

thanks

ummesulaith commented 4 years ago

Method: POST URL: https://www.googleapis.com/fitness/v1/users/me/dataSources

Method : POST URL: https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate

ummesulaith commented 4 years ago

Have you tried these ?

shazabnku commented 4 years ago

yes

shazabnku commented 4 years ago

first one is for get request and 2nd is for post

ummesulaith commented 4 years ago

I hope for the console also you're using same account to view the data and to perform activity.

shazabnku commented 4 years ago

of course

ummesulaith commented 4 years ago

can you share the response of both the API I have mentioned above with their method as post.

shazabnku commented 4 years ago

{5CCEE2BA-48C4-4279-8145-03F59C17079F} png

shazabnku commented 4 years ago

{CE2B992C-F1CE-48A0-9F9D-CF018D6ADBD6} png

ummesulaith commented 4 years ago

Okay. Where you said you're using google fit api on mobile or web site?

shazabnku commented 4 years ago

@ummesulaith xamarin forms or hit api direct form https://developers.google.com/oauthplayground/

for testing purpose i use developers.google.com/oauthplayground/

ummesulaith commented 4 years ago

Ohh okay. May be I know why are you getting the response as null. You have to request OAuth 2.0 client id. Register you're project details with SHA key and then execute the project it will show you the data. You can find the steps in fit rest api get started. As per to my knowledge I don't think so it will show you the google fit of the application data. I'm sorry that went out of my mind. Please use the sample code and then do the steps. It will help you.

shazabnku commented 4 years ago

i already create my project in google console or use the client id to my xamarin project. thanks anyway for help

ummesulaith commented 4 years ago

https://github.com/googlearchive/android-fit-xamarin/blob/master/XamFit/MainActivity.cs just use the sample code to perform some activity with the project and you should receive the data. No Problem.

piotrkst commented 3 years ago

Hey guys, I have run into the same problem - valid buckets, empty data sets.

I have managed to overcome it by using deprecated methods from historical API sample: return DataReadRequest.Builder() .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA) .bucketByTime(1, TimeUnit.DAYS) .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS) .build()

Hope this will save someone's day, enjoy!

rakshahegde10 commented 3 years ago

So this cannot be done using the android emulator is it? I am running my app on the emulator, it doesn't seems to return any data point values for my steps. (I entered the step count on my phone's google fit app) So I have to run my app on my phone instead of emulator?

AniG007 commented 3 years ago

@rakshahegde10 Yes you need to run it in your phone instead of the emulator to fetch datapoints and buckets

divyanshrws commented 3 years ago

@piotrkst I'm also facing the same problem, is there an option to get data using historical API in REST APIs. I'm working on a web application.

lambui09 commented 7 months ago
private fun createDataRequest(startTime: Long, endTime: Long) =
        DataReadRequest.Builder()
            .read(DataType.TYPE_HEART_RATE_BPM)
            .enableServerQueries()
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build()

=====

 GoogleSignIn.getLastSignedInAccount(applicationContext)?.let {
                Fitness.getHistoryClient(
                    applicationContext,
                    it
                )
                    .readData(createDataRequest(startTime, endTime))
                    .addOnFailureListener { onError ->
                        Timber.e("#####---onError -- ${onError.message} -- ${onError.cause} ")
                    }
                    .addOnSuccessListener { response ->
                    }

I want to get data of 2 months ago, data 86400 record, data return 50023 error. but setLitmit 1000. it's okay