Google Fit数据

时间:2017-03-08 09:49:19

标签: android google-fit google-fit-sdk

大家好我使用Google Fit Api时遇到问题。我只收到特定情况下的活动数据。

我正在使用RxFit库(但我使用默认实现具有相同的行为)。当我在自己的手机(Nexus 5X)上使用我自己的帐户试用它时,它可以正常工作。如果我在手机上尝试使用其他帐户,则会收到成功回复,但没有实际活动数据。其他设备和其他帐户也是如此。而我自己的帐户在另一台设备上也不起作用。使用模拟器时会出现相同的行为。

我的实施:

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()
}

有没有人有一些想法会导致这种情况?

亲切的问候,

布赖恩

0 个答案:

没有答案