简体   繁体   English

使用按钮读取NFC卡(Isodep)

[英]Reading NFC card using a button(Isodep)

I am quite new to android development and trying to read an nfc card and was wondering if it's possible to read it when the user presses a button. 我对android开发非常陌生,试图读取nfc卡,并且想知道当用户按下按钮时是否可以读取它。 I know how to read a card once the card is near the reader or when it is tapped to the nfc reader (onTagDiscovered). 我知道一旦卡片靠近读卡器或将其轻触到nfc读卡器(onTagDiscovered)时如何读取卡。 I would like to know if it's possible when a user presses a button then the device will read the card. 我想知道是否有可能当用户按下按钮时设备会读取卡。

Android not working as this. Android不能这样工作。

  1. You could declare in your AndroidManifest that an Activity in your application could read NFC tag. 您可以在AndroidManifest中声明应用程序中的Activity可以读取NFC标签。

When a tag is detected by system, 当系统检测到标签时,

  • if only your application declared that could reading NFC tags then your Activity is launched with discovered tag in Intent 如果仅您的应用程序声明可以读取NFC标签,则您的Activity将在Intent使用发现的标签启动
  • if multiple application declared that could reading NFC tags then a "popup" is shown to user to choose application 如果多个应用程序声明可以读取NFC标签,则显示“弹出窗口”以供用户选择应用程序

The other solution 另一种解决方案

  1. You use enableForgroundDispatch : https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch 您使用enableForgroundDispatchhttps : //developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch

The foreground dispatch system allows an activity to intercept an intent and claim priority over other activities that handle the same intent. 前台分派系统允许活动拦截意图并声明优先于其他处理相同意图的活动。 Using this system involves constructing a few data structures for the Android system to be able to send the appropriate intents to your application. 使用此系统涉及为Android系统构造一些数据结构,以便能够将适当的意图发送到您的应用程序。 To enable the foreground dispatch system 启用前台调度系统

If multiple applications "listen" to NFC tag and your Activity is in foreground and enableForgroundDispatch then only your application, receive NFC discovered Tag . 如果多个应用程序“监听” NFC标签,并且您的“ Activity处于前台状态,并且enableForgroundDispatch则仅您的应用程序收到NFC发现的Tag。

With previous mechanism, you could catch all tags in your Activity and when user press a button do required action, when user not press button, do nothing. 使用以前的机制,您可以捕获“ Activity所有标记,并且当用户按下按钮时执行所需的操作,而当用户未按下按钮时则什么也不做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM