2

I'm trying to send a message from a wearable to a handheld and then a response from the handheld to the wearable (both modules use the same code and logic).
Gradle:

compile 'com.google.android.support:wearable:2.0.0-alpha2'
compile 'com.google.android.gms:play-services-wearable:9.6.1'

...

classpath 'com.android.tools.build:gradle:2.2.1'
classpath 'com.google.gms:google-services:3.0.0'

AndroidManifest:

    <application>
    ...
        <service android:name=".ReceiverService"
                 android:enabled="true"
                 android:exported="true">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED"/>
                <data android:scheme="wear" android:host="*"/>
            </intent-filter>
        </service>    
    </application>

ReceiverService:

public class ReceiverService extends WearableListenerService {
    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        super.onMessageReceived(messageEvent);
        Log.v(TAG, "onMessageReceived: " + messageEvent);
    }
}

MessageSender:

public class MessageSender implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    private static final String TAG = "_wear_message_sender";
    private final GoogleApiClient mGoogleApiClient;

    private static class InstanceHolder {
        private static final MessageSender sInstance = new MessageSender();
    }

    private MessageSender() {
        mGoogleApiClient = new GoogleApiClient.Builder(WearApp.getInstance().getContext())
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    public static MessageSender getInstance() {
        return InstanceHolder.sInstance;
    }

    public void sendMessage(final String path, final String message) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (!mGoogleApiClient.isConnected()) {
                    mGoogleApiClient.blockingConnect(Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS);
                }    
                List<Node> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await().getNodes();
                Log.d(TAG, "Nodes count " + nodes.size());

                for (Node node : nodes) {
                    Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                        @Override
                        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                            Log.d(TAG, "sendMessage(" + path + "): " + sendMessageResult.getStatus().isSuccess());
                        }
                    });
                }
                mGoogleApiClient.disconnect();
            }
        }).start();
    }
    ...
}

And I got this output:

D/_wear_message_sender: onConnected: null
D/_wear_message_sender: Nodes count 1
D/_wear_message_sender: sendMessage(/test): true

I've used a Moto 360 and Emulator, the message is sent but the handheld WearableListenerService onMessageReceived() is not called. Same goes the other way, I've sent a message from a mobile phone and the wearable onMessageReceived is not called.
What am I missing here?

GuilhE
  • 11,591
  • 16
  • 75
  • 116
  • @Blackbelt it's now deprecated: http://tools.android.com/tech-docs/bind-listener – GuilhE Oct 11 '16 at 15:07
  • 1
    Check if the [`applicationID`](https://plus.google.com/+PetrN%C3%A1levka/posts/deCyoF6ALvV) were the same in the build.gradle for mobile and wear app because otherwise Android wouldn't know to which the messages should be sent. You can check this [SO thread](http://stackoverflow.com/questions/33817622/wearablelistenerservice-onmessagereceived-is-not-called-on-device). Also make sure that the `buildTypes` part and the `signingConfigs` part are the same in both apps. – abielita Oct 12 '16 at 07:20
  • @abielita thanks, I had different packages name indeed. Now it's working but only 50% because I can send messages from the handheld to the watch but not the other way. Thanks for your help! – GuilhE Oct 12 '16 at 10:36
  • 1
    @abielita, found out the problem, I've commented a few lines in my app manifest. Now it's working 100%. Post your comment as answer and I'll accept it ;) – GuilhE Oct 12 '16 at 10:50

1 Answers1

6

Check if the applicationID were the same in the build.gradle for mobile and wear app because otherwise Android wouldn't know to which the messages should be sent. You can check this SO thread. Also make sure that the buildTypes part and the signingConfigs part are the same in both apps.

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59