如何使用Kotlin连接到WearOS伴侣手表应用程序?

huangapple go评论68阅读模式
英文:

How to connect to a WearOS companion watch app using Kotlin?

问题

我已经尝试了好几天,试图将Jetpack Compose应用连接到WearOS手表(模拟器),但我就是弄不明白。我已经成功地配对了模拟器(Wear OS应用显示已连接到模拟器),但我无法让它们在我的应用程序中连接起来。我已经查看了示例、文档,与ChatGPT讨论过,但似乎无论我做什么都不起作用。所以我在想我是否漏掉了某个步骤。

最终,我希望能够从我的蓝牙类向手表发送数据(可能使用消息客户端)。

在手表端,我只有一个WearableListenerService()类,其中包含一个用于接收数据的重写函数。

我期望会收到onCapabilityChanged回调,但这从未发生过,当使用capabilityClient.getCapability手动检查时,我总是得到"未连接到手表"的日志消息。我在onCreate函数中调用MyCapabilityListener(context).addListener

英文:

For days now I have been trying to connect a Jetpack Compose app to an WearOS watch (simulator), and I just can't figure it out. I have successfully paired the simulators (Wear OS app shows Connected to emulator), but I can't get them to connect in my app. I have looked at examples, documentation, concurred with chatGPT but nothing I seem to do works. So I am wondering if I am missing a step somewhere.

In the end I want to be able to send data from my bluetooth class to the watch (probably using the message client).

 class MyCapabilityListener(private val context: Context) :
    DataClient.OnDataChangedListener,
    MessageClient.OnMessageReceivedListener,
    CapabilityClient.OnCapabilityChangedListener {

    private val capabilityClient: CapabilityClient = Wearable.getCapabilityClient(context)
    private val dataClient: DataClient = Wearable.getDataClient(context)

    fun sendData(data: String) {
        val dataItem = PutDataMapRequest.create("/data/path").run {
            dataMap.putString("data", data)
            asPutDataRequest()
        }
        dataClient.putDataItem(dataItem)
    }

    override fun onCapabilityChanged(capabilityInfo: CapabilityInfo) {
        // Check if the wearable device is available
        val nodes = capabilityInfo.nodes
        if (nodes.isNotEmpty()) {
            Log.v("Connected", "Connected to the watch")
            // Do something with the connected wearable device
        } else {
            Log.v("Not connected", "Not connected to the watch")
        }
    }

    fun addListener() {
        // Connect to the capability client
        Wearable.getCapabilityClient(context)
        Wearable.getDataClient(context)
        capabilityClient.addListener(this, Uri.parse("wear://"), CapabilityClient.FILTER_REACHABLE)
        // Check the current capability status
        capabilityClient.getCapability("wear://", CapabilityClient.FILTER_REACHABLE)
            .addOnSuccessListener { capabilityInfo ->
                onCapabilityChanged(capabilityInfo) // Call onCapabilityChanged manually
            }
            .addOnFailureListener { e ->
                Log.e("Capability", "Failed to get capability: ${e.message}")
            }
    }

}

On the watch side I have just have a WearableListenerService() class with an override fun for data that gets received.

I am expecting an onCapabilityChanged callback, but that never happens and when manually checking with capabilityClient.getCapability I always get the "Not connected", "Not connected to the watch", log statement back. I call MyCapabilityListener(context).addListener in the onCreate fun.

答案1

得分: 1

你是否在使用wear-os示例?你可能会在这里找到有关向手表发送数据的答案,以及在这里检查远程连接。我认为你可能错过了双方的res/values/wear.xml

因此,在手机端,你需要在values文件夹中添加以下内容:

<resources>
    <string-array name="android_wear_capabilities">
        <!-- 重要说明:应与Wear res/values/wear.xml中的能力不同。 -->
        <item>verify_remote_example_phone_app</item>
    </string-array>
</resources>

而在可穿戴设备端

<resources>
    <string-array name="android_wear_capabilities">
        <!-- 重要说明:应与应用程序res/values/wear.xml中的能力不同。 -->
        <item>verify_remote_example_wear_app</item>
    </string-array>
</resources>

然后,你可以参考你的wear.xml,而不是使用getCapability("wear://", CapabilityClient.FILTER_REACHABLE)。就像这样:

private const val CAPABILITY_PHONE_APP = "verify_remote_phone_app"

val capabilityInfo = capabilityClient
            .getCapability(CAPABILITY_PHONE_APP, CapabilityClient.FILTER_ALL)
            .await()
英文:

Are you using the wear-os samples? You might find your answer for sending data to your watch here and checking remote connection here.

What I think you might be missing is the res/values/wear.xml on both sides.

So on the mobile side you would need to add this in the values folder:

&lt;resources&gt;
    &lt;string-array name=&quot;android_wear_capabilities&quot;&gt;
   &lt;!-- IMPORTANT NOTE: Should be different than capability in Wear res/values/wear.xml. --&gt;
        &lt;item&gt;verify_remote_example_phone_app&lt;/item&gt;
    &lt;/string-array&gt;
&lt;/resources&gt;

And on the wearable side:

&lt;resources&gt;
    &lt;string-array name=&quot;android_wear_capabilities&quot;&gt;
   &lt;!-- IMPORTANT NOTE: Should be different than capability in App res/values/wear.xml. --&gt;
        &lt;item&gt;verify_remote_example_wear_app&lt;/item&gt;
    &lt;/string-array&gt;
&lt;/resources&gt;

Then instead of using getCapability(&quot;wear://&quot;, CapabilityClient.FILTER_REACHABLE) you can refer to your wear.xml. Like this:

private const val CAPABILITY_PHONE_APP = &quot;verify_remote_phone_app&quot;

and

val capabilityInfo = capabilityClient
            .getCapability(CAPABILITY_PHONE_APP, CapabilityClient.FILTER_ALL)
            .await()

huangapple
  • 本文由 发表于 2023年2月26日 20:53:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定