Send message to android device, which sends message back to android wear, but on received is never triggered on wear

牧云@^-^@ 提交于 2019-12-24 17:50:00

问题


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.



来源:https://stackoverflow.com/questions/37247548/send-message-to-android-device-which-sends-message-back-to-android-wear-but-on

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