Wearable设备上GoogleApiClient的onConnected从未被调用

40

我有一个可穿戴设备,想要连接到GoogleApiClient,但回调函数从未被调用(onConnected、onConnectionSuspended或onConnectionFailed)。其他所有功能都正常工作,DataLayerListenerService能够接收来自手持设备的信息,当连接时也会调用onPeerConnected。我已经在模拟器和三星Gear Live设备上尝试过。以下是我在尝试连接到GoogleApiClient的Activity中的代码。

public class WearReaderActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    public static final String CONTENT_EXTRA = "contentExtra";
    private String LOG_TAG = WearReaderActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reader);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("Connected?", String.valueOf(mGoogleApiClient.isConnected()));
        //new Thread(new GetContent()).start();
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.d("Connected", "Connected");
        new Thread(new GetContent()).start();
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d("Connection suspened", "Connection suspended");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d("Connection suspened", "Connection suspended");
    }
...
}

不确定它是否有帮助,但这是我可穿戴应用程序的清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="my.packagename">
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

    <activity
        android:name=".WearReaderActivity"
        android:label="Reading" >
        <intent-filter>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

<meta-data android:name="com.google.android.gms.version"
       android:value="@integer/google_play_services_version" />

    <service
        android:name=".DataLayerListenerService" >
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </service>

</application>

有任何想法吗?

编辑:已将以下内容添加到可穿戴设备清单中,但仍无法正常工作。

<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />
3个回答

142

哦,哇,答案非常简单。我错过了在 onStart() 中需要调用mGoogleApiClient.connect()的部分。


97
当文档“教程”忽略了这一点时,这并不尴尬。 - iforce2d
1
正是我所想的,iforce2d。 - bmeulmeester
13
在 https://developer.android.com/training/location/retrieve-current.html 的教程中,有一个令人震惊的遗漏! - Craig McMahon
2
我已经提出了修复此处文档的请求。 - Wayne Piekarski
3
文档中的课程忽略了这一点,正如@iforce2d所指出的那样。请注意在onStop()中也要调用mGoogleApiClient.disconnect()。 - Sébastien BATEZAT
显示剩余2条评论

3

在onStart和onStop生命周期方法中,您可以手动调用connect和disconnect,或者您可以使用enableAutoManage功能。

mCredentialsApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .enableAutoManage(this, this)
            .addApi(Auth.CREDENTIALS_API)
            .build();

0
此外,我注意到如果在用户未设置Google Play时尝试使用GoogleApiClient可能会出现连接冲突(我刚刚重置了我的设备,就发生了这种情况)。因此,最好的做法是使用GoogleApiClient测试连接。
mCredentialsApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)

或者在执行任何任务之前,先检查mCredentialsApiClient.isConnected()。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接