Simon
Simon

Reputation: 13

Two WearableListenerService listening for messages from single wearable app

I'm having an issue where I want to have two diffrent WearableListenerServices listening for messages from a wearable app. Both extending WearableListenerService and configured thusly:

<service android:name="no.xx.xx.wear.WearService">
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
    </intent-filter>
</service>

Messages are sent from the wearable app using Wearable.MessageApi.sendMessage(...) and listened for using WearableListenerService's onMessageReceived(MessageEvent messageEvent) method.

But it seems messages are then no longer recieved by the when I go from one to two listener services?

So my question is this: Is there a limitation as to how many services can listen for messages over gms in a single companion-wearable app combination?

Upvotes: 1

Views: 755

Answers (4)

Amir Alagic
Amir Alagic

Reputation: 1790

Just one listener is enough. With help of different "paths" you can check in your listener service who sent you the message and then decide what to do with it.

Upvotes: 0

Michał Tajchert
Michał Tajchert

Reputation: 10383

It is not possible by extending WearableListenerService but I have created a small library that uses one WearableListenerService and a BroadcastReceiver for passing those events without lag to any number of Listeners that you wish.

https://github.com/tajchert/ServiceWear

Upvotes: 0

Benjamin
Benjamin

Reputation: 76

You can listen for wearable messages in any regular service or activity without limitation. I believe that you can only have one WearableListenerService at a time though. So what you should probably do is handle any messages that need to be handled in the background with WearableListenerService. And when you are running an activity or plain old service which needs to listen for wearable messages, implement MessageApi.MessageListener. Here is an example

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

    private GoogleApiClient googleClient; // A google client for Wear interaction

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Build a new GoogleApiClient that includes the Wearable API
        googleClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        Wearable.MessageApi.addListener(googleClient, this);
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        //Same onMessageReceived as in WearableListenerService
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) { }
    @Override
    public void onConnectionSuspended(int i) { }
}

Note: You can use this same basic principle with a service too.

Upvotes: 0

gruszczy
gruszczy

Reputation: 42208

You can only have one WearableListenerService. Android Wear doesn't support having more than one.

Upvotes: 1

Related Questions