快速入门
复制一条配置 prompt,包含安装步骤和该插件的完整 Markdown 指南。
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
标题为“示例: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 概览中的实现细节, 简介 有关 Introduction 中的实现细节, API 密钥 有关 API Keys 中的实现细节,以及 设备 有关 Devices 中的实现细节。