【问题标题】:Android Kotlin - How to read NFC tag inside ActivityAndroid Kotlin - 如何在 Activity 中读取 NFC 标签
【发布时间】:2018-10-11 15:32:24
【问题描述】:

我在这里找到了一些关于使用 Android 读取 NFC 标签的最新帖子。 我得到的结论是执行 NFC 读取操作会触发分离的意图。

我想要实现的是只有我当前的活动是以文本/纯格式从 NFC 标签读取 NDEF 消息。

所以第一个问题: 是否有必要在我的清单中列出意图过滤器?

<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="text/plain" />

我认为这不是必需的,因为我确实想通过 NFC 标签事件启动我的应用程序,对吧?

第二个问题:如何让我的 NFC 读取逻辑/功能与我的应用/活动相关?

之后

<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />

我转到我当前的 Activity 并在创建时初始化 NFC 适配器:

mNfcAdapter = NfcAdapter.getDefaultAdapter(this)

读取 nfc 标签 NDEF 消息的下一步是什么? 在调度前台发现与意图相关的内容:

 @Override
protected void onNewIntent(Intent intent) { 

    handleIntent(intent);
}

如果有人有一个想法/示例(Kotlin)如何读取 NFC 标签,那就太好了 从活动中没有启动/发送 NFC 操作之类的东西。

在 iOS 中,例如在 VC 中需要时有一个简单的 NFC 会话。

【问题讨论】:

    标签: android kotlin nfc ndef


    【解决方案1】:

    正确,如果您只想在 Activity 处于前台时接收标签,您可以在运行时注册。您正在寻找的是NfcAdapter 上的enableForegroundDispatch 方法。您可以为要过滤到的特定类型的标签注册PendingIntent,每当检测到标签时,您的Activity 将在onNewIntent() 中收到Intent

    如果您只寻找IsoDep 兼容的 NFC 标签,Kotlin 中的一个简单示例会是什么样子:

    override fun onResume() {
        super.onResume()
    
        NfcAdapter.getDefaultAdapter(this)?.let { nfcAdapter ->
            // An Intent to start your current Activity. Flag to singleTop
            // to imply that it should only be delivered to the current 
            // instance rather than starting a new instance of the Activity.
            val launchIntent = Intent(this, this.javaClass)
            launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
    
            // Supply this launch intent as the PendingIntent, set to cancel
            // one if it's already in progress. It never should be.
            val pendingIntent = PendingIntent.getActivity(
                this, 0, launchIntent, PendingIntent.FLAG_CANCEL_CURRENT
            )
    
            // Define your filters and desired technology types
            val filters = arrayOf(IntentFilter(ACTION_TECH_DISCOVERED))
            val techTypes = arrayOf(arrayOf(IsoDep::class.java.name))
    
            // And enable your Activity to receive NFC events. Note that there
            // is no need to manually disable dispatch in onPause() as the system
            // very strictly performs this for you. You only need to disable 
            // dispatch if you don't want to receive tags while resumed.
            nfcAdapter.enableForegroundDispatch(
                this, pendingIntent, filters, techTypes
            )
        }
    }
    
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
    
        if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) {
            val tag = intent.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)
            IsoDep.get(tag)?.let { isoDepTag ->
                // Handle the tag here
            }
        }
    }
    

    【讨论】:

    • 伟大的先生!正是我想要的。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多