WearableListenerService - onPeerConnected从未调用

时间:2015-05-11 20:00:29

标签: android wear-os android-wear-data-api

我创建了简单的手机< - > Wear App。

Wear app有一个Activity代码:

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();
}}

手机应用程序有一个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);
    }
}}

问题是onPeerConnectedonPeerDisconnected 从未被调用,如果我不发送任何消息,也不会调用onCreate

如果我发送的消息很少onCreate被调用,但onPeerConnectedonPeerDisconnected 从不 ......

在向我提出问题之前: 1. applicationId是一样的 2. onConnected在可穿戴活动上被调用

1 个答案:

答案 0 :(得分:3)

As far as I can tell, the @model.save and onPeerConnected 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 onPeerDisconnected method instead.

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

相关问题