GoogleApiClient Node is null

元气小坏坏 提交于 2019-12-11 03:24:56

问题


I'm writing an Android Wear app and I want to make the wear device start an intent on the phone when a button is clicked.

To do this, I've first set up the connection to the GoogleApiClient like this

 googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        googleApiClient.connect();

Then on button click the function sendMessage is run. This is the function

private void sendMessage(){
        if(mNode != null && googleApiClient != null && googleApiClient.isConnected()){
            Wearable.MessageApi.sendMessage(
                    googleApiClient, mNode.getId(),HELLO_WORLD_WEAR_PATH, null).setResultCallback(
                    new ResultCallback<MessageApi.SendMessageResult>() {
                        @Override
                        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                            if(!sendMessageResult.getStatus().isSuccess()){
                                Log.e("TAG", "Failed to send message with status code: " + sendMessageResult.getStatus().getStatusCode());
                            }
                            else{
                                Log.e("TAG", "Success");
                            }
                        }
                    }
            );
        }
        else {
            Log.e("TAG","No nodes" + mNode);

        }
    }

The problem is that the function goes into the else statement of sendMessage which means mNode is null. mNode is a variable of type Node.

To get the node, I have this function

private void resolveNode() {
        Wearable.NodeApi.getConnectedNodes(googleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
            @Override
            public void onResult(NodeApi.GetConnectedNodesResult nodes) {
                for (Node node : nodes.getNodes()) {
                    mNode = node;
                }
            }
        });
    }

I've connected my phone with the Android Wear Virtual Device in Android Studio.

What am I doing wrong here?


回答1:


When you call googleApiClient.connect(), the connection takes a short amount of time and this call does not block. So you need to wait until the connection succeeds with a call to onConnected() before you do any further calls.

Next, when you call resolveNode(), the mNode variable is not set instantly. The setResultCallback is executed a short time later, and so you should not call sendMessage() until the result is processed.

So you need to ensure proper ordering for everything here.

(Edit)

After going through the comments, it turns out the problem was caused by the connection to Google Play Services failing. This was because the emulator used was an old API 20 device, which has an old version of Google Play Services. Using the latest API 22 emulator will resolve the issue.




回答2:


try with this to get Node

new Thread(new Runnable() {
            @Override
            public void run() {
                NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
                for (Node node : nodes.getNodes()){
                    MessageApi.SendMessageResult resultMessage = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), ID_PATH, BYTES HERE).await();

                    if (resultMessage.getRequestId() == MessageApi.UNKNOWN_REQUEST_ID) {
                        Log.e("Message failed", ".......");
                    }
                }
            }
        }).start();


来源:https://stackoverflow.com/questions/31324610/googleapiclient-node-is-null

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