0

I am trying to get the total number of steps for the current day using Google's FIT API. I'm using the code snippets from https://developers.google.com/android/reference/com/google/android/gms/fitness/HistoryClient

This is the particular code snippet which I'm using from that page

    val currentTime = Calendar.getInstance()
        val midNight = Calendar.getInstance()
        val now = Date()
        midNight.apply {
            time = now
            set(Calendar.MILLISECOND, 0)
            set(Calendar.SECOND, 0)
            set(Calendar.MINUTE, 0)
            set(Calendar.HOUR, 0)
        }

        val response: Task<DataReadResponse> =
        Fitness.getHistoryClient(context, GoogleSignIn.getLastSignedInAccount(context)!!)
            .readData(
                DataReadRequest.Builder()
                    .read(TYPE_STEP_COUNT_DELTA)
                    .setTimeRange(
                        midNight.timeInMillis,
                        currentTime.timeInMillis,
                        TimeUnit.MILLISECONDS
                    )
                    .bucketByTime(1, TimeUnit.DAYS)
                    .build()
            )

//        val readDataResult: DataReadResponse? = Tasks.await(response)
        .addOnSuccessListener {
                Log.d("Test", "Buckets "+it.buckets.toString())

                for (bucket in it.buckets) {
                    Log.d("Test","Bucket "+bucket)
                    val dataSets = bucket.dataSets
                    for (dataSet in dataSets) {
                        Log.d("Test","dp's"+dataSet.dataPoints.toString())
                        for (dataPoint in dataSet.dataPoints) {
                            Log.d("Datapoint", dataPoint.toString())
                        }
                    }
                }
            }
            .addOnFailureListener{
                Log.d("Test","Failed "+it.toString())
            }

According to the google snippet I had to put the main thread on hold but that also throws an error. So I have commented the code and instead, used onSuccessListener

The data in buckets is

Buckets [Bucket{startTime=1589814000000, endTime=1589818566423, activity=4, dataSets=[DataSet{d:step_count.delta:gms:overlay_explicit_input_local []}], bucketType=time, serverHasMoreData=true}]

Bucket Bucket{startTime=1589814000000, endTime=1589818566423, activity=4, dataSets=[DataSet{d:step_count.delta:gms:overlay_explicit_input_local []}], bucketType=time, serverHasMoreData=true}

but the data points are empty. (I suppose this is where steps are stored)

dp's[]
Anirudh Ganesh
  • 446
  • 4
  • 22
  • Could you clarify why you're not able to use midnight.getMillis() and now.getMillis()? – Egor May 16 '20 at 17:34
  • I've edited the content. It throws an 'unresolved reference' error – Anirudh Ganesh May 16 '20 at 18:06
  • Are those variables declared in your code? – Egor May 16 '20 at 18:11
  • No. I thought they'd be built-in variables. I have no idea what type of variables should be declared. If it is of type calendar, I'm confused as of how to set the time limits. I can get the current time but I'm thinking of how to set it so that data can be collected from current day's midnight till the end of the day (12:00am to 11:59 pm of that day). – Anirudh Ganesh May 16 '20 at 18:25
  • 1
    Looking at the code, `setTimeRange` allows you to specify the `TimeUnit`, the example above uses `TimeUnit.MILLISECONDS`, which is probably the most convenient option for this use case. Obtaining the current time in millis using Java's API is trivial, you should be able to figure it out in no time. Getting the midnight time isn't much harder, look into Java's Calendar API. – Egor May 16 '20 at 18:30
  • I've edited the code. Now it says the user must be signed it but I am able to print the user's displayname. If I return 0 as the value, the app works fine and the user is signed in! Also I don't know whether updating questions is a good practice in Stackoverflow. – Anirudh Ganesh May 17 '20 at 06:27

1 Answers1

0

Well....Apparently I was testing the code on AVD and Google FIT wasn't installed in the AVD. Even if one installs google fit in AVD, steps data won't be displayed (I know it won't be counted but I was expected it to get synced across devices). I installed the app on a physical device along with Google FIT and it worked.

Anirudh Ganesh
  • 446
  • 4
  • 22