自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
安全 NFC
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 支持脱离主机的卡模拟,也就是具有安全元件的 NFC 卡模拟。如需了解详情,请参阅基于主机的卡模拟概览。
在某些使用情形(例如使用 FeliCa 进行传输)中,当设备的屏幕处于锁定或关闭状态或者设备已关闭时,允许进行脱离主机的卡模拟。
安全 NFC 是 Android 10 中引入的一项功能,允许仅在设备屏幕处于解锁状态时启用脱离主机的 NFC 卡模拟。通过实现此功能,用户可以选择启用安全 NFC,以提高安全性。
实现
如需实现安全 NFC 功能,设备必须具有支持 NCI 2.0 标准的 NFC 控制器,并且必须使用 Android 开源项目 (AOSP) NFC 框架。您可以使用 config_skuSupportsSecureNfc
属性在 NFC 资源 XML 文件中添加支持安全 NFC 功能的硬件 (ro.boot.hardware.sku
)。
框架 API
如需实现安全 NFC,请实现 Android 开源项目中提供的以下框架 API:
设置界面
在“设置”应用中,添加切换开关以允许用户启用和停用安全 NFC 功能。您可以将“设置”应用中的默认设置自定义为启用或停用。
图 1 的示例显示的是在“设置”应用中启用和停用安全 NFC 功能(步骤为依次点按设置 > 已连接的设备 > 连接偏好设置 > NFC > 必须解锁设备才能使用 NFC)的切换开关。
图 1. 启用和停用安全 NFC 功能的切换开关示例
如果启用了安全 NFC 功能,则在用户将设备放在 NFC 读取器上时,Android 会在锁定屏幕上显示需要解锁才能使用 NFC 通知,如图 2 所示。
图 2. 锁定屏幕上显示的“需要解锁才能使用 NFC”通知
验证
如需验证您的实现,请启用安全 NFC 功能,并验证在设备屏幕处于关闭或锁定状态以及设备已关闭时是否停用了 NFC 卡模拟。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-25。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-25。"],[],[],null,["# Secure NFC\n\nAndroid supports off-host card emulation, which means\nNFC card emulation with a secure element. For more information, see\n[Host-based card emulation overview](https://developer.android.com/guide/topics/connectivity/nfc/hce).\n\nIn certain use cases such as using\n[FeliCa](https://en.wikipedia.org/wiki/FeliCa)\nfor transit, off-host card\nemulation is permitted when a device's screen is locked or turned off, or\nwhen a device is turned off.\n\nSecure NFC is a feature introduced in Android 10\nthat allows off-host NFC card\nemulation to be enabled only when the device's screen is unlocked. Implementing\nthis feature gives users the option to enable Secure NFC for\nimproved security.\n\nImplementation\n--------------\n\nTo implement the Secure NFC feature, the device must have an NFC controller that\nsupports the NCI 2.0 standard and must use the Android Open Source Project\n(AOSP) NFC framework. Add the hardware (`ro.boot.hardware.sku`) that supports\nthe Secure NFC feature in the NFC\n[resource XML file](https://android.googlesource.com/platform/packages/apps/Nfc/+/refs/heads/android16-release/res/values/config.xml)\nwith the `config_skuSupportsSecureNfc` attribute.\n\n### Framework APIs\n\nTo implement Secure NFC, implement the following framework APIs found in the\nAndroid Open Source Project:\n\n- [`isSecureNfcSupported()`](https://developer.android.com/reference/android/nfc/NfcAdapter#isSecureNfcSupported()): Checks if the device supports the Secure NFC feature.\n- [`isSecureNfcEnabled()`](https://developer.android.com/reference/android/nfc/NfcAdapter#isSecureNfcEnabled()): Checks if the Secure NFC feature is enabled.\n- [`enableSecureNfc(boolean enable)`](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android16-release/core/java/android/nfc/NfcAdapter.java#1722): Enables the Secure NFC feature.\n\n### Settings UI\n\nIn the Settings application, add a toggle switch to allow users to enable and\ndisable the Secure NFC feature. You can customize the default setting in the\nSettings application to be either enabled or disabled.\n\nFigure 1 shows an example of a toggle switch to enable and disable Secure NFC\nin the Settings app in **Settings \\\u003e Connected devices \\\u003e\nConnection preferences \\\u003e NFC \\\u003e Require device unlock for NFC**.\n\n**Figure 1.** Example toggle switch to enable and disable Secure NFC\n\nWhen Secure NFC is enabled and a user places the device on an NFC reader,\nAndroid displays the **Unlock to use NFC** notification on the lock screen\nas shown in Figure 2.\n\n**Figure 2.** Unlock to use NFC notification on the lock screen\n\nValidation\n----------\n\nTo validate your implementation, enable the Secure NFC feature and verify that\nNFC card emulation is disabled when the device's screen is off or locked, and\nwhen the device is turned off."]]