Google Fit Data

我只是一个虾纸丫 提交于 2019-12-08 10:47:24

问题


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

来源:https://stackoverflow.com/questions/42667954/google-fit-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!