Jayson Tamayo
Jayson Tamayo

Reputation: 2811

Getting API Exception Wearable API is not available on this device error

I have a button in my MapsActivity that communicates to the Wear app. The following Thread is executed:

    class NewThread extends Thread {
    String path;
    String message;
    NewThread(String p, String m) {
        path = p;
        message = m;
    }

    public void run() {

        Task<List<Node>> wearableList =
                Wearable.getNodeClient(getApplicationContext()).getConnectedNodes();
        try {

            List<Node> nodes = Tasks.await(wearableList);
            for (Node node : nodes) {
                Task<Integer> sendMessageTask =

                        Wearable.getMessageClient(MapsActivity.this).sendMessage(node.getId(), path, message.getBytes());

                try {
                    Integer result = Tasks.await(sendMessageTask);
                    sendmessage("I just sent the wearable a message " + sentMessageNumber++);

                } catch (final ExecutionException exception) {
                    MapsActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toasty.error(MapsActivity.this, "[1] Something went wrong. Error details: " + exception.getMessage()).show();

                        }
                    });
                    } catch (final InterruptedException exception) {
                    MapsActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toasty.error(MapsActivity.this, "[2] Something went wrong. Error details: " + exception.getMessage()).show();
                        }
                    });

                }

            }

        } catch (final ExecutionException exception) {
            MapsActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toasty.error(MapsActivity.this, "[3] Something went wrong. Error details: " + exception.getMessage()).show();

                }
            });
        } catch (final InterruptedException exception) {
            MapsActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toasty.error(MapsActivity.this, "[4] Something went wrong. Error details: " + exception.getMessage()).show();

                }
            });
        }

    }
}

But I get the following error in the image when trying to accomplish the Thread.

enter image description here

Upvotes: 4

Views: 900

Answers (1)

Jan Kubovy
Jan Kubovy

Reputation: 421

This happens on an Emulator where the WearOS app/api is not available to me (Probably on real devices which do not have WearOS App/Api too).

It happens on the Wearable.getMessageClient(MapsActivity.this).sendMessage(node.getId(), path, message.getBytes());.

I didn't find a clean way of checking the Wear API to be available so I am trying to get the capabilities first, catching the ApiException and setting a isWearableAvailable property:

private fun checkCapability() {
    try {
        val capability = capabilityClient
                .getCapability(CAPABILITY_XYZ, CapabilityClient.FILTER_REACHABLE)
                .await()
        isConnected = capability.nodes.any(Node::isNearby)
        if (BuildConfig.DEBUG) Log.d(LOG_TAG,
                "Capability: ${capability.nodes.joinToString()}} > ${isConnected}")
        isWearableApiAvailable = true
    } catch (e: ApiException) {
        isWearableApiAvailable = false
    }
}

Upvotes: 1

Related Questions