Cannot detect node using api feature

I'm having problems with api features. I am trying to send a message from my android being portable to mobile. I discovered an opportunity, but not a 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 these are the magazines 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 a mobile to a watch, I do not use this feature:

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 it works great, I get messages on my watch.

+4
source share
1 answer

, . gradle . , , , , gradle ( ).

+5

Source: https://habr.com/ru/post/1626897/


All Articles