android - 一触式读取标签中的 NDEF 消息两次(无需移动手机)

标签 android android-intent tags nfc ndef

我通过从 NFC 标签读取 NDEF 消息成功地获得了一切。读取后,我移动手机,它可以再次读取标签。

我使用 onNewIntent 和 foregroundDispatch 来处理消息。

问题是: 我想在不移动手机(不再次触摸标签)的情况下读取同一个 NFC 标签两次(出于安全原因)。所以对于一次触摸,我想读两次。

我尝试查看生命周期,但似乎如果您不移动手机,它就不会再次发出新的 Intent 。

private NfcAdapter nfc = null;
private boolean inReadMode = false;
private boolean isNFC_support = false;
private PendingIntent mPendingIntent;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ignore the unrelevent part of layout
    isNFC_support = true;
    nfc = NfcAdapter.getDefaultAdapter(this);
    if(nfc == null) {
        Toast.makeText(this, "Not support NFC device.", Toast.LENGTH_LONG).show();
        isNFC_support = false;
    }
    if(!nfc.isEnabled()) {
        Toast.makeText(this, "Please go the setting and enable NFC first.", Toast.LENGTH_LONG).show();
        isNFC_support = false;
    }
    if (isNFC_support == true) {
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    }
}

@Override
protected void onNewIntent(Intent intent) {
    Log.i("NFC", "---- onNewIntent called ---- ");
    if (this.inReadMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Log.i("NFC", "---- onNewIntent called ---- AND read nfc success!");
        try {
            readFromTag(intent);
        } catch (Exception e) {
            Log.e("NFC", "nfc cmac validate: ", e);
        }
    }
}

@Override
public void onResume() {
    super.onResume();
    Log.i("NFC", "---- onResume called ---- ");
    nfc.enableForegroundDispatch(this, mPendingIntent, null, null);
}


@Override
public void onPause() {
    Log.i("NFC", "---- onPause called ---- ");
    if (nfc != null) {
        nfc.disableForegroundDispatch(this);
    }
    if (isFinishing()) {
        cleanupReadingFromTag();
    }
    super.onPause();
}

private void readFromTag(Intent intent) throws RuntimeException, NoSuchAlgorithmException, IOException {
    Parcelable[] msgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    // code I handle the message as I get. Not important for read twice I think?
}

那么我怎样才能再次读取该标签呢?

最佳答案

NFC Intent 仅在检测到 NFC 标签时调度。如果标签包含 NDEF 消息,则会自动处理该消息并在 Intent Extra (NfcAdapter.EXTRA_NDEF_MESSAGES) 中与您的应用共享。

如果您想在稍后阶段再次读取标签(并设法保持 NFC 标签持续连接),您将需要直接与标签通信。您可以通过标签句柄对象来完成此操作。该对象(Tag 类)也会在标签检测时传递到您的应用程序(作为 NFC Intent 的附加 Intent ):

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

当标签已连接时,您可以随时使用该对象来启动与标签的通信。例如。要重新读取标签上当前的 NDEF 消息,您可以使用:

Ndef ndef = Ndef.get(tag);
if (ndef != null) {
    try {
        ndef.connect();
        NdefMessage msg = ndef.getNdefMessage();
        // do something with the NDEF message
    } catch (IOException e) {
    } finally {
        try {
            ndef.close();
        } catch (Exception e) {}
    }
}

关于android - 一触式读取标签中的 NDEF 消息两次(无需移动手机),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53328885/

相关文章:

java - Android 异常 Intent ,找不到 Activity

android - 按应用程序图标按钮返回父 Activity

android - Facebook Android 应用程序支持哪些 Intents

html - 中心标签仍然有效吗?

c++ - 远近不一致

java - 在启动时加载单选按钮选择

Java NullPointerException 获取资源

android - 如何通过单击按钮在 ListView 中获取所选项目

android - 在后台服务中使用 TYPE_STEP_COUNTER?

java - 如何在JSP中out.print() HTML属性?