Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't detect node using the capability api

Tags:

wear-os

I am having trouble with the capability api. I am trying to send a message from my android wearable to my mobile. I detect a capability, but no node at all.

private static final String CAPABILITY_NAME = "mobile";

@Override
public void onConnected(Bundle bundle) {
    Log.d(TAG, "CONNECTED TO API");
    setupNode();
    Wearable.CapabilityApi.addCapabilityListener(mApiClient, capabilityListener, CAPABILITY_NAME);
}

private void setupNode() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            Wearable.CapabilityApi.getCapability(
                    mApiClient, CAPABILITY_NAME,
                    CapabilityApi.FILTER_REACHABLE).setResultCallback(new ResultCallback<CapabilityApi.GetCapabilityResult>() {
                @Override
                public void onResult(CapabilityApi.GetCapabilityResult result) {
                    if (result.getCapability() == null) {
                        Log.d(TAG, "we detected no capability");
                    } else {
                        Log.d(TAG, "we detected a capability");
                    }
                    updateCapability(result.getCapability());
                }
            });
        }
    }).start();
}

private void updateCapability(CapabilityInfo capabilityInfo) {
    Set<Node> connectedNodes = capabilityInfo.getNodes();
    nodeId = pickBestNodeId(connectedNodes);
    Log.d(TAG, "nodeId : " + nodeId);

    if (nodeId != null) {
        sendMessage(MESSAGE_PATH_READY, "connectionToMobile"); //try a config message to mobile
    } else {
        //if (mobileAppIsRunning) {
            Intent intent = new Intent();
            String status = "DISCONNECTED";
            intent.putExtra("status", status);
            intent.setAction("com.orange.myapp.watch.moto360.receiveDeviceEvent");
            sendBroadcast(intent);
        //} else {
            //DO NOTHING
        //}
    }
}

private String pickBestNodeId(Set<Node> nodes) {
    String bestNodeId = null;
    // Find a nearby node or pick one arbitrarily
    for (Node node : nodes) {
        if (node.isNearby()) {
            return node.getId();
        }
        bestNodeId = node.getId();
    }
    Log.d(TAG, "best node id :" + bestNodeId);
    return bestNodeId;
}

CapabilityApi.CapabilityListener capabilityListener =
        new CapabilityApi.CapabilityListener() {
            @Override
            public void onCapabilityChanged(CapabilityInfo capabilityInfo) {
                Log.d(TAG, "CAPABILITY CHANGED!");

                updateCapability(capabilityInfo);
            }
        };

And on the mobile side, I added the following code in the wear.xml file :

<resources>
    <string-array name="android_wear_capabilities">
        <item>mobile</item>
    </string-array>
</resources>

And those are the logs I printed :

02-01 18:09:16.758    4907-4907/? D/WearMessageListenerService﹕ creating service
02-01 18:09:16.811    4907-4907/? D/WearMessageListenerService﹕ mApiClient : com.google.android.gms.common.api.zzg@7ba32
02-01 18:09:17.138    4907-4907/? D/WearMessageListenerService﹕ CONNECTED TO API
02-01 18:09:17.182    4907-4907/? D/WearMessageListenerService﹕ we detected a capability
02-01 18:09:17.214    4907-4907/? D/WearMessageListenerService﹕ best node id :null
02-01 18:09:17.215    4907-4907/? D/WearMessageListenerService﹕ nodeId : null

To send messages from the mobile to the watch, I don't use the capability :

private void sendMessage(final String path, final String text) {
    Wearable.NodeApi.getConnectedNodes(mApiClient)
        .setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
            @Override
            public void onResult(NodeApi.GetConnectedNodesResult result) {
                for (Node node : result.getNodes()) {
                    Wearable.MessageApi.sendMessage(mApiClient, node.getId(), path, text.getBytes());
                }
            }
        });
}

And that works fine, I receive the messages on my watch.

like image 820
krakig Avatar asked Dec 15 '22 08:12

krakig


1 Answers

Well, I found it after digging. I had a problem in the package signatures in my gradle on the wear side. One advice I can give so you don't lose too much time, always check that the package names are the same in your gradle files (wear and mobile).

like image 175
krakig Avatar answered Feb 28 '23 04:02

krakig