Getting Started
复制一个包含安装步骤和本插件的完整 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.
bun add @capgo/capacitor-nfcbunx cap syncimport { CapacitorNfc } from '@capgo/capacitor-nfc';API 概述
标题为“API 概述”的部分startScanning
开始扫描开始监听NFC标签。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.startScanning();stopScanning
停止扫描停止当前的NFC扫描会话。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.stopScanning();write
写入将提供的NDEF记录写入最后发现的标签。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.write({} as WriteTagOptions);erase
擦除尝试通过写入空的NDEF消息来擦除最后发现的标签。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.erase();makeReadOnly
Section titled “makeReadOnly”尝试将最后发现的标签设置为只读。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.makeReadOnly();通过对等连接(仅限Android)共享NDEF消息。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.share({} as ShareTagOptions);unshare
Section titled “unshare”停止共享之前提供的NDEF消息(仅限Android)。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.unshare();getStatus
Section titled “getStatus”获取当前NFC适配器状态。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.getStatus();showSettings
Section titled “showSettings”打开系统设置页面,用户可以在此处启用NFC。
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.showSettings();isSupported
Section titled “isSupported”检查设备是否支持NFC硬件。
返回 true 如果设备上存在NFC硬件,无论NFC是否当前启用或禁用,均返回true。 false 如果设备上没有NFC硬件,返回false。
使用此方法来确定是否在应用程序UI中显示NFC功能。要检查NFC是否当前启用,请使用 getStatus().
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.isSupported();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
写标签选项用于在当前标签上写入 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
分享标签选项用于使用 Android Beam / P2P 模式共享 NDEF 消息的选项。
export interface ShareTagOptions { records: NdefRecord[];}NfcStatus
NFC 状态由 .返回的可能的 NFC 适配器状态。
export type NfcStatus = 'NFC_OK' | 'NO_NFC' | 'NFC_DISABLED' | 'NDEF_PUSH_DISABLED';NfcEvent
NfcEvent插件发送的通用NFC发现事件。
export interface NfcEvent { type: NfcEventType; tag: NfcTag;}NfcStateChangeEvent
NfcStateChangeEventNFC适配器可用性发生变化时发出的事件。
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 上游发生变化时,请重新运行同步
从开始阶段继续
如果您正在使用开始阶段 规划仪表板和 __CAPGO_KEEP_0__ 操作,连接它 to plan dashboard and API operations, connect it with 使用 @capgo/capacitor-nfc 使用 @capgo/capacitor-nfc API 简介 查看 API 简介 介绍 查看 介绍 API 密钥 查看 API 密钥 设备 查看 设备