android - Ndef.get(tag) 返回 null

标签 android android-intent tags nfc ndef

我有这段代码,我总是得到 Ndef ndef = Ndef.get(tag) == null。我试图以不同的方式得到它,但我总是在同一个地方得到 null 。有人知道我做错了什么吗?

private ImageView imgSearch;
private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nfc);
}

@Override
protected void onResume() {
    super.onResume();
    mAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mAdapter == null) {
        //nfc not support your device.
        Toast.makeText(this, R.string.no_nfc_supported, Toast.LENGTH_SHORT).show();
    } else {
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    }
    mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}

@Override
protected void onPause() {
    super.onPause();
    if (mAdapter != null) {
        mAdapter.disableForegroundDispatch(this);
    }
}

@Override
protected void onNewIntent(Intent intent) {
    getTagInfo(intent);
}

private void getTagInfo(Intent intent) {
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            NdefMessage ndefMesg = ndef.getCachedNdefMessage();
            if (ndefMesg != null) {
                Toast.makeText(this, ndefMesg.toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}

在我的 list 中:

<uses-permission android:name="android.permission.NFC" />

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

和:

    <activity android:name=".activities.NFCActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain"/>
        </intent-filter>
    </activity>

最佳答案

您正在使用前台调度系统来获取有关新标签的通知

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);

当您将 null 作为 intent 过滤器和技术列表参数(最后两个参数)传递时,您的 Activity 将收到有关位于前台的任何标记的通知。因此,在方法 getTagInfo()(或实际上是 onNewIntent())中,您会收到一个 ACTION_TAG_DISCOVERED Intent 。

private void getTagInfo(Intent intent) {
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        // this part is executed, hence, you received an ACTION_TAG_DISCOVERED intent
    }
}

这意味着无论标签支持何种标签技术,getTagInfo() 中的代码都会被执行。因此,如果标签不支持Ndef标签技术,Ndef.get(tag)将返回null

您可以使用以下方法获取标签支持的技术列表:

String[] techList = tag.getTechList();

如果标签不支持 Ndef 标签技术,这可能意味着标签尚未配置为携带 NDEF 负载,必须首先使用 初始化(“格式化”) >NdefFormatable 技术。

Ndef ndef = Ndef.get(tag);
if (ndef != null) {
    NdefMessage ndefMesg = ndef.getCachedNdefMessage();
    if (ndefMesg != null) {
        ...
    }
} else {
    NdefFormatable ndefFormatable = NdefFormatable.get(tag);
    if (ndefFormatable != null) {
        // initialize tag with new NDEF message
        try {
            ndefFormatable.connect();
            ndefFormatable.format(newNdefMessage);
        } finally {
            try {
                ndefFormatable.close();
            } catch (Exception e) {}
        }
    }
}

但是,如果标签既不支持Ndef也不支持NdefFormatable,这意味着该标签不是NFC论坛标签,根本不支持携带NDEF载荷。

关于android - Ndef.get(tag) 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33784218/

相关文章:

android - 是否可以仅通过 Android 中的单个顶级 Activity 来处理事件总线的事件?

Android 键盘将标签栏置于顶部

java - 无法解析方法 'isIntentAvailable(android.content.intent)'

ios - 使用标记符号访问 UILabel

ruby - Rails 4 表单远程更新 div 问题

java - 使用 SimpleAdapter 将获取的图像加载到 listView

android - 运行示例 android 项目

android - 在 Android 中设置根 Activity/Intent

java - 应用程序在按下按钮时崩溃,并显示 "Attempt to invoke virtual method"

javascript - 有没有办法创建自己的 HTML 元素?