Nfc 标签 detected/lost 侦听器

Nfc tag detected/lost listener

我一直在研究 NFC MifareClassic 卡。我已经在卡上读取和写入数据。 我想在检测到我拥有的标签时向用户显示通知,

    @Override
    protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    // 1) Parse the intent and get the action that triggered this intent
    String action = intent.getAction();
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
        //  3) Get an instance of the TAG from the NfcAdapter

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


        AppController.getInstance().setTag(tagFromIntent);

    }
}

但是当标签移开时,这就是我所需要的。我想向用户显示标签不再可用。有监听器吗?

无法检测到标签何时不再可用,因为标签不会通过接近标签 reader 进行操作,它们会在经过 [=17] 时通过读取标签来工作=].

您当然可以在一段时间内通过 polling 标签实现自己的标签超出范围的检测。如果超出范围,您可以使用委托触发 own 事件。

NfcAdapter 有这个 ignore 方法。它是在 API 24 中添加的。我还没有测试过它,但它似乎提供了你需要的东西。它允许您为具体 Tag.

设置 OnTagRemovedListener

有一个名为 isConnected() 的函数可以检查卡是否仍处于连接状态。可以这样使用:

try {
    ndef.connect();
    while(ndef.isConnected()){

    //Your code here

    }
} catch (IOException e) {
    //Error
}