Google适合空数据集

时间:2019-05-14 07:36:06

标签: android google-fit

我正在使用Google Fit开发计步器。
我尝试实施Google Fit文档中定义的方法。但是问题在于,当我查询步数时,它总是返回空数据集,而当我使用readDailyTotal函数查询时,它返回一个数据集。
我找不到原因。

  1. 我获得了所需的步数计数权限
val fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)  
.addDataType(DataType.TYPE_STEP_COUNT_CUMULATIVE, FitnessOptions.ACCESS_READ)  
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)  
.build()

if (! GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(activity),  
fitnessOptions)) {
    GoogleSignIn.requestPermissions(
        activity, // your activity
        REQUEST_CODE_GOOGLE_FIT_PERMISSIONS,
        GoogleSignIn.getLastSignedInAccount(activity),
        fitnessOptions
    )
} else {
    onSuccess.invoke()
}
  1. 我订阅了用于记录步数的应用程序。
Fitness.getRecordingClient(context, client!!)
.subscribe(DataType.TYPE_STEP_COUNT_DELTA)
.addOnSuccessListener {
    onSuccess.invoke()
}
.addOnFailureListener { e ->
    onFail.invoke(e)
}
  1. 我使用历史记录API查询了1周的时间,但始终返回空数据集。
// Setting a start and end date using a range of 1 week before this moment.  
val cal = Calendar.getInstance()  
val now = Date()  
cal.time = now  
val endTime = cal.timeInMillis  
cal.add(Calendar.WEEK_OF_YEAR, -1)  
val startTime = cal.timeInMillis  

val dateFormat = DateFormat.getDateInstance()
Log.i(TAG, "Range Start: " + dateFormat.format(startTime))
Log.i(TAG, "Range End: " + dateFormat.format(endTime))

val readRequest = DataReadRequest.Builder()
// The data request can specify multiple data types to return, effectively
// combining multiple data queries into one call.
// In this example, it's very unlikely that the request is for several hundred
// datapoints each consisting of a few steps and a timestamp.  The more likely
// scenario is wanting to see how many steps were walked per day, for 7 days.
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
// Analogous to a "Group By" in SQL, defines how data should be aggregated.
// bucketByTime allows for a time span, whereas bucketBySession would allow
// bucketing by "sessions", which would need to be defined in code.
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.enableServerQueries()
.build()
Fitness.getHistoryClient(context, client)
    .readData(readRequest)
    .addOnSuccessListener { dataReadResponse ->
        dumpDataSets(dataReadResponse.dataSets)
        onSuccess.invoke(dataReadResponse)
    }
    .addOnFailureListener { e ->
        onFail.invoke(e)
    }
    .addOnCompleteListener { task ->
        dumpDataSets(task.result!!.dataSets)
        onComplete.invoke(task)
    }

1 个答案:

答案 0 :(得分:0)

可以在存储桶中找到数据集:

dataReadResponse.buckets.forEach { bucket ->
   bucket.dataSet.forEach { dataSet ->
       dataSet.dataPoints.forEach { dataPoint ->
           // USE DataPoint
       }
   }
}