michal.luszczuk
michal.luszczuk

Reputation: 2903

WearableListenerService - onPeerConnected never called

I created simple Phone<->Wear App.

Wear app has one Activity with code:

public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks {

private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
        }
    });

    getGoogleApiClient(this).connect();
}


private GoogleApiClient getGoogleApiClient(Context context) {
    return new GoogleApiClient.Builder(context)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .build();
}

@Override
public void onConnected(Bundle bundle) {

    Toast.makeText(this, "onConnected", Toast.LENGTH_SHORT).show();
}

@Override
public void onConnectionSuspended(int i) {

    Toast.makeText(this, "onConnectionSuspended", Toast.LENGTH_SHORT).show();
}}

And Phone app has one WearableListenerService

public class ListenerOfConnection extends WearableListenerService  {

private static final String TAG = "DataLayerListenerServic";


protected GoogleApiClient googleApiClient;
@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(ListenerOfConnection.this, "onCreate", Toast.LENGTH_SHORT).show();
    googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();

    googleApiClient.connect();
}


@Override
public void onPeerConnected(Node peer) {
    super.onPeerConnected(peer);
    Toast.makeText(ListenerOfConnection.this, "onPeerConnected", Toast.LENGTH_SHORT).show();
    LOGD(TAG, "onPeerConnected: " + peer);
}

@Override
public void onPeerDisconnected(Node peer) {
    super.onPeerDisconnected(peer);
    Toast.makeText(ListenerOfConnection.this, "onPeerDisconnected", Toast.LENGTH_SHORT).show();
    LOGD(TAG, "onPeerDisconnected: " + peer);
}

public static void LOGD(final String tag, String message) {
    if (Log.isLoggable(tag, Log.DEBUG)) {
        Log.d(tag, message);
    }
}}

The problem is that onPeerConnected and onPeerDisconnected are never called, also onCreate is not called if I not send any message.

And if i send few messages onCreate is called but onPeerConnected and onPeerDisconnected never...

Before questions to me: 1. applicationId is the same 2. onConnected is called on wearable Activity

Upvotes: 1

Views: 1109

Answers (1)

Sterling
Sterling

Reputation: 6635

As far as I can tell, the onPeerConnected and onPeerDisconnected events are only called when your app is running and the bluetooth connection between the watch and phone is made or broken. In other words, you will very rarely see these events, and virtually never when you're debugging the two devices sitting on your desk.

If you're trying to do some work when your app first starts up and the API-level connection between the devices is initialized, you'll need to use the Wearable.NodeApi.getConnectedNodes method instead.

The documentation for this stuff is less than clear...

Upvotes: 3

Related Questions