Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

android - GoogleApiClient onConnected never called on Wearable device

I have a wearable device that I'm trying to connect to the GoogleApiClient but the callbacks are never called (onConnected, onConnectionSuspended or onConnectionFailed). Everything else is working fine, the DataLayerListenerService is able to receive messages from the handheld and the onPeerConnected is being called when it connects. I've tried in on both the emulator and a Samsung Gear Live device. This is the code that I have in the Activity where I'm trying to connect to the 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");
    }
...
}

Not sure if it'll help but this is my Manifest for the Wearable app

    <?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>

Any ideas?

EDIT: Added the following to the wearable manifest, still not working though

<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />
question from:https://stackoverflow.com/questions/24681722/googleapiclient-onconnected-never-called-on-wearable-device

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Oh wow, the answer is embarrassingly simple. I missed the part where in onStart() you need to call mGoogleApiClient.connect().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...