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?
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.
You must use Wearable.NodesApi.getConnnectedNodes()
API, as you state.
There is no other method to do this, at least currently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With