问题描述:

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:

  • Using the Nodelistener API. I want to to something simple - like restarting the watch and see it go offline and online, but I don't receive these events. I've read that it's only hardcore pairing / unpairing of the watch that causes these events to be sent (which seems wrong imo, and it's too complicated to do illustrate the purpose of my app). I've also read this post (https://plus.google.com/+NathanSchwermann/posts/1Rs9etY5qte) but I'm already using a WLS in my app.
  • Using a bluetooth listener instead (Broadcastreceiver listening for BluetoothDevice.ACTION_ACL_CONNECTED and DISCONNECTED actions) which works perfectly - except that I cannot determine if it's a Wear device that has connected. Again, this is because of the NodeApi: Wearable.NodeApi.getConnectedNodes(wearApiClient).setResultCallback ... never gets a result callback. This is what I'm trying to do when getting the ACION_ACL_CONENCTED action by the way - and then I simply want to see how many nodes are connected and determine if it's a newly connected node.

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.

网友答案:

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.

网友答案:

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.

网友答案:

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
    }
相关阅读:
Top