所以我有一个应用程序,可以通过手机与我的LG G Watch进行良好的通信。问题是我无法发回消息。这是我从可穿戴设备发送的代码,它始终指示消息已发送:
public void oops(View view){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
for (Node node : nodes.getNodes()) {
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), "/start/MainActivity", "Hello".getBytes()).await();
if (!result.getStatus().isSuccess()) {
Log.e("INFO", "ERROR");
} else {
Log.i("INFO", "Success sent to: " + node.getDisplayName());
}
}
}
});
thread.start();
}
发送后我总是收到成功消息。这是另一端的接收器服务:
public class Reciever extends Service implements MessageApi.MessageListener {
public Reciever() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.i("INFO", "We got it!");
}
}
这是我在我的监视应用程序中使用的完全相同的代码,它可以很好地接收消息。我确信该服务已正确启动并声明,并且两个应用程序具有相同的ID。有什么建议吗?
答案 0 :(得分:2)
我发现你必须在onBind或OnCreate中注册监听器:
Wearable.MessageApi.addListener(googleApiClient, this);