This can be done using mentioned MessageApi: http://developer.android.com/training/wearables/data-layer/messages.html
You need to initialize and connect with GoogleApiClient. Once you clicks the button you have to get list of nodes and send a message to them. The last step is to read this message fron phone part of app, this can be done by registering proper WearableListenerService. Please see the sample code below.
Wearable part of app:
Please define such Activity on your Wearable part of app:
public class WearableButtonActivity extends Activity {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wearable_button_activity);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
public void onButtonClicked(View target) {
if (mGoogleApiClient == null)
return;
final PendingResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient);
nodes.setResultCallback(new ResultCallback() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult result) {
final List nodes = result.getNodes();
if (nodes != null) {
for (int i=0; i pendingSendMessageResult = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), "/MESSAGE", null);
// pendingSendMessageResult.setResultCallback(new ResultCallback() {
// public void onResult(SendMessageResult sendMessageResult) {
// if (sendMessageResult.getStatus().getStatusCode()==WearableStatusCodes.SUCCESS) {
// // do something is successed
// }
// }
// });
}
}
}
});
}
}
and attack onButtonClick method to your button in your res/layout/wearable_button_activity.xml file:
OR just set OnClickListener from code if you like that way:
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onButtonClicked(v);
}
});
Phone part of app:
Then declare DataLayerListenerService on yout phone's manifest:
DataLayerListenerService class:
public class DataLayerListenerService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
if("/MESSAGE".equals(messageEvent.getPath())) {
// launch some Activity or do anything you like
}
}
}
IMPORTANT: Both parts of your app need to have the same package name to contact with each other.