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

Galaxy Watch4 Classic(KQW) Wear OS NFC标签检测问题求助

Wear OS NFC标签检测问题解决方案

问题根源分析

你的代码存在以下几个关键问题,导致无法检测到NFC标签:

  1. 错误使用BroadcastReceiver监听ACTION_TAG_DISCOVERED,Wear OS环境下官方推荐使用**前台调度(Foreground Dispatch)**机制处理NFC事件
  2. Manifest中把TAG_DISCOVERED和启动页的MAIN/LAUNCHER放在同一个Intent Filter中,配置逻辑混乱
  3. 未在Activity生命周期中正确启用/禁用NFC监听,导致前台状态下无法捕获事件

修正步骤及代码

1. 移除冗余的NfcReceiver类

前台调度机制会直接将NFC事件传递到Activity的onNewIntent方法,无需额外的广播接收器。

2. 修改主Activity代码(Detect.kt)

package com.example.detectnfc

import android.app.PendingIntent
import android.content.Intent
import android.nfc.NfcAdapter
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class Detect : AppCompatActivity() {

    private var nfcAdapter: NfcAdapter? = null
    private var pendingIntent: PendingIntent? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wear)

        Log.d("detect", "Activity created")

        nfcAdapter = NfcAdapter.getDefaultAdapter(this)

        // 检测NFC硬件可用性及开关状态
        when {
            nfcAdapter == null -> {
                Log.d("detect", "NFC not available on device")
                Toast.makeText(this, "设备不支持NFC", Toast.LENGTH_SHORT).show()
                finish()
            }
            !nfcAdapter!!.isEnabled -> {
                Log.d("detect", "NFC is disabled")
                Toast.makeText(this, "请在设置中开启NFC", Toast.LENGTH_SHORT).show()
                finish()
            }
            else -> {
                Log.d("detect", "NFC is enabled")
                // 初始化PendingIntent,确保NFC事件回到当前Activity
                val intent = Intent(this, this::class.java).apply {
                    addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
                }
                pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        // 前台时启用NFC事件监听
        nfcAdapter?.enableForegroundDispatch(this, pendingIntent, null, null)
    }

    override fun onPause() {
        super.onPause()
        // 后台时禁用监听,避免资源浪费
        nfcAdapter?.disableForegroundDispatch(this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        intent?.let {
            // 覆盖所有NFC事件类型,确保不遗漏
            when(it.action) {
                NfcAdapter.ACTION_NDEF_DISCOVERED,
                NfcAdapter.ACTION_TECH_DISCOVERED,
                NfcAdapter.ACTION_TAG_DISCOVERED -> {
                    Log.d("detect", "NFC Tag detected: ${it.action}")
                    Toast.makeText(this, "检测到NFC标签", Toast.LENGTH_SHORT).show()
                    // 这里可以添加标签解析逻辑,比如读取NDEF数据
                }
            }
        }
    }
}

3. 修正AndroidManifest.xml配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.detectnfc">

    <uses-feature android:name="android.hardware.type.watch" />

    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">

        <meta-data
            android:name="com.google.android.wearable.standalone"
            android:value="true" />
        <activity
            android:name="com.example.detectnfc.Detect"
            android:label="@string/app_name"
            android:theme="@style/AppTheme"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!-- 移除静态的TAG_DISCOVERED过滤,前台调度不需要此配置 -->
        </activity>
    </application>
</manifest>

关键修改说明

  • 前台调度机制:Wear OS要求只有前台应用才能接收NFC事件,enableForegroundDispatch是官方推荐的可靠方式,比广播接收器更适配穿戴设备的资源限制
  • 生命周期绑定:在onResume启用、onPause禁用监听,确保仅在Activity可见时处理NFC事件
  • 全事件覆盖:同时监听三种NFC事件类型,避免因标签类型不同导致的检测遗漏
  • 简化配置:移除冗余的静态Intent Filter,保持Manifest配置简洁合理

测试注意事项

  1. 确保测试的Wear OS设备具备NFC硬件模块(部分入门款手表无NFC功能)
  2. 测试时保持Activity处于前台,手表屏幕处于唤醒状态
  3. 若需要读取标签内容,可以在onNewIntent中添加NDEF数据解析逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 12:35:55