Android NFC一般使用方法

本文介绍Android NFC的基本配置方法,包括AndroidManifest配置、NFC状态检查、NFC标签读写及不同Intent类型的处理流程。同时提供了代码示例,帮助开发者更好地理解和使用Android NFC功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android NFC官网文档地址:官网文档1官网地址2

一般使用方法:

1、 AndroidManifest配置(可不配置):

  <action android:name="android.nfc.action.TAG_DISCOVERED" />
  • 1> (ACTION_NDEF_DISCOVERED:如果扫描到包含 NDEF 负载的标签,并且可识别其类型,则使用此 Intent 启动 Activity。这是优先级最高的 Intent,标签调度系统会尽可能尝试使用此 Intent 启动 Activity,在行不通时才会尝试使用其他 Intent。
  • 2> ACTION_TECH_DISCOVERED:如果没有登记要处理 ACTION_NDEF_DISCOVERED Intent 的 Activity,则标签调度系统会尝试使用此 Intent 来启动应用。此外,如果扫描到的标签包含无法映射到 MIME 类型或 URI 的 NDEF 数据,或者该标签不包含 NDEF 数据,但它使用了已知的标签技术,那么也会直接启动此 Intent(无需先启动 ACTION_NDEF_DISCOVERED)。
  • 3> ACTION_TAG_DISCOVERED:如果没有处理 ACTION_NDEF_DISCOVERED 或者 ACTION_TECH_DISCOVERED Intent 的 Activity,则使用此 Intent 启动 Activity。

2、 NfcAdapter.getDefaultAdapter(this)获取nfc adapter实列,isEnabled可判断nfc是否可用;

3、 Activity的onNewIntent会回调nfc读取的相应intent内容, NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action判断来自什么类型的vfc内容。

4、 val pendingIntent = PendingIntent.getActivity( this, PENDING_RESULT, intent, PendingIntent.FLAG_UPDATE_CURRENT)

NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch –使用前台调度,这种方式不用AndroidManifest注册intent-filter

5、 支持的标签技术:

说明
TagTechnology这是所有标签技术类都必须实现的接口。
NfcA提供对 NFC-A (ISO 14443-3A) 属性和 I/O 操作的访问权限。
NfcB提供对 NFC-B (ISO 14443-3B) 属性和 I/O 操作的访问权限。
NfcF提供对 NFC-F (JIS 6319-4) 属性和 I/O 操作的访问权限。
NfcV提供对 NFC-V (ISO 15693) 属性和 I/O 操作的访问权限。
IsoDep提供对 ISO-DEP (ISO 14443-4) 属性和 I/O 操作的访问权限。
Ndef提供对 NDEF 格式的 NFC 标签上的 NDEF 数据和操作的访问权限。
NdefFormatable为可设置为 NDEF 格式的标签提供格式化操作。

可选择支持的标签技术:

说明
MifareClassic提供对 MIFARE Classic 属性和 I/O 操作的访问权限(如果此 Android 设备支持 MIFARE)。
MifareUltralight提供对 MIFARE Ultralight 属性和 I/O 操作的访问权限(如果此 Android 设备支持 MIFARE)。

6、代码相关:

  • 1>当前是否支持NFC功能:
    AndroidManifest(permission):
    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.NFC_PREFERRED_PAYMENT_INFO" />
    // Initialize the NFC adapter
    private fun initNfc() {
        // Gets the default NFC adapter for the system
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this)
        textBtn?.apply {
            text = if (mNfcAdapter == null) {
                "The current phone does not support NFC"
            } else if (!mNfcAdapter?.isEnabled!!) {
                "Please enable NFC in System Settings first"
            } else {
                "Mobile phones currently support NFC"
            }
        }
    }
  • 2>注册当前页面接收的NFC类型(IsoDepNfcANfcB…):
      override fun onResume() {
        super.onResume()
    
        Log.d("$TAG==", "onResume")
        mNfcAdapter?.let {
            if (!it.isEnabled) {
                return
            }
            val intent =
                Intent(this, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
            val pendingIntent = PendingIntent.getActivity(
                this,
                PENDING_RESULT,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )
            //It can define more than one here, but pay attention to the collocation, or there will be no callbacks
            val techLists = arrayOf(
                arrayOf(
                    IsoDep::class.java.name
//                    ,
//                    NfcA::class.java.name,
//                    NfcB::class.java.name,
//                    NfcF::class.java.name,
//                    Ndef::class.java.name,
//                    NdefFormatable::class.java.name
                )
            )
            try {
                val filters = arrayOf(IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED, "*/*"))
                it.enableForegroundDispatch(this, pendingIntent, filters, techLists)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
  • 3>接收当前页面感应到的NFC信息,通过intent接收传递的信息,action来区分:
  override fun onNewIntent(intent: Intent) {
        Log.d(
            "$TAG== onNewIntent==",
            "$TAG ${intent.action} " +
                    "    ${intent.extras} " +
                    " ${intent.flags} "
        )
        super.onNewIntent(intent)
        Toast.makeText(this, "$TAG ${intent.action} $intent", Toast.LENGTH_LONG).show()
        Log.d(
            "$TAG== onNewIntent==",
            "NfcAdapter.ACTION_NDEF_DISCOVERED => ${NfcAdapter.ACTION_NDEF_DISCOVERED}"
        )

        if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) {
            intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)?.also { rawMessages ->
                val messages: List<NdefMessage> = rawMessages.map { it as NdefMessage }
                // Process the messages array.
                Log.d("==nfc message==", messages.toString())
            }
        } else if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) {
            val extraTag = intent.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)
            Log.d(
                "$TAG==", "${intent.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)}"
                        + ",ACTION_NDEF_DISCOVERED==${intent.getParcelableArrayExtra(NfcAdapter.ACTION_NDEF_DISCOVERED)}"
                        + ",EXTRA_NDEF_MESSAGES==${intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)}"
            )
            extraTag?.let {
                Log.d("$TAG==", "id:${extraTag.id},techList:${extraTag.techList}")
            }
        }
    }
  • 4>发送NFC信息example:
  //nfcv process
    private fun processTagNfcV(tag: Tag?) {
        val nfcV = NfcA.get(tag)
        Log.d("$TAG==NfcA ", "${nfcV}")
        if (nfcV != null) {
            // Create a connection
            nfcV.connect()
            val data = ByteArray(20)
            // Send a message
            val response: ByteArray = nfcV.transceive(data)
            Log.d("$TAG==response ", "${response.let {
                NdefRecord(
                    TNF_ABSOLUTE_URI,
                    "https://developer.android.com/index.html".toByteArray(Charset.forName("US-ASCII")),
                    it,
                    it
                )
            }}")
            // close junction
            nfcV.close()
        }
    }

  • 4.1>读写NFC信息(官网示例):
   class MifareUltralightTagTester {

	    //write 
        fun writeTag(tag: Tag, tagText: String) {
            MifareUltralight.get(tag)?.use { ultralight ->
                ultralight.connect()
                Charset.forName("US-ASCII").also { usAscii ->
                    ultralight.writePage(4, "abcd".toByteArray(usAscii))
                    ultralight.writePage(5, "efgh".toByteArray(usAscii))
                    ultralight.writePage(6, "ijkl".toByteArray(usAscii))
                    ultralight.writePage(7, "mnop".toByteArray(usAscii))
                }
            }
            
            fun readTag(tag: Tag): String? {
                return MifareUltralight.get(tag)?.use { mifare ->
                    mifare.connect()
                    val payload = mifare.readPages(4)
                    String(payload, Charset.forName("US-ASCII"))
                }
            }
        }
    }
  

  • 5>当前页面停止接收:
    override fun onPause() {
        super.onPause()
        //forbidden nfc
        mNfcAdapter?.isEnabled.takeIf { it != true }
            ?: mNfcAdapter?.disableForegroundDispatch(this)
        Log.d("$TAG", "onPause")
    }



end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值