当前位置: 首页 > 知识库问答 >
问题:

Android Kotlin-如何读取活动内部的NFC标签

郭阳泽
2023-03-14

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

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

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

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

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

我认为这是没有必要的,因为我不想推出我的应用程序通过NFC标签事件,对吗?

第二个问题:我如何保持我的NFC阅读逻辑/功能与我的app/活动相关?

<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
mNfcAdapter = NfcAdapter.getDefaultAdapter(this)
 @Override
protected void onNewIntent(Intent intent) { 

    handleIntent(intent);
}

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

共有1个答案

喻珂
2023-03-14

正确,如果您只想在activity处于前台时接收标记,您可以在运行时为此注册。您要查找的是NFCADapter上的EnableForegroundDispatch方法。您可以为要筛选到的特定类型的标记注册PendingIntent,并且每当检测到标记时,您的活动将在onNewIntent()中接收Intent

Kotlin提供了一个简单的示例,说明如果您只需要ISODEP兼容NFC标签,将会是什么样子:

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
        }
    }
}
 类似资料:
  • 我有一个阅读NFC类型A标签(非NDEF)的活动。我用Android手机在开发者模式下运行Android Studio。 该项目在开发者Android手机上正确启动应用程序,并在手机上打开NFC。当我轻触手机上的NFC非接触式卡时,手机会检测到NFC卡,但会显示手机上安装的其他NFC读卡器应用程序的选项列表,而不是将意图传递给前台应用程序。 如何让前台项目的应用程序接收NFC意图,而不是弹出建议列

  • 我有两项活动。我想用第一个活动(如)读卡,第二个活动写卡。因为在发现卡时,活动需要处于活动状态。因此,我对这两项活动都使用了以下设置: 然而,我的问题是,当我在第二个活动中扫描NFC卡时,手机会显示第一个和第二个活动的意向选择器。 那么,当我在第二个活动(反之亦然)中时,如何通过代码禁用第一个活动的intent过滤器? 这是完整的AndroidManifest文件:

  • 类似的问题 - 如何在Android中读取检测到的NFC标签(NDEF内容)详细信息? 我希望我的android应用程序能够读取和解析检测到的NDEF消息。 我已经编辑了AndroidManifest.xml来检测nfc标签,并添加了意图过滤器,如下所示 我相信这很好,因为当我使用SDK附带的NFCDemo示例应用程序创建MockNDEF标签时,当我可以选择处理这些生成的标签的应用程序列表出现时,

  • 我正在尝试使用nfc android库读取ISO15693 RFID标签: 有关该标签的更多信息如下:http://img42.com/gw07d+

  • 如果使用Android应用程序记录(AAR),则需要执行意图操作 所以我不知道这是正常发射还是nfc发射。 我需要在我的活动中做稍微不同的事情,无论活动是正常启动还是在nfc标签阅读之后启动。如果手机上有另一个应用具有相同的目的过滤器,我也不想显示选择应用的提示,例如:

  • 我使用下面的示例代码来读取NFC标签,但它不是多次读取标签(有时读取3次,有时读取6-7次)。在我的应用程序中,我需要连续读取nfc标签。 https://github.com/andijakl/NfcDemo