我一直致力于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);
}
}
但是当标签移开时,这就是我需要的。我想向用户显示标签不再可用。有听众吗?
答案 0 :(得分:3)
无法检测到标签何时不再可用,因为标签不靠近标签阅读器操作,它们通过读取标签时读取标签。
当然,您可以在一段时间内通过polling标记实现自己检测标记何时超出范围。如果超出范围,您可以使用委托来触发自己的事件。
答案 1 :(得分:2)
NfcAdapter
有这个ignore
方法。它已经在API 24中添加了。我还没有测试它,但它似乎提供了你需要的东西。它允许您为具体OnTagRemovedListener
设置Tag
。
答案 2 :(得分:0)
有一项功能可以检查卡是否仍然连接称为isConnected()
。可以像这样使用:
try {
ndef.connect();
while(ndef.isConnected()){
//Your code here
}
} catch (IOException e) {
//Error
}