While Android Wear provides a lot of great features, such as notifications, out of the box, its real potential lies in the ability to create native apps that communicate with a paired smartphone. Luckily, Android Wear comes with a few new APIs to help make this communication a lot smoother for developers: the Message API, Node API and DataLayer API. Each of these has their own unique purpose; the Message API is designed for “fire and forget it” types of messages, the DataLayer API supports syncing data between a smartphone and a wearable, and the Node API handles events related to local and connected device nodes.
For this tutorial I’ll cover using the Message API to send data from a smartphone to an Android Wear device. We’ll start a watch activity by using a WearableListenerService and display text in a ListView through the MessageApi.MessageListener interface, though all three APIs are used in a similar way. All sample code for this tutorial can be found on GitHub.
To start, create a project in Android Studio that has both a mobile and wear module:
Once your project and modules are created, you can start building the mobile side of your application. The sample project contains simple ListView, EditText and Button views that allow the user to enter text and, when the Button is pressed, display it in the ListView while also sending that text to the wearable.
To start, open AndroidManifest.xml in the mobile module and add a meta-data tag for Google Play Services
Next, we can focus on the mobile MainActivity class. The first thing we need to do is connect the GoogleApiClient and implement the GoogleApiClient.ConnectionCallbacksinterface:
private void initGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.build();
mApiClient.connect();
}
When the GoogleApiClient has finished connecting, onConnected will be called. When it is, we’ll send a message through the Message API to a WearableListenerService (implemented later in this tutorial) running on the Wear in order to start our watch activity.
@Override
public void onConnected(Bundle bundle) {
sendMessage( START_ACTIVITY, "" );
}
…where START_ACTIVITY is a string value that must begin with /, denoting a message path.
private static final String START_ACTIVITY = "/start_activity";
Aside from our initial message, we also want a message to be sent when our ‘send’ button is pressed. This is done through a simple OnClickListener that uses a different path value of /messageand sends the text present in the EditText at the bottom of the screen.
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( WEAR_MESSAGE_PATH, text );
}
}
});
The sendMessage method is where we really start to see how we interact with the new Android Wear APIs. In order to send a message to the Wear, we must first use the Node API to get a list of nodes connected to the device. Once we have this list, we send a message to each node using MessageAPI with references to the GoogleApiClient, node ID, the path used to determine the type of message being sent, and the message payload as a byte array. A MessageApi.SendMessageResult is returned that can be used to determine if a message was successfully sent to the current node. Once the message is sent, we clear the EditText view to allow the user to enter more text.
private void sendMessage( final String path, final String text ) {
Finally in onDestroy, we need to disconnect from GoogleApiClient.
@Override
protected void onDestroy() {
super.onDestroy();
mApiClient.disconnect();
}
Now that the mobile MainActivity is ready, it’s time to move into the wear module. First we’ll want to create a class that extends from WearableListenerService, which is a service that implements the three Wear communication APIs. For our purposes we only need to override the onMessageReceived method. In this method we check the path that was sent over the MessageApi to determine the type of message, and then, if appropriate, fire off an intent to our wear MainActivity to bring our application to the forefront.
private static final String START_ACTIVITY = "/start_activity";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
As mentioned earlier, there are two ways to receive a message on Android Wear. The next way is implementing MessageApi.MessageListener in our Wear MainActivity. To start, we need to connect GoogleApiClient in the same way as our mobile application. In the onConnectedcallback, we add our wearable listener to the api client
@Override
public void onConnected( Bundle bundle ) {
Wearable.MessageApi.addListener( mApiClient, this );
}
Next, we simply override the onMessageReceived method from the MessageListenerinterface, check the path value to determine if we want to take action, and then add the payload text to our adapter to display the text in our wear ListView
@Override
public void onMessageReceived( final MessageEvent messageEvent ) {
mAdapter.add( new String( messageEvent.getData() ) );
mAdapter.notifyDataSetChanged();
}
}
});
}
This will display the text from our mobile app on the wearable, as seen below (the screen shot was taken from a Moto 360, hence the cut off portion at the bottom).
And with that we now have a working communication channel from a smartphone to a paired Android Wear device. I hope this tutorial helps you build your own applications. Good luck!
DataMapItem 또는 message, assets등등의 방법을 이용할수 있다. 쌍방의 통신이 필요한경우는 DataItem , DataMapItem을 이용하면되고 일방의 통신의 경우는 Message를 이용할수 있다. (RPC 처럼 사용 가능)
Message를 사용하는 경우(DataItem,DataMapItem의 경우는 사용해보지 않아서 같은지 아직 모름) 일단 네트워크의 node를 확인해야한다. 그리고 가장 가까운 기기기 node(mobile인경우가 대부분)가 접속되었는지, 또 message작업이 가능한지 capability를 확인해야 한다. 그리고 나서 data는 byte[]의 형태로 MessageApi.sendMessage를 통해 전달된다. 그리고 message를 mobile에서 받는 경우 두가지 경우를 생각할수 있다.
사용자가 app을 사용하고 있는 경우
사용자가 app을 사용하지 않고 있는 경우
첫번째 경우에만 통신이 필요한경우는 MessageApi.MessageListener를 inplements 한 activity가 받아들이면된다. 후자의 경우는 service를 이용해서 받는다.
DataMapItem 또는 message, assets등등의 방법을 이용할수 있다. 쌍방의 통신이 필요한경우는 DataItem , DataMapItem을 이용하면되고 일방의 통신의 경우는 Message를 이용할수 있다. (RPC 처럼 사용 가능)
Message를 사용하는 경우(DataItem,DataMapItem의 경우는 사용해보지 않아서 같은지 아직 모름) 일단 네트워크의 node를 확인해야한다. 그리고 가장 가까운 기기기 node(mobile인경우가 대부분)가 접속되었는지, 또 message작업이 가능한지 capability를 확인해야 한다. 그리고 나서 data는 byte[]의 형태로 MessageApi.sendMessage를 통해 전달된다. 그리고 message를 mobile에서 받는 경우 두가지 경우를 생각할수 있다.
사용자가 app을 사용하고 있는 경우
사용자가 app을 사용하지 않고 있는 경우
첫번째 경우에만 통신이 필요한경우는 MessageApi.MessageListener를 inplements 한 activity가 받아들이면된다. 후자의 경우는 service를 이용해서 받는다.