Send message to android device, which sends messag

2019-07-23 13:19发布

问题:

I am writing an app for android which communicates to the device and back to the wear. This is the code of onclick on wear.

 message1Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE1_PATH, null).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                @Override
                public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                    Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);
                    if (sendMessageResult.getStatus().isSuccess()) {

                        intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION);
                        intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.message1_sent));
                    } else {
                        intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.FAILURE_ANIMATION);
                        intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.error_message1));
                    }
                    startActivity(intent);
                }
            });

The following is the code for on received on Handheld device.

 public void onMessageReceived(MessageEvent messageEvent) {
            if (messageEvent.getPath().equals(MESSAGE1_PATH)) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        receivedMessagesEditText.append("\n" + getString(R.string.received_message1));
                        String serverURL = "A Working URL";
                        try {
                            ArrayList<String> list=new ArrayList<String>();
                            list.add(0,serverURL);
                            list.add(1,"SELECT DISTINCT ACCOUNTNO FROM CUSTOMR_WISE_WALLET_POINTS ORDER BY ACCOUNTNO");
                            jsonContent= new LongOperation().execute(list).get();
                        } catch (InterruptedException e) {
                            Log.e("Error",e.getMessage());
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        }

                        Log.d("Value of json",jsonContent);

                    }
                });
                Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE4_PATH, jsonContent.getBytes()).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                    @Override
                    public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                        if (sendMessageResult.getStatus().isSuccess())
                            Toast.makeText(getApplication(), "Message 4 sent", Toast.LENGTH_SHORT).show();
                        else
                            Toast.makeText(getApplication(), getString(R.string.error_message1), Toast.LENGTH_SHORT).show();
                    }
                });
            } else if (messageEvent.getPath().equals(MESSAGE2_PATH)) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        receivedMessagesEditText.append("\n" + getString(R.string.received_message2));
                    }
                });
            }
        }

The handheld device shows "Message 4 sent" toast. but the message listener on the wear is never triggered.The code for which is.

messageListener = new MessageApi.MessageListener() {
        @Override
        public void onMessageReceived(final  MessageEvent messageEvent) {
            Log.d("Inside","On message recieved");
            if (messageEvent.getPath().equals(MESSAGE1_PATH)) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        receivedMessagesEditText.append("\n" + getString(R.string.received_message1));
                        /*COde to send another request*/
                        Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE3_PATH, "Hello".getBytes()).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                            @Override
                            public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                                Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);
                                if (sendMessageResult.getStatus().isSuccess()) {
                                    intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION);
                                    intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, "Message 3 sent");
                                } else {
                                    intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.FAILURE_ANIMATION);
                                    intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.error_message2));
                                }
                                startActivity(intent);
                            }
                        });


                    }
                });
            } else if (messageEvent.getPath().equals(MESSAGE2_PATH)) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        receivedMessagesEditText.append("\n" + getString(R.string.received_message2));
                    }
                });
            }
            else if(messageEvent.getPath().equals(MESSAGE4_PATH)){

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        receivedMessagesEditText.append("\n" + getString(R.string.received_message1));
                        /*COde to send another request*/

                       // String jsonInString=new String(messageEvent.getData());
                        Log.d("Inside Message 4","Inside");
                        String[] items = new String[]{"Select User", "2", "three"};
                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, items);
                        dropdown.setAdapter(adapter);
                        message2Button.setEnabled(true);

                    }
                });

            }
        }
    };

To summarize, I can send the message from wear and trigger messagerecieved on the handhled. The message is successfully sent from handheld but the on message received is not triggered on the wear when called from within the scope of messagelistener . I checked the app Id , they are same.I also tested on message recieved on the wear by triggering with button2 from handheld.It works fine. Thanks in advance

回答1:

MessageApi does not guarantee that a message will be received even a successful result code returned. Check the document here.

Note: A successful result code does not guarantee delivery of the message. If your app requires data reliability, use DataItem objects or the ChannelApi class to send data between devices.