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

NFC发送NdefRecord却收到android.nfc.tech.IsoDep问题排查求助

问题修复方案

核心问题定位

你当前接收端返回的标签技术列表仅包含IsoDep、NfcA,无NDEF相关技术类型,且触发的是TECH_DISCOVERED/TAG_DISCOVERED而非NDEF_DISCOVERED,所以无法直接读取到NDEF格式的文本数据,问题主要出在配置和逻辑处理两部分。

修复步骤

1. 修正接收端Manifest的Intent过滤器配置

你发送端创建的是text/plain类型的MIME NdefRecord,接收端的NDEF_DISCOVERED意图过滤器没有指定匹配的MIME类型,导致系统不会将对应的NDEF事件分发到你的Activity。
修改接收端Manifest的Activity配置:

<activity
    android:name=".MainActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <!-- 新增单独的NDEF_DISCOVERED过滤器,匹配text/plain类型 -->
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
        <action android:name="android.nfc.action.TAG_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

额外确认:发送端createApplicationRecord指定的包名com.example.nfcreceiver必须和接收端实际包名完全一致,否则应用无法被拉起。

2. 修复接收端onNewIntent的逻辑漏洞

当前你在onNewIntent中仅更新了Intent,没有触发NDEF内容解析,P2P传输触发onNewIntent时不会重新走onResume生命周期,导致事件被漏处理:

@Override
protected void onNewIntent(final Intent intent) {
    super.onNewIntent(intent);
    new Handler().postDelayed(() -> {
        setIntent(intent);
        // 新增:主动调用解析逻辑
        resolveIntent(intent);
    }, 0);
}

3. 增加非NDEF_DISCOVERED场景下的主动读取逻辑

如果事件触发的是TECH_DISCOVERED,你可以主动从Tag对象中读取NDEF内容,修改sendToFirebase方法:

private void sendToFirebase(Intent intent) {
    String title, message;
    NdefMessage[] messages = null;
    // 先处理NDEF_DISCOVERED场景
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        messages = getNdefMessages(intent);
    } else {
        // 非NDEF_DISCOVERED场景主动读取NDEF内容
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (tag != null) {
            Ndef ndef = Ndef.get(tag);
            if (ndef != null) {
                try {
                    ndef.connect();
                    messages = new NdefMessage[]{ndef.getNdefMessage()};
                    ndef.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    // 后续原有解析messages的逻辑保持不变
    // ...
}

4. 适配高版本Android系统

  • Android 10及以上系统已废弃Android Beam功能,需要手动进入系统NFC设置页面开启Android Beam,否则发送端无法正常推送NDEF消息,发送成功时发送端会弹出Message sent.的Toast,可先确认是否发送成功。
  • Android 12及以上系统要求PendingIntent必须指定可变性标识,修改接收端PendingIntent创建逻辑:
// 适配Android 12+
int flag = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_IMMUTABLE : 0;
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
        getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), flag);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 15:15:03