在 kotlin (Android) 中获取 nodeID 时遇到问题

问题描述 投票:0回答:1

我正在尝试编写一个配套应用程序,将 Play 商店链接从手机推送到佩戴 OS 手表以安装我的表盘。我有一个按下按钮时调用的函数。它应该获取 Wear OS 设备的 NodeID 并将链接推送到 Wear OS 设备。当我使用 Android Studio 将手机置于无线调试模式并在手机(已连接到我的手表)上运行该应用程序时,该按钮显示为响应式,并且显示 toast 消息,但我的手表上没有任何反应。我添加了一条 toast 消息来显示 nodeID,它返回为“null”,这让我相信应该检索 nodeID 的代码部分不起作用。 Android Studio 中没有任何错误。谁能帮我弄清楚为什么这段代码没有成功获取nodeID?

@Composable
fun InstallButton() {
    val context = LocalContext.current
    OutlinedButton(onClick = {
        context.sendLink()
    }) {
    Text("Install on Watch")
    }
}

fun Context.sendLink() {
    var watchNode: Node? = null
    val uri: String = [package uri]
    // Creates a thread
    Thread {
        // Get all nodes (nodes are devices connected)
       val nodeListTask: Task<List<Node>> =
           Wearable.getNodeClient(applicationContext).connectedNodes
        try {
            // Try to get the first node and assign it to the watchNode variable
            watchNode = Tasks.await(nodeListTask)[0]
            } catch (ignore: Exception) {
        }
    }.start()
    Toast.makeText(this, "NodeID: " + watchNode, Toast.LENGTH_SHORT).show()
    
    ...more code for RemoteActivityHelper....}
android kotlin wear-os wearables
1个回答
0
投票

尝试查看这个示例(它已从谷歌存储库中删除,但仍然可以访问)

特别是在移动应用程序中的这个功能

openPlayStoreOnWearDevicesWithoutApp()

  nodesWithoutApp.forEach { node ->
            lifecycleScope.launch {
                try {
                    remoteActivityHelper
                        .startRemoteActivity(
                            targetIntent = intent,
                            targetNodeId = node.id
                        )
                        .await()

                    Toast.makeText(
                        this@MainMobileActivity,
                        getString(R.string.store_request_successful),
                        Toast.LENGTH_SHORT
                    ).show()
                } 

请注意,此函数会检查可放入应用程序的穿戴版本中的功能名称。然后您可以通过检查功能名称来验证穿戴应用程序是否已安装

© www.soinside.com 2019 - 2024. All rights reserved.