Google Fit History API returns empty dataset [ ]
Opened this issue ยท 39 comments
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.
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 ๐
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
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
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.
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.
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?
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.
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.
Hi Done that. And its working fine. Thanks.
Hi @rajatanand86 same issue for me . can you please describe me how you solve this problem. Thanks
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?
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
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.
After that try to check the data on the Android studio as well as on the console
@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
You're using java or kotlin? I can share a sample code. You can log the response
@ummesulaith xamarin forms
or hit api direct form https://developers.google.com/oauthplayground/
Ok I will check the api and share
thanks
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
Have you tried these ?
yes
first one is for get request and 2nd is for post
I hope for the console also you're using same account to view the data and to perform activity.
of course
can you share the response of both the API I have mentioned above with their method as post.
Okay. Where you said you're using google fit api on mobile or web site?
@ummesulaith xamarin forms
or hit api direct form https://developers.google.com/oauthplayground/
for testing purpose i use developers.google.com/oauthplayground/
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.
i already create my project in google console or use the client id to my xamarin project. thanks anyway for help
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.
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!
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?
@rakshahegde10 Yes you need to run it in your phone instead of the emulator to fetch datapoints and buckets
@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.
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