Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to detect if an Android Wear device is connected to the phone?

I want my app to behave differently if an Android Wear device is currently connected. What is the easiest way to detect this?

I believe that you could use this method which is in Google Play Services, but that seems like a lot of overhead just to see if the device is there or not. Is there any other way?

like image 969
jzplusplus Avatar asked Jul 07 '14 20:07

jzplusplus


2 Answers

NodeAPI can check whether your Android Wear device and phone are connected or not in a simple way like this:

This is the callback function when you call:

private void checkNodeAPI(){
    Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(this, 1000, TimeUnit.MILLISECONDS);
}

//Callback

@Override
public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
    if (getConnectedNodesResult != null && getConnectedNodesResult.getNodes() != null){
        mWearableConnected = false;
        for (Node node : getConnectedNodesResult.getNodes()){
            if (node.isNearby()){
                mWearableConnected = true;
            }
        }
    }
    Log.v("TEST", "mWearableConnected: " + mWearableConnected);
}

getConnectedNodes() is an async function, so the result is just returned after a while, you can not get it immediately.

To receive the event when an Android Wear connects/disconnects to your phone, you can use this below function to add listener:

Wearable.NodeApi.addListener(mGoogleApiClient, this);

@Override
public void onPeerConnected(Node node) {
    Log.v("TEST", "onPeerConnected");
    mWearableConnected = true;
}

@Override
public void onPeerDisconnected(Node node) {
    Log.v("TEST", "onPeerDisconnected");
    checkNodeAPI();
}

But this listener is not always called on some Android Wear devices.

like image 178
Ken Avatar answered Sep 28 '22 10:09

Ken


You must use Wearable.NodesApi.getConnnectedNodes() API, as you state.

There is no other method to do this, at least currently.

like image 33
matiash Avatar answered Sep 28 '22 10:09

matiash