Lompat ke Konten

Mulai

Jendela Terminal
bun add @capgo/capacitor-nfc
bunx cap sync
import { CapacitorNfc } from '@capgo/capacitor-nfc';

Mengaktifkan pemindaian tag NFC.

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

Menghentikan sesi pemindaian NFC yang sedang berlangsung.

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

Menulis rekaman NDEF yang disediakan ke tag terakhir yang ditemukan.

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

Mencoba menghapus tag terakhir yang ditemukan dengan menulis pesan NDEF kosong.

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

Mencoba membuat tag terakhir yang ditemukan hanya dapat dibaca.

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

Membagikan pesan NDEF dengan perangkat lain melalui peer-to-peer (hanya Android).

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

Menghentikan pembagian pesan NDEF sebelumnya (hanya Android).

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

Mengembalikan status NFC adapter saat ini.

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

Membuka halaman pengaturan sistem di mana pengguna dapat mengaktifkan NFC.

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

Mengecek apakah perangkat memiliki dukungan perangkat keras NFC.

Mengembalikan true jika perangkat memiliki perangkat keras NFC, terlepas dari apakah NFC saat ini diaktifkan atau dinonaktifkan. false jika perangkat tidak memiliki perangkat keras NFC.

Gunakan metode ini untuk menentukan apakah fitur NFC harus ditampilkan di UI aplikasi Anda. Untuk mengecek apakah NFC saat ini diaktifkan, gunakan getStatus().

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

Opsi mengontrol perilaku dari .

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

Opsi digunakan ketika menulis pesan NDEF pada tag saat ini.

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

Opsi digunakan ketika berbagi pesan NDEF dengan perangkat lain menggunakan Android Beam / P2P mode.

export interface ShareTagOptions {
records: NdefRecord[];
}

Status adapter NFC yang mungkin dikembalikan oleh .

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

Event pencarian NFC umum yang dipancarkan oleh plugin.

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

Event yang dipancarkan ketika ketersediaan adapter NFC berubah.

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

Struktur JSON yang mewakili rekaman NDEF tunggal.

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

Jenis acara yang menjelaskan jenis penemuan NFC yang terjadi.

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

Perwakilan informasi tag penuh yang dikembalikan oleh lapisan asli.

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

Halaman ini dihasilkan dari plugin’s. src/definitions.tsRe-run sinkronisasi ketika API publik berubah secara upstream.