Android Wear - Wearable Message Api

When an Android application has both Mobile/Phone and Android Wear component, need for establishing communication and data exchange between those components is obvious.
Android SDK provides three methods for data exchange and data synchronization between phone and Android Wear applications:
  • Messages
  • DataItems
  • Assets
The simplest method to achieve communication between phone and wearable applications is by Messages, using Google Play Services’s Wearable MessageApi.
Mobile device must have Google’s “Android Wear” application installed to make other applications able to use Wearable MessageApi.
Messages have a characteristic that every Message has a defined path as an identifier. This makes Messages a good choise for a mechanism of triggering activities inside one application by sending a message from another application.
The mechanism of sending and receiving Messages consists of:
  • A Message that will be sent from one application (Phone application or Wearable application) to another application.
  • A Message listener that receives messages. There are two ways for implementing Message listeners:
    • MessageApi.MessageListener - Suitable for listening for messages in short-life application components (Activities)
    • WearableListenerService - Abstract class used for creating a service that will listen for Messages in background. Besides Messages, WearableListenerService also listens for other changes. It provides following methods that are being called when appropriate changes are registered: onDataChanged(), onMessageReceived(), onPeerConnected(), onPeerDisconnected(). In the context of receiving Messages, onMessageReceived() is relevant.

Sending Messages

Here is an example code of creating and sending a Wearable Message:

Send Message
1
 Wearable.MessageApi.sendMessage(googleApiClient, connectedNodeId, messagePath, null);

Listening for Messages

Listening using MessageListener

An example of creating and registering a MessageListener:

Creating and registering MessageListener
1
2
3
4
5
6
7
8
9
10
11
 // Create MessageListener
  messageListener = new MessageApi.MessageListener() {
      @Override
      public void onMessageReceived(MessageEvent messageEvent) {
          // To execute when a message is received
          ...
      }
  };

  // Register MessageListener
  Wearable.MessageApi.addListener(googleApiClient, messageListener);

Unregistering a MessageListener:

Unregistering MessageListener
1
 Wearable.MessageApi.removeListener(googleApiClient, messageListener);

Listening using WearableListenerService

Every application can have only one WearableListenerService service.
En example of extending WearableListenerService service:

WearableListenerService
1
2
3
4
5
6
7
8
9
public class MessageService extends WearableListenerService {
  @Override
  public void onMessageReceived(MessageEvent messageEvent) {
      super.onMessageReceived(messageEvent);

      // To execute when a message is received
      ...
  }
}

The service has to be included in AndroidManifest.xml, with an appropriate intent-filter:

AndroidManifest.xml
1
2
3
<service android:name=".MessageService">
  <intent-filter>com.google.android.gms.wearable.BIND_LISTENER</intent-filter>
</service>

I have created and uploaded on GitHub a working example of using Wearable MessageApi. The example of creating and sending Wearable Messages from Mobile application to Android Wear application (and vice versa), and how to listen for Messages using MessageListener.

Comments

Popular Posts