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
相关产品推荐
相关产品推荐

