获取API异常:在此设备上不可用的Wearable API错误。

4

我在我的MapsActivity中有一个按钮,用于与手表应用进行通信。执行以下Thread

    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();

                }
            });
        }

    }
}

但是当我尝试执行线程时,出现了如下图像中的错误。

enter image description here


你解决了吗? - lcj
1个回答

1
这是在模拟器上发生的,我无法使用WearOS应用程序/ API(可能也适用于没有WearOS应用程序/ API的真实设备)。
这会发生在上。
我没有找到一种干净的方法来检查Wear API是否可用,所以我首先尝试获取能力,在捕获ApiException并设置isWearableAvailable属性:
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
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接