`android.nfc.tech` 是 Android SDK 中用于访问 NFC(近场通信)标签底层技术的包

张开发
2026/4/12 4:24:16 15 分钟阅读

分享文章

`android.nfc.tech` 是 Android SDK 中用于访问 NFC(近场通信)标签底层技术的包
android.nfc.tech是 Android SDK 中用于访问 NFC近场通信标签底层技术的包它提供了一系列抽象类和具体实现类用于与不同类型的 NFC 标签进行交互如 NDEF、ISO-DEP、MifareClassic、NfcA、NfcB、NfcF、NfcV 等。该包在 Kotlin 和 Java 中均可使用API 完全一致只是语法风格不同。核心类说明Kotlin/Java 通用Tag: 表示检测到的 NFC 标签由系统传递不可直接构造。NfcA,NfcB,NfcF,NfcV,IsoDep: 分别对应 ISO/IEC 14443-3A/B、JIS X 6319-4FeliCa、ISO/IEC 15693、ISO/IEC 14443-4 协议的底层通信接口。MifareClassic,MifareUltralight: 厂商特定技术需设备支持 MIFARE 芯片及相应驱动部分 Android 设备不支持。Ndef/NdefFormatable: 用于读写标准 NDEFNFC Data Exchange Format消息NdefFormatable用于格式化未初始化的 NDEF 标签。⚠️ 注意事项所有android.nfc.tech.*类实例必须通过Tag.getTechList()Tag.getTech()或更推荐的TagTechnology.getInstance(tag, techClass)获取Kotlin 中可简写为NfcA.get(tag)等静态工厂方法。使用前需在AndroidManifest.xml中声明权限uses-permissionandroid:nameandroid.permission.NFC/需添加intent-filter响应 NFC Intent如NDEF_DISCOVERED,TECH_DISCOVERED,TAG_DISCOVERED并配置meta-data指定支持的技术尤其TECH_DISCOVERED需nfc_tech_filter.xml。Kotlin 示例获取并读取 NfcA 标签valnfcANfcA.get(tag)// tag 来自 onNewIntent() 或 NfcAdapter.enableReaderMode()nfcA?.let{it.connect()valuidit.uid// ByteArrayLog.d(NFC,UID:${uid.toHexString()})it.close()}Java 示例等效NfcAnfcANfcA.get(tag);if(nfcA!null){try{nfcA.connect();byte[]uidnfcA.getUid();Log.d(NFC,UID: bytesToHex(uid));}catch(IOExceptione){e.printStackTrace();}finally{try{nfcA.close();}catch(IOExceptionignored){}}}✅ 最佳实践优先使用Ndef抽象层处理标准数据仅当需底层控制如防冲突、自定义命令、密钥认证时才用NfcA/MifareClassic等。android.nfc.tech Kotlin |JavaThese classes provide access to a tag technology’s features, which vary by the type of tag that is scanned. A scanned tag can support multiple technologies, and you can find out what they are by calling getTechList().For more information on dealing with tag technologies and handling the ones that you care about, see The Tag Dispatch System. The TagTechnology interface provides an overview of the supported technologies.InterfacesTagTechnology TagTechnology is an interface to a technology in a Tag.ClassesIsoDep Provides access to ISO-DEP (ISO 14443-4) properties and I/O operations on a Tag.MifareClassic Provides access to MIFARE Classic properties and I/O operations on a Tag.MifareUltralight Provides access to MIFARE Ultralight properties and I/O operations on a Tag.Ndef Provides access to NDEF content and operations on a Tag.NdefFormatable Provide access to NDEF format operations on a Tag.NfcA Provides access to NFC-A (ISO 14443-3A) properties and I/O operations on a Tag.NfcB Provides access to NFC-B (ISO 14443-3B) properties and I/O operations on a Tag.NfcBarcode Provides access to tags containing just a barcode.NfcF Provides access to NFC-F (JIS 6319-4) properties and I/O operations on a Tag.NfcV Provides access to NFC-V (ISO 15693) properties and I/O operations on a Tag.

更多文章