跳过内容

开始使用

终端窗口
bun add @capgo/capacitor-nfc
bunx cap sync
import { CapacitorNfc } from '@capgo/capacitor-nfc';

开始监听NFC标签。

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

停止当前的NFC扫描会话。

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

将提供的NDEF记录写入最后发现的标签。

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

尝试通过写入一个空的NDEF消息来清除最后发现的标签。

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

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

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

通过对等连接(仅限Android)将NDEF消息共享给另一个设备。

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

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

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

getStatus

获取状态

获取当前NFC适配器状态。

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

showSettings

显示设置

在系统设置页面中打开,用户可以在这里启用NFC功能。

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

检测设备是否支持NFC硬件。

返回结果 true 如果设备上存在NFC硬件,无论NFC当前是否已启用或禁用,均返回 false 如果设备没有NFC硬件。

使用此方法来确定是否在您的应用程序UI中显示NFC功能。要检查是否当前已启用NFC,请使用__CAPGO_KEEP_0__ 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

Nfc事件

插件通过此事件派发的通用 NFC 发现事件

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

复制到剪贴板

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

复制到剪贴板

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;
}

当公共 __CAPGO_KEEP_0__ 上游更改时,请重新运行同步 src/definitions.ts. Re-run the sync when the public API changes upstream.