WearableListenerService onMessageReceived is not called on the device

I am trying to send a simple message from my android app to my phone app using Wearable.MessageApi.

This is my answer onConnectedfrom GoogleApiClient on the Wear device.

final PendingResult<Status> status = Wearable.DataApi.addListener(googleApiClient, this);
status.setResultCallback(new ResultCallback<Status>() {
    @Override
    public void onResult(Status status) {
        if (!status.isSuccess()) {
            return;
        }

        NodeApi.GetConnectedNodesResult nodes =
                Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
        for (Node node : nodes.getNodes()) {
            System.out.println("Sending message: " + node.getDisplayName());
            final MessageApi.SendMessageResult result =
                    Wearable.MessageApi.sendMessage(googleApiClient, node.getId(),
                            "request", "12345".getBytes())
                            .await();
            System.out.println("sent: " + result.getStatus().isSuccess());
        }
    }
});

And it displays the following at startup

Sending message: Nexus 6P
sent: true

And this is my registered service in my application:

public class MyWearableListenerService extends WearableListenerService {

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Toast.makeText(this, "Received message", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onPeerConnected(Node peer) {
        Toast.makeText(this, "Peer connected", Toast.LENGTH_LONG).show();
    }
}

I correctly checked that the toast Peer connectedappears when the emulator is connected to my device. I did the right port forwarding to debug the wear emulator. I checked that my application and package names are consistent in the application and wear the application. However, I never get a callback onMessageReceivedon my device.

Any suggestions are welcome! I debugged this all day :(

+4
1

, . . , , , , com.example.android com.example.android.dev..

, , , :\

+6

Source: https://habr.com/ru/post/1616660/


All Articles