0

Hi guys I have a problem using the Google Fit Api. I only receive activity data in specific situations.

I'm using the RxFit library (but I had the same behaviour using the default implementation). When I try this on my own phone (Nexus 5X) with my own account it works perfectly fine. If I try a different account on my phone I receive a success response, but no actual activity data. Same goes for other device and other account. And with my own account on the other device it does not work either. The same behaviour occurs when using the emulator.

My implementation:

fun requestActivities(): Single<DataReadResult> {
    val fit = RxFit(context, arrayOf(Fitness.HISTORY_API), arrayOf(Scope(Scopes.FITNESS_ACTIVITY_READ)))
    val dataReadRequest = buildRequest(getStartEndTime())
    return fit.history().read(dataReadRequest).doOnSuccess { storeDatapoints(it) }
}

private fun storeDatapoints(data: DataReadResult) {
    val idlist = activityRepository.all().map { it.activityId }
    val activities = data.buckets
          .flatMap { it.dataSets }
          .flatMap { it.dataPoints }
          .filter { point ->
              //https://developers.google.com/fit/rest/v1/reference/activity-types
              val activity = point.getValue(Field.FIELD_ACTIVITY).asInt()
              return@filter activity != 0 && activity != 3 //&& !(109..112).contains(activity)
          }
          .map { point ->
              PersistentExerciseActivity(
                    activityId = point.timestampNanos.toString(),
                    date = Instant(point.getTimestamp(TimeUnit.MILLISECONDS)).toDateTime().toLocalDateTime(),
                    duration = point.getValue(Field.FIELD_DURATION).asInt() / 1000 / 60,
                    activity = point.getValue(Field.FIELD_ACTIVITY).asActivity(),
                    apiId = null
              )
          }
          .filter { !idlist.contains(it.activityId) }
    activityRepository.store(activities)
}

private fun getStartEndTime(): Pair<Long, Long> {
    val cal = Calendar.getInstance()
    val now = Date()
    cal.time = now

    cal.set(Calendar.HOUR_OF_DAY, 0)
    cal.set(Calendar.MINUTE, 0)
    cal.set(Calendar.MILLISECOND, 0)
    cal.set(Calendar.SECOND, 0)

    val endTime = cal.timeInMillis
    cal.add(Calendar.WEEK_OF_YEAR, -1)
    val startTime = cal.timeInMillis

    return Pair(startTime, endTime)
}

private fun buildRequest(startEndTime: Pair<Long, Long>): DataReadRequest {
    return DataReadRequest.Builder()
          .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
          .bucketByTime(1, TimeUnit.DAYS)
          .setTimeRange(startEndTime.first, startEndTime.second, TimeUnit.MILLISECONDS)
          .enableServerQueries()
          .build()
}

Does anyone have some ideas what would be causing this?

Kind regards,

Bryan

Bryan
  • 46
  • 2
  • Your are receiving data when you use your own email because your email was used to [Get an OAuth 2.0 Client ID](https://developers.google.com/fit/android/get-api-key). Now when you use other accounts, it doesn't recognize them. To test this, use a different email and use the OAUTH client ID from its GDC and use that emai to log-inl and fetch Fit data. Check this [SO thread](http://stackoverflow.com/questions/38049767/google-fit-api-steps-returns-0-for-some-users) for additional info. – ReyAnthonyRenacia Mar 08 '17 at 15:57
  • Hey noogui, thanks for your answer. I tried it with a different email (and device). When I created an OAUTH id for this address it worked and successfully retrieved Fit data. However it still won't work on all accounts. I'm not sure what I am supposed to do, as creating an OAUTH ID for every potential user is not an option. – Bryan Mar 15 '17 at 21:47
  • Hey Bryan - curious if you found a solution to this? – Dan Jul 16 '18 at 05:24
  • No I haven't found a solution, – Bryan Jul 20 '18 at 12:01

0 Answers0