Joakim
Joakim

Reputation: 3284

Determine if Android Wear device is online

I'm developing an Android application that also uses notifications on an Android Wear device. It's crucial to my applications functionality to know if the Wear device is connected or not - and it seems impossible to determine.

I have tried the following:

So none of this works and I'm out of ideas. It seems like the NodeApi doesn't work as a developer wants to use it and I really need this functionality.

Does anyone know a clever way to get around this?

**UPDATE: **

Just to clarify, I want to get a callback when devices connect and disconnect.

Upvotes: 4

Views: 1382

Answers (3)

Alessandro Prete
Alessandro Prete

Reputation: 460

As posted in Snow's answer, the correct method is:

List<Node> connectedNodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await().getNodes();

or, if you can't call the await() (for example if you are on the main UI thread):

Wearable.NodeApi.getConnectedNodes(wearApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
    @Override
    public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
        List<Node> nodes = getConnectedNodesResult.getNodes();
        // Do your stuff with connected nodes here
    }
});

The point here, is that the ResultCallback will give your the currently connected wearable devices, so it will be called only once (unless you call a getConnectedNodes() again), not every time a device connects or disconnects.

Upvotes: 1

Oded Breiner
Oded Breiner

Reputation: 29729

I'm using a much simpler method that is suitable for my use case, check if the android wear app is installed:

    try {
        getPackageManager().getPackageInfo("com.google.android.wearable.app", PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        //android wear app is not installed
    }

Upvotes: 0

Tony Malghem
Tony Malghem

Reputation: 222

You can check the number of nodes connected to your device with
List<Node> connectedNodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await().getNodes();
But be careful, Google Glass counts as a node, so if you have a smartwatch and a Google Glass connected to your handheld, you will have 2 nodes, not one.

Upvotes: 1

Related Questions