跳过内容

开始学习

GitHub

安装

安装

You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:

终端窗口
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下提示:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-nfc` plugin in my project.

如果您更喜欢手动设置,请运行以下命令并按照以下平台特定的说明进行操作:

终端窗口
bun add @capgo/capacitor-nfc
bunx cap sync
import { 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

标题:使只读

尝试将最后发现的标签设置为只读状态。

import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.makeReadOnly();

通过 Android-only 的对等 (peer-to-peer) 共享 NDEF 消息。

import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.share({} as ShareTagOptions);

停止共享之前提供的 NDEF 消息 (Android-only)。

import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.unshare();

获取当前 NFC 适配器状态。

import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.getStatus();

打开系统设置页面,用户可以在此处启用NFC。

import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.showSettings();

检查设备是否支持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;
}

用于使用Android Beam / P2P模式与另一个设备共享NDEF消息的选项。

export interface ShareTagOptions {
records: NdefRecord[];
}

由返回的NFC适配器状态。

export type NfcStatus = 'NFC_OK' | 'NO_NFC' | 'NFC_DISABLED' | 'NDEF_PUSH_DISABLED';

插件通过此事件发送的通用NFC发现事件。

export interface NfcEvent {
type: NfcEventType;
tag: NfcTag;
}

NfcStateChangeEvent

标题:NfcStateChangeEvent

NFC适配器可用性发生变化时发出的事件。

export interface NfcStateChangeEvent {
status: NfcStatus;
enabled: boolean;
}

表示单个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[];
}

描述发生NFC发现的类型的事件。

export type NfcEventType = 'tag' | 'ndef' | 'ndef-mime' | 'ndef-formatable';

本地层返回的完整标签信息的表示

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在上游发生变化时,重新运行同步。

如果您正在使用 开始 为了计划仪表板和API操作,连接它与 使用@capgo/capacitor-nfc 在使用@capgo/capacitor-nfc中, API概述 在API概述中,了解实现细节 简介 在简介中了解实现细节 API密钥 在API密钥中了解实现细节, 设备 在设备中了解实现细节。