【发布时间】:2020-09-05 13:36:51
【问题描述】:
我正在尝试为我的论文项目构建一个 bpm 计数器。我是 Google Fitness Api 的新手,所以我首先从 git 克隆了 google 的计步器示例。 (如果感兴趣,请链接 [此处][1])。我设置了所有内容,授权了我的帐户,将其连接到云项目,使用 keytool 获取 SHA-1 证书,但我仍然无法获得包含我要求的数据的响应,它是 bpm 或步骤。我根据需要在 xml 文件中设置了每个权限,但仍然面临问题。下面,我给你我的代码的一些部分,如果你还需要什么 lmk。提前感谢您的任何回复。
XML 清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
登录功能:
private fun fitSignIn(requestCode: FitActionRequestCode) {
if (oAuthPermissionsApproved()) {
performActionForRequestCode(requestCode)
} else {
requestCode.let {
GoogleSignIn.requestPermissions(
this,
requestCode.ordinal,
getGoogleAccount(), fitnessOptions)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (resultCode) {
RESULT_OK -> {
val postSignInAction = FitActionRequestCode.values()[requestCode]
postSignInAction.let {
performActionForRequestCode(postSignInAction)
}
}
else -> oAuthErrorMsg(requestCode, resultCode)
}
}
选择我的 GAccount 时的代码请求:
private fun performActionForRequestCode(requestCode: FitActionRequestCode) = when (requestCode) {
FitActionRequestCode.READ_DATA -> readData()
FitActionRequestCode.SUBSCRIBE -> subscribe()
}
private fun oAuthErrorMsg(requestCode: Int, resultCode: Int) {
val message = """
There was an error signing into Fit. Check the troubleshooting section of the README
for potential issues.
Request code was: $requestCode
Result code was: $resultCode
""".trimIndent()
Log.e(TAG, message)
}
private fun oAuthPermissionsApproved() = GoogleSignIn.hasPermissions(getGoogleAccount(), fitnessOptions)
private fun getGoogleAccount() = GoogleSignIn.getAccountForExtension(this, fitnessOptions)
在请求数据时,我总是以结果 0 结束。
[![模拟器][2]][2]
这是我帐户的云设置。 [![GCloud][3]][3]
我已经从 Google Fit 的常见问题解答中检查了一切是否正确,并且我为我的项目启用了 Fitness Api,但我仍然没有得到任何步骤。如果有人能指出我正确的方向,我将不胜感激。提前致谢 [1]:https://github.com/android/fit-samples/tree/master/StepCounterKotlin [2]:https://i.stack.imgur.com/zvTni.png [3]:https://i.stack.imgur.com/UbtPb.png
【问题讨论】:
标签: java android kotlin google-api google-oauth