Search code examples
wear-osremote-controlandroid-wear-data-api

Send message to phone from Android Wear Notification


I am new to Android Wear programming and I'm learning as I develop this application.

It's simply going to be a media controller - so that the user can press play / pause on the watch and the phone that plays video receives and handles it.

What I have done so far:

I have set up the Android Wear app so that I can receive a notification from the mobile app.

Intent notificationIntent = new Intent(this, NotificationActivity.class);
                        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .extend(new NotificationCompat.WearableExtender()
                            .setContentIcon(R.drawable.ic_launcher)                                          .setCustomSizePreset(NotificationCompat.WearableExtender.SIZE_FULL_SCREEN)
                            .setDisplayIntent(pendingIntent)
                            .addAction(new NotificationCompat.Action.Builder(R.drawable.ic_launcher, "Hej", pendingIntent).build())
                                    .setContentAction(0));

((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
.notify(NOTIFICATION_ID, notificationBuilder.build());

The code above is what I use to show my remote card on the watch. However, what I wanted to achieve was that when this button was pressed, it should send a message BACK to the phone.

Now my problems are:

  • I can only set a new intent as an action. Is this really the right way to send one simple message (like a string or int) back to the phone? Like one activity that only sends a message and then destorys? And if so, how do I get the API into that activity so i can send the message to the phone?
  • How do I send this message and receive it on the phone? I have found nothing on this in the guides... I have seen indications that I can start an acitivty on the phone when the Wear device sends a message, but that's absolutely not what I want to do since I have a running activity there...

thanks!


Solution

  • So this is how I solved my problem:

    On the Android Wear module, I used a WearableListenerService. From my mobile module, I simply use the Wearable DataAPI to send data events to it. The function onDataChanged in WearableListenerService recieves them, so I just subclassed it and implemented my own.

    In this method, I use the information provided to create and show a new Notification. From the notfication activity, I request an instance of my ListenerService class, and use it to send a message BACK to the mobile.

    The activity on the mobile then implements MessageAPI.MessageListener and the communication works.

    A few of the lessons I learned on the way:

    • PutDataRequest request = putDataMapRequest.asPutDataRequest(); Wearable.DataApi.putDataItem(wearApiClient, request);

      The request needs to change EVERY time. So even if I'm only interested in the first post, it didn't happen because I didn't change the data. So when I used the putString, I had to add a counter do the String and increment it. Like:

      putDataMapRequest.getDataMap().putString("path", "hello" + (count++));

    • The listener service is to be used when an activity is not already running on the device. So if you want to start a mobile activity from wear - make a service class in the mobile module. If you want to display a notification on the wear device when something happens on the phone - use a lsitener service in the wear module. If you just want communication between two running apps - use message or Data API.