Wearable MessageAPI onMessageReceived never hit, different devices id's
Asked Answered
H

2

7

Trying to send a message from an emulated mobile device to an emulated wear device. I'm able to pair the wear device through the Android Wear app and verify that onPeerConnected of the wear device is hit (onMessageReceived isn't).

Using two code versions to return node.getId() results in two different id's of the wear device.

Running this:

          new Thread(new Runnable() {
                @Override
                public void run() {

                    NodeApi.GetLocalNodeResult nodes = Wearable.NodeApi.getLocalNode(mGoogleApiClient).await();
                    Node node = nodes.getNode();
                    MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), "Hello Watch 1", null).await();

                    if (!result.getStatus().isSuccess()) {
                        Log.e(getPackageName(), "error");
                    } else {
                        Log.i(getPackageName(), "success!!!! sent to: " + node.getId());
                    }
                }
            }).start

returns: 08-09 10:24:33.106 17914-18007/com.wear.myapp I/com.wear.myapp: success!!!! sent to: 223faf0e

Running this:

            new Thread(new Runnable() {
                @Override
                public void run() {

                    NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();

                    for (Node node : nodes.getNodes()) {
                        MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), "Hello Watch 2", null).await();

                        if (!result.getStatus().isSuccess()) {
                            Log.e(getPackageName(), "error");
                        } else {
                            Log.i(getPackageName(), "success!!!! sent to: " + node.getId());
                        }
                    }
                }
            }).start();

Returns: 08-09 10:24:33.108 17914-18006/com.wear.myapp I/com.wear.streamer: success!!!! sent to: 3a000c12

Even stranger hardcoding in a fake Node ID for the wear device still returns a success message in the logs. Feel like I'm getting a false positive result.

WearableListenerService:

            @Override
            public void onMessageReceived(MessageEvent messageEvent) {
                Log.i(getPackageName(), "Message received");
            }


            @Override
            public void onPeerConnected(Node peer) {
                Log.i(getPackageName(), "Peer connected");
            }

I've read through every SO questions similar to this but haven't seen anyone mention different device id's. I've triple-checked that the applicationIds and dependencies are identical between mobile and wear.

UPDATE:

If unpair the Wear emulator and run Wearable.NodeApi.getLocalNode I still get a nodeId returned, while Wearable.NodeApi.getConnectedNodes does not, which leads me to believe getConnectedNodes is what I should be using.

Also, shutting down the Wear emulator still returns a connected node id for getLocalNode which leads me to believe it's returning something other then the watch.

Heed answered 9/8, 2017 at 14:44 Comment(0)
S
1

First of all, do not use deprecated methods like onPeerConnected. Always use CapabilityApi methods instead. This is improtaint!

I've got a question. Which kind of Android Wear APK (on handheld) file are you using? Don't you know that modern versions of Android Wear APK are not useful? They doesn't work properly.

If you are using old version of API you have to understand that modern APIs such as CapabilityApi doesn't work.

Nowadays, this is not possible to make a stable connection between emulated Wearable and emulated Handheld. One of them have to be a real device.

The only solution that I can suggest is to test your application of real wearable and handheld devices.

Always use up-to-date GoogleServices library to test, use real devices, use not deprecated APIs.

Before publishing decrease GoogleServices library version in order to make your app workable for not updated devices.

In my practice, emulators work very strange.

Scyros answered 14/8, 2017 at 14:20 Comment(2)
I wasn't aware onPeerConnected was deprecated. Usually Android Studio will display a message if an api is deprecated but it's not for onPeerConnected. Unfortunately, the devices I own do not have the APIs I need yet, so I'm forced to use the emulator. I'll look into the CapabilityAPI. ThanksHeed
If this helps anyone in the future, you can use the emulators to send messages from a phone to a wear device using the poorly documented CapabilityApiHeed
B
0

One possible issue could be the following: you are grabbing all connected nodes and then you grab the very first one to target for your message. That can very well end up being the cloud node (or another wearable device if you have multiple ones) and not your phone. The correct approach is to use the CapabilityApis to find the right node to send your message to. In your code, look at the node.toString() for the node that you selected to confirm that it is picking the cloud, to be sure that is the issue.

Check this related SO ticket:

Beverly answered 13/8, 2017 at 16:44 Comment(1)
Thanks for the response. I'm looking into the CapabilityApis per your post. I ran across someone suggesting to make sure the same certificate is being used for each device, though I didn't think emulators needed to worry about certificates. After more trial and error I'm thinking that getConnectedNodes is the correct method to use and it seems to be sending the message correctly but the wear app is not receiving it.Heed

© 2022 - 2024 — McMap. All rights reserved.