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

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

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).

  1. class MyCapabilityListener(private val context: Context) :
  2. DataClient.OnDataChangedListener,
  3. MessageClient.OnMessageReceivedListener,
  4. CapabilityClient.OnCapabilityChangedListener {
  5. private val capabilityClient: CapabilityClient = Wearable.getCapabilityClient(context)
  6. private val dataClient: DataClient = Wearable.getDataClient(context)
  7. fun sendData(data: String) {
  8. val dataItem = PutDataMapRequest.create("/data/path").run {
  9. dataMap.putString("data", data)
  10. asPutDataRequest()
  11. }
  12. dataClient.putDataItem(dataItem)
  13. }
  14. override fun onCapabilityChanged(capabilityInfo: CapabilityInfo) {
  15. // Check if the wearable device is available
  16. val nodes = capabilityInfo.nodes
  17. if (nodes.isNotEmpty()) {
  18. Log.v("Connected", "Connected to the watch")
  19. // Do something with the connected wearable device
  20. } else {
  21. Log.v("Not connected", "Not connected to the watch")
  22. }
  23. }
  24. fun addListener() {
  25. // Connect to the capability client
  26. Wearable.getCapabilityClient(context)
  27. Wearable.getDataClient(context)
  28. capabilityClient.addListener(this, Uri.parse("wear://"), CapabilityClient.FILTER_REACHABLE)
  29. // Check the current capability status
  30. capabilityClient.getCapability("wear://", CapabilityClient.FILTER_REACHABLE)
  31. .addOnSuccessListener { capabilityInfo ->
  32. onCapabilityChanged(capabilityInfo) // Call onCapabilityChanged manually
  33. }
  34. .addOnFailureListener { e ->
  35. Log.e("Capability", "Failed to get capability: ${e.message}")
  36. }
  37. }
  38. }

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文件夹中添加以下内容:

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

而在可穿戴设备端

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

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

  1. private const val CAPABILITY_PHONE_APP = "verify_remote_phone_app"
  2. val capabilityInfo = capabilityClient
  3. .getCapability(CAPABILITY_PHONE_APP, CapabilityClient.FILTER_ALL)
  4. .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:

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

And on the wearable side:

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

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

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

and

  1. val capabilityInfo = capabilityClient
  2. .getCapability(CAPABILITY_PHONE_APP, CapabilityClient.FILTER_ALL)
  3. .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:

确定