快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-nfc`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/nfc/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
“安装”章节你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
bunx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-nfc` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
bun add @capgo/capacitor-nfcbunx cap sync导入
“导入”章节import { CapacitorNfc } from '@capgo/capacitor-nfc';快速入门
章节标题为“快速入门”import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.startScanning();示例:stopScanning
标题为“示例:stopScanning”的章节import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.stopScanning();示例:write
标题为“示例:写入”的章节import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.write({ records: [ { tnf: 1, type: [1], id: [1], payload: [1], }, ],});常见坑点
章节标题为“常见陷阱”- 监听器: 在页面卸载时移除句柄。
- 原生同步:运行
bunx cap sync在插件升级后。 - 唯一可信来源:信任
src/definitions.ts在 GitHub 仓库中。
API 概览
章节标题:“API 概览”startScanning
“startScanning” 部分开始监听 NFC 标签。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.startScanning();stopScanning
“stopScanning” 部分停止正在进行的 NFC 扫描会话。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.stopScanning();write
“write” 部分将提供的 NDEF 记录写入最近发现的标签。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.write({ records: [ { tnf: 1, type: [1], id: [1], payload: [1], }, ],});erase
“erase” 部分尝试通过写入空 NDEF 消息擦除最近发现的标签。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.erase();makeReadOnly
标题为“makeReadOnly”的章节尝试将最近发现的标签设为只读。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.makeReadOnly();share
标题为“share”的章节通过点对点方式与另一台设备共享 NDEF 消息(仅限 Android)。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.share({ records: [ { tnf: 1, type: [1], id: [1], payload: [1], }, ],});unshare
标题为“unshare”的章节停止共享之前提供的 NDEF 消息(仅限 Android)。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.unshare();getStatus
标题为“getStatus”的章节返回当前 NFC 适配器状态。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
const result = await CapacitorNfc.getStatus();console.log(result);showSettings
标题为“showSettings”的部分打开系统设置页面,用户可以在其中启用 NFC。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.showSettings();isSupported
标题为“isSupported”的部分检查设备是否支持 NFC 硬件。
返回 true 如果设备具备 NFC 硬件,无论当前 NFC 是启用还是禁用。返回 false 如果设备不具备 NFC 硬件。
使用此方法来判断是否应在应用的 UI 中显示 NFC 功能。若要检查 NFC 当前是否已启用,请使用 getStatus().
import { CapacitorNfc } from '@capgo/capacitor-nfc';
const result = await CapacitorNfc.isSupported();console.log(result);类型参考
“类型参考” 章节StartScanningOptions
标题为 “StartScanningOptions” 的章节控制 . 行为的选项。
export interface StartScanningOptions { /** * iOS-only: closes the NFC session automatically after the first successful tag read. * Defaults to `true`. */ invalidateAfterFirstRead?: boolean; /** * iOS-only: custom message displayed in the NFC system sheet while scanning. */ alertMessage?: string; /** * iOS-only: session type to use for NFC scanning. * - `'ndef'`: Uses NFCNDEFReaderSession (default). Only detects NDEF-formatted tags. * - `'tag'`: Uses NFCTagReaderSession. Detects both NDEF and non-NDEF tags (e.g., raw MIFARE tags). * Allows reading UID from unformatted tags. * **Requires** the `Near Field Communication Tag Reader Session Formats` entitlement * in your app with the `TAG` format included. Without it the session will fail to * start and the promise will reject with a `NO_NFC` error code. * Defaults to `'ndef'` for backward compatibility. */ iosSessionType?: 'ndef' | 'tag'; /** * Android-only: raw flags passed to `NfcAdapter.enableReaderMode`. * Defaults to enabling all tag types with skipping NDEF checks. */ androidReaderModeFlags?: number;}WriteTagOptions
标题为 “WriteTagOptions” 的章节在当前标签上写入 NDEF 消息时使用的选项。
export interface WriteTagOptions { /** * Array of records that compose the NDEF message to be written. */ records: NdefRecord[]; /** * When `true`, the plugin attempts to format NDEF-formattable tags before writing. * Defaults to `true`. */ allowFormat?: boolean;}ShareTagOptions
标题为 “ShareTagOptions” 的章节使用 Android Beam / P2P 模式与其他设备共享 NDEF 消息时使用的选项。
export interface ShareTagOptions { records: NdefRecord[];}NfcStatus
标题为 “NfcStatus” 的章节由 . 返回的可能 NFC 适配器状态。
export type NfcStatus = 'NFC_OK' | 'NO_NFC' | 'NFC_DISABLED' | 'NDEF_PUSH_DISABLED';NfcEvent
标题为 “NfcEvent” 的章节插件分发的通用 NFC 发现事件。
export interface NfcEvent { type: NfcEventType; tag: NfcTag;}NfcStateChangeEvent
标题为 “NfcStateChangeEvent” 的章节NFC 适配器可用性变化时触发的事件。
export interface NfcStateChangeEvent { status: NfcStatus; enabled: boolean;}NdefRecord
标题为 “NdefRecord” 的章节表示单个 NDEF 记录的 JSON 结构。
export interface NdefRecord { /** * Type Name Format identifier. */ tnf: number; /** * Type field expressed as an array of byte values. */ type: number[]; /** * Record identifier expressed as an array of byte values. */ id: number[]; /** * Raw payload expressed as an array of byte values. */ payload: number[];}NfcEventType
标题为 “NfcEventType” 的章节描述所发生 NFC 发现类型的事件类型。
export type NfcEventType = 'tag' | 'ndef' | 'ndef-mime' | 'ndef-formatable';NfcTag
“NfcTag” 章节表示原生层返回的完整标签信息。
export interface NfcTag { /** * Raw identifier bytes for the tag. */ id?: number[]; /** * List of Android tech strings (e.g. `android.nfc.tech.Ndef`). */ techTypes?: string[]; /** * Human readable tag type when available (e.g. `NFC Forum Type 2`, `MIFARE Ultralight`). */ type?: string | null; /** * Maximum writable size in bytes for tags that expose NDEF information. */ maxSize?: number | null; /** * Indicates whether the tag can be written to. */ isWritable?: boolean | null; /** * Indicates whether the tag can be permanently locked. */ canMakeReadOnly?: boolean | null; /** * Array of NDEF records discovered on the tag. */ ndefMessage?: NdefRecord[] | null;}单一事实来源
标题为“单一事实来源”的部分本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 要规划 dashboard 和 API 操作,请将其连接到 使用 @capgo/capacitor-nfc 用于“使用 @capgo/capacitor-nfc”中的原生能力, API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节,以及 设备 有关 Devices 的实现细节。