Search code examples
androidkotlingoogle-apigoogle-fit-api

Tracking total number of steps for current day using Google's FIT API


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[]

Solution

  • 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.