【问题标题】:Nfc tag detected/lost listenerNFC 标签检测/丢失侦听器
【发布时间】:2015-05-31 15:40:53
【问题描述】:

我一直在研究 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);

    }
}

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

【问题讨论】:

标签: android nfc


【解决方案1】:

当标签不再可用时无法检测到,因为标签不是通过靠近标签阅读器来操作的,它们通过在经过阅读器时读取标签来工作。

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

【讨论】:

  • 按照here的规定,是否可以通过轮询。然而,据我所知,没有基于事件的方法。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    有一个函数可以检查卡是否仍然连接,称为isConnected()。可以这样使用:

    try {
        ndef.connect();
        while(ndef.isConnected()){
    
        //Your code here
    
        }
    } catch (IOException e) {
        //Error
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 2016-07-07
      相关资源
      最近更新 更多