简体   繁体   English

WearableListenerService onMessageReceived()未调用

[英]WearableListenerService onMessageReceived() not called

On a phone device I have a WearableListenerService listening for input from a Android Wear device. 在电话设备上,我有一个WearableListenerService来侦听来自Android Wear设备的输入。

I am using both DataItems and Messages. 我正在同时使用DataItems和Messages。 DataItems sync between the two devices just fine, however, I am having problems with receiving messages on the phone. 数据项在两个设备之间同步很好,但是,我在电话上接收消息时遇到问题。

I have tried the following: 我尝试了以下方法:

  • Confirmed Wear has sent the message 已确认磨损已发送消息
  • Checked if package names are the same 检查包名称是否相同
  • Checked if signatures are the same (both are Android Debug) 检查签名是否相同(均为Android Debug)

Still, onMessageReceived is not called in DataLayerListenerService. 不过,在DataLayerListenerService中不会调用onMessageReceived。 Originally, I was using an activity and extending MessageListener which did not work either besides one point where it briefly worked. 最初,我使用的是活动并扩展了MessageListener,除了短暂地起作用的一点之外,它也没有作用。

Android Wear code Android Wear代码

public static void sendMessageToDevice(final String commandPath, final byte[] additionalData)
{
    // Separate thread from UI thread
    new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            String nodeId = null;

            // Find first connected device id

            NodeApi.GetConnectedNodesResult result =
                    Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
            List<Node> nodes = result.getNodes();

            if (nodes.size() > 0)
            {
                nodeId = nodes.get(0).getId();
            }
            if (nodeId != null)
            {
                Wearable.MessageApi.sendMessage(mGoogleApiClient, nodeId,
                        commandPath, additionalData).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>()
                {
                    @Override
                    public void onResult(MessageApi.SendMessageResult sendMessageResult)
                    {
                        if (!sendMessageResult.getStatus().isSuccess())
                        {
                            System.err.println("Message " + commandPath + " could not be sent.");
                        }
                    }
                });

                System.out.println("Command path is: " + commandPath);
            }
        }
    }).start();
}

Android device code Android设备代码

public class DataLayerListenerService extends WearableListenerService
{

@Override
public void onMessageReceived(MessageEvent messageEvent)
{
    System.out.println("Received command");
    String command = messageEvent.getPath();
    System.out.println("Received command is: " + command);
    if (command.contains("/mobile/input/"))
    {
        System.out.println(command);
    }
}

@Override
public void onDataChanged(DataEventBuffer dataEvents)
{
    for (DataEvent event : dataEvents)
    {
        if (event.getType() == DataEvent.TYPE_CHANGED)
        {
            DataItem item = event.getDataItem();
            DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
            String itemPath = item.getUri().getPath();
            if (itemPath.equals("/mobile/input/level"))
            {
                int level = dataMap.getInt("level");
                MainActivity.readLevel(level);
            }
        }
    }
}
}

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. 正确的方法是使用CapabilityApis查找将消息发送到的正确节点。 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. 在您的代码中,查看所选节点的node.toString(),以确认它正在选择云,以确保这是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM