SendMessage from Android mobile service to Wear not received

故事扮演 提交于 2020-01-06 20:14:32

问题


I've been battling this for a while. every test says success, but no message is received. I'm sending from a WearableListenerService to Wear as a reply to a message i've just received.

sending (from mobile WearListenerService):

 @Override
public void onMessageReceived(MessageEvent messageEvent)
{

        apiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).build();
        ConnectionResult connectionResult = apiClient.blockingConnect(10, TimeUnit.SECONDS);

        if (!connectionResult.isSuccess())
        {
            Log.e(TAG, "Failed to connect to GoogleApiClient.");
            return;
        }

        nodeID = messageEvent.getSourceNodeId();
        sendReplyAsync("some reply");

}

private void sendReplyAsync(String msg)
{

    PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi
            .sendMessage(apiClient, nodeID, REPLY_PATH,
                         msg.getBytes(Charset.forName("UTF-8")));

    messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>()
    {
        @Override
        public void onResult(MessageApi.SendMessageResult result)
        {
            confirm(result.getStatus().isSuccess());
        }
    });
}

The log shows that the reply is sent. And If i query the connected nodes, the one that im sending to is indeed correct.

then over in Wear:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    apiClient =
            new GoogleApiClient.Builder(this).addApi(Wearable.API)
                    .addConnectionCallbacks(this)
                    .build();

    apiClient.connect();
}

@Override
protected void onDestroy()
{
    if (DBG) Log.d(TAG, "onDestroy");
    Wearable.MessageApi.removeListener(apiClient, apiClientMessageListener);
    super.onDestroy();
}

@Override
public void onConnected(Bundle bundle)
{
    if (DBG) Log.d(TAG, "adding message listener");
    Wearable.MessageApi.addListener(apiClient, apiClientMessageListener);

    // sends message here to mobile.
}


 private MessageApi.MessageListener apiClientMessageListener = new             MessageApi.MessageListener()
{
    @Override
    public void onMessageReceived(MessageEvent messageEvent)
    {
        String path = messageEvent.getPath();
        if (DBG) Log.d(TAG, "received message from " + path);

        // nothing ever received here
    }
};

As far as i can tell, everything is OK. Could there be something weird about sending from a service. When i sent from the MainActivity it worked, but now i have to put the sending in a service.

any ideas appreciated. thanks.

来源:https://stackoverflow.com/questions/35191832/sendmessage-from-android-mobile-service-to-wear-not-received

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!