You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin中如何从Wear OS的Node对象提取手表类型?

提取Wear设备的手表类型字段

你可以直接通过Node类的displayName属性获取设备的完整显示名称,再通过字符串处理提取括号前的手表类型部分。

代码示例

if (wearNodesWithApp!!.isNotEmpty()) {
    val node = wearNodesWithApp!!.first()
    Log.d(TAG, "Found node $node")
    
    // 获取完整显示名称,比如"Galaxy Watch4 (KBGZ)"
    val fullDeviceName = node.displayName
    // 提取括号前的手表类型
    val watchType = fullDeviceName.substringBefore(" (")
    
    Log.d(TAG, "Watch type: $watchType")
    result.success(node.id)
} else result.success("Empty")

关键说明

  • displayName是Node类自带的属性,对应日志输出里Node{}中第一个逗号前的内容,这是官方提供的可靠获取方式,比手动解析toString()输出更稳定。
  • 使用substringBefore(" (")可以精准截取到括号前的纯设备类型名称,比如从"Galaxy Watch4 (KBGZ)"得到"Galaxy Watch4"。

内容的提问来源于stack exchange,提问作者Bo Hellgren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 17:15:12