i developing an app for android wear. Below code with explanation of the problem
if(mGoogleApiClient.isConnected()){
K.i("Always called!");
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult nodes) {
K.i("Never called :( ");
for (Node node : nodes.getNodes()) {
Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), message, null);
}
}
});
}
UPD: I solve problem by turn off and turn on my phone again (Nexus 5) May be is there easier way to solve problem?
Tried to add .await() and AsyncTask, but result is the same
I got it working:
Init the Google API client:
Then in your onStart method connect the API client:
And to clean up, in your onStop method:
Here's a full code of how to check it:
add this to the gradle file:
And use this function to check if a wearable is connected:
If you check out the google wear samples, there's a project called FindMyPhone. I think the way they solve your problem is a lot cleaner. They check if the device is connected or disconnected with a background service.
They also add this to the AndroidManifest.xml
I believe you can only call getConnectedNodes once per GoogleApiClient connection. You want to cache the node ID the first time you get the result, and then use the onPeerConnected/Disconnected() callbacks to track whether the node ID is still relevant.