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).
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 ->
val activity = point.getValue(Field.FIELD_ACTIVITY).asInt()
return@filter activity != 0 && activity != 3
}
.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()
}
What I have tried:
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.
Does anyone have some ideas what would be causing this?
Kind regards,
Roelof