Paul Ruiz
Paul Ruiz

Reputation: 2436

MessageApi.MessageListener in Wear Activity Not Triggering

So I've looked around on StackOverflow and haven't found an answer, so I'm wondering if there's just something I'm missing. I have an activity in the 'wear' portion of my app with the same package and application name as my mobile part, but it isn't being triggered when a message is sent:

public class MainActivity extends Activity implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks {

private GoogleApiClient mApiClient;
private ArrayAdapter<String> mAdapter;

private ListView mListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mListView = (ListView) findViewById(R.id.list);

    mAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1 );
    mListView.setAdapter( mAdapter );

    initGoogleApiClient();
}

private void initGoogleApiClient() {
    mApiClient = new GoogleApiClient.Builder( this )
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .build();

    if( mApiClient != null && !( mApiClient.isConnected() || mApiClient.isConnecting() ) )
        mApiClient.connect();
}

@Override
protected void onResume() {
    super.onResume();
    if( mApiClient != null && !( mApiClient.isConnected() || mApiClient.isConnecting() ) )
        mApiClient.connect();
}

@Override
public void onMessageReceived(MessageEvent messageEvent) {
    mAdapter.add( new String( messageEvent.getData() ) );
    mAdapter.notifyDataSetChanged();

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

@Override
public void onConnected(Bundle bundle) {
    Toast.makeText( this, "Wear onConnected", Toast.LENGTH_SHORT ).show();
    Wearable.MessageApi.addListener( mApiClient, this );
}

@Override
protected void onStop() {
    super.onStop();
    if( mApiClient != null && mApiClient.isConnected() ) {
        mApiClient.disconnect();
    }
}

@Override
public void onConnectionSuspended(int i) {

}

From my mobile activity, I send a message that I would expect would be picked up from the Wear activity. If I use a WearListenerService, then onMessageReceived is triggered fine, I just can't get it to hit in an activity.

public class MainActivity extends Activity {

private GoogleApiClient mApiClient;

private ArrayAdapter<String> mAdapter;

private ListView mListView;
private EditText mEditText;
private Button mSendButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    init();
    initGoogleApiClient();
}

private void initGoogleApiClient() {
    mApiClient = new GoogleApiClient.Builder( this )
            .addApi( Wearable.API )
            .build();

    mApiClient.connect();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mApiClient.disconnect();
}

private void init() {
    mListView = (ListView) findViewById( R.id.list_view );
    mEditText = (EditText) findViewById( R.id.input );
    mSendButton = (Button) findViewById( R.id.btn_send );

    mAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1 );
    mListView.setAdapter( mAdapter );

    mSendButton.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String text = mEditText.getText().toString();
            if( !TextUtils.isEmpty( text ) ) {
                mAdapter.add( text );
                mAdapter.notifyDataSetChanged();

                sendMessage( text );
            }
        }
    });
}

private void sendMessage( final String text ) {
    new Thread( new Runnable() {
        @Override
        public void run() {
            NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( mApiClient ).await();
            for(Node node : nodes.getNodes()) {
                Log.e("MainActivity", "node id: " + node.getId() );
                MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
                        mApiClient, node.getId(), text, null).await();
            }

            runOnUiThread( new Runnable() {
                @Override
                public void run() {
                    mEditText.setText( "" );
                }
            });
        }
    }).start();
}

Anyone have any suggestions on what I may be missing? I can fall back to using a service, but I want to try and get this working with an activity if I can.

Thanks!

Upvotes: 2

Views: 1186

Answers (3)

Csaba8472
Csaba8472

Reputation: 31

Check signingConfigs in build.gradle, for me the problem was that I used different signing keys.

Upvotes: 0

Paul Ruiz
Paul Ruiz

Reputation: 2436

So it turns out onMessageReceived is running on its own thread, not the UI thread, so you have to wrap the toast message and any of your own UI code in runOnUiThread.

Upvotes: 1

barkside
barkside

Reputation: 3971

Try adding this in your Wear Activity:

@Override
protected void onDestroy() {
    Log.d(TAG, "onDestroy()");

    if(mApiClient != null) 
        mApiClient.unregisterConnectionCallbacks(this);
    super.onDestroy();
}

And adding this to the start of your onStop():

    if (mApiClient != null) {
        Wearable.MessageApi.removeListener(mApiClient, this);

Then reboot your watch, and re-test. Otherwise a previous instance of your activity will have registered the listeners, possibly confusing matters.

Upvotes: 0

Related Questions