GoogleApiClient onConnected从未调用过可穿戴设备

时间:2014-07-10 16:25:08

标签: android google-api-client wear-os

我有一个可穿戴设备,我正在尝试连接到GoogleApiClient但是从不调用回调(onConnected,onConnectionSuspended或onConnectionFailed)。其他所有工作正常,DataLayerListenerService能够从掌上电脑接收消息,并在连接时调用onPeerConnected。我试过了仿真器和三星Gear Live设备。这是我在Activity中的代码,我正在尝试连接到GoogleApiClient。

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 个答案:

答案 0 :(得分:142)

哇,哇,答案简直令人尴尬。我错过了onStart()您需要致电mGoogleApiClient.connect()的部分。

答案 1 :(得分:3)

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

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

答案 2 :(得分:0)

此外,我注意到如果您在用户未设置Google Play时尝试使用GoogleApiClient,则可能会发生冲突连接。 (我只是重置我的设备,这就是发生的事情)。因此,使用

测试来自GoogleApiClient的连接是一种很好的做法
mCredentialsApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)

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