我目前正在开发使用Google Fit API的Android Wear 2.0应用程序。我发现我的谷歌api客户端总是无法连接。显示的错误是:
“无效帐户错误:连接时指定了无效帐户。请提供有效帐户。”
似乎是通过添加范围addScope()方法导致这种情况发生。没有这一行,谷歌api客户端将连接,但显然不能在没有范围的情况下正确使用。
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
.build();
googleApiClient.connect();
我在Google开发者控制台和OAuth 2.0客户端ID上启用了Google Fit Api。我已经启用了设备本身的权限。我无法确定导致此问题的原因或如何对其进行排序。我已经将开发人员站点中的示例示例代码跟踪到了T。
app manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.androidfitnessapp" >
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.javapapers.android.maps.path.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BODY_SENSORS" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-feature
android:name="android.hardware.type.watch" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.Material" >
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
<activity
...
构建脚本
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.project.androidfitnessapp"
minSdkVersion 25
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.support:wearable:2.0.0'
provided 'com.google.android.wearable:wearable:2.0.0'
compile 'com.google.android.gms:play-services-wearable:10.2.0'
compile 'com.google.android.gms:play-services-fitness:10.2.0'
compile 'com.google.android.gms:play-services-maps:10.2.0'
compile 'com.google.android.gms:play-services:10.2.0'
compile 'com.google.android.gms:play-services-auth:10.2.0'
compile 'com.jjoe64:graphview:4.2.1'
}