Reputation: 1293
I've got a basic Android Wear app that successfully uses the Wearable Data Layer to send and receive messages between the mobile device and Wear.
However, any time I send a message (from Mobile or Wear device) the other end receives it multiple times (like anywhere from 5-17). Why is it not just received once? As far as I can tell it's only sending once.
After successfully connecting, here is my code to Send Message (same on both ends):
private void sendMsgToWear(final String strMsg){
new AsyncTask<Void, Void, List<Node>>(){
@Override
protected List<Node> doInBackground(Void... params) {
return getNodes();
}
@Override
protected void onPostExecute(List<Node> nodeList) {
for(Node node : nodeList) {
Log.v("DEVELOPER", "......Phone: Sending Msg: " + strMsg + " to node: " + node.getId());
PendingResult<MessageApi.SendMessageResult> result = Wearable.MessageApi.sendMessage(
mGoogleApiClient,
node.getId(),
strMsg,
null
);
result.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
Log.v("DEVELOPER", "......Phone: " + sendMessageResult.getStatus().getStatusMessage());
}
});
}
}
}.execute();
}
private List<Node> getNodes() {
List<Node> nodes = new ArrayList<Node>();
NodeApi.GetConnectedNodesResult rawNodes =
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
for (Node node : rawNodes.getNodes()) {
nodes.add(node);
nodeID = node.getId();
}
return nodes;
}
And here is how I Receive on my activity that implements MessageApi.MessageListener:
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.d("DEVELOPER", "......Wear: successfully received phone to wear communication!");
}
My output on send side looks like:
07-11 10:54:57.646 26920-28711/com.wearTest.message V/DEVELOPER﹕ ......Phone: Sending Msg: testMessage to node: 6ecc099a-60f8-4c3d-90fe-fa2f248c6bf7
07-11 10:54:57.666 26920-26920/com.wearTest.message V/DEVELOPER﹕ ......Phone: null
And the receive side logs:
07-11 10:54:57.962 5509-5672/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.962 5509-5610/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.962 5509-5660/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.962 5509-5521/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.962 5509-5539/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972 5509-5570/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972 5509-5604/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972 5509-5601/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972 5509-5566/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972 5509-5520/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972 5509-5582/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.972 5509-5567/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:57.982 5509-5587/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:58.002 5509-5581/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:58.052 5509-5581/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:58.052 5509-5587/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
07-11 10:54:58.062 5509-5570/com.wearTest.message D/DEVELOPER﹕ ......Wear: successfully received phone to wear communication!
Thanks in advance!
Upvotes: 4
Views: 2735
Reputation: 55370
The MessageApi
should be working fine. Just be careful to call addListener()
only once, or you will get notified multiple times.
Alternatively, add matching removeListener()
calls (for example in onResume()
/ onPause()
).
Upvotes: 2