我一直在争夺这个问题。每个测试都表示成功,但没有收到任何消息。我正在从WearableListenerService发送到Wear,作为对刚刚收到的消息的回复。
发送(来自移动设备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());
}
});
}
日志显示已发送回复。如果我查询连接的节点,我发送到的节点确实是正确的。
然后穿上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
}
};
据我所知,一切都好。从服务发送会有什么奇怪的吗?当我从MainActivity发送它时,它工作,但现在我必须把发送服务。
任何想法都赞赏。感谢。