GoogleApiClient onConnected从未呼吁可穿戴设备

我有一个可穿戴设备,我尝试连接到GoogleApiClient,但从不会调用callback(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" /> 

哦,哇,答案是尴尬简单。 我错过了onStart()需要调用mGoogleApiClient.connect()

您可以在onStart和onStop生命周期方法中手动调用连接和断开连接,也可以使用enableAutoManagefunction。

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

此外,我注意到,如果您尝试在用户未设置Google Play时尝试使用GoogleApiClient,则可能会出现连接冲突。 (我只是重置我的设备,这就是发生了什么)。 因此,使用GoogleApiClienttesting连接是一个很好的做法

 mCredentialsApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) 

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