Langsung ke konten

Panduan Memulai

GitHub

Anda bisa menggunakan AI-Assisted Setup kami untuk menginstal plugin ini. Tambahkan skill Capgo ke tool AI Anda dengan command berikut:

Jendela terminal
bunx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

Lalu gunakan prompt berikut:

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

Jika Anda lebih memilih Manual Setup, instal plugin dengan menjalankan perintah berikut dan ikuti petunjuk khusus platform di bawah ini:

Jendela terminal
bun add @capgo/capacitor-nfc
bunx cap sync
import { CapacitorNfc } from '@capgo/capacitor-nfc';
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.startScanning();
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.stopScanning();
import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.write({
records: [
{
tnf: 1,
type: [1],
id: [1],
payload: [1],
},
],
});
  • Listener: hapus handle saat screen unmount.
  • Sinkronisasi Native: jalankan bunx cap sync setelah upgrade plugin.
  • Sumber kebenaran tunggal: percayai src/definitions.ts di repo GitHub.

Mulai mendengarkan tag NFC.

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

Menghentikan sesi pemindaian NFC yang sedang berjalan.

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

Menulis record NDEF yang diberikan ke tag terakhir yang ditemukan.

import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.write({
records: [
{
tnf: 1,
type: [1],
id: [1],
payload: [1],
},
],
});

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 menjadi read-only.

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

Berbagi pesan NDEF dengan perangkat lain via peer-to-peer (Android saja).

import { CapacitorNfc } from '@capgo/capacitor-nfc';
await CapacitorNfc.share({
records: [
{
tnf: 1,
type: [1],
id: [1],
payload: [1],
},
],
});

Menghentikan berbagi pesan NDEF yang diberikan sebelumnya (Android saja).

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

Mengembalikan status adaptor NFC saat ini.

import { CapacitorNfc } from '@capgo/capacitor-nfc';
const result = await CapacitorNfc.getStatus();
console.log(result);

Membuka halaman pengaturan sistem agar pengguna dapat mengaktifkan NFC.

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

Memeriksa apakah perangkat mendukung hardware NFC.

Mengembalikan true jika perangkat memiliki hardware NFC, terlepas dari apakah NFC saat ini aktif atau nonaktif. Mengembalikan false jika perangkat tidak memiliki hardware NFC.

Gunakan metode ini untuk menentukan apakah fitur NFC harus ditampilkan di UI app Anda. Untuk mengecek apakah NFC sedang aktif, gunakan getStatus().

import { CapacitorNfc } from '@capgo/capacitor-nfc';
const result = await CapacitorNfc.isSupported();
console.log(result);

Opsi yang mengatur perilaku .

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 saat 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 saat berbagi pesan NDEF dengan perangkat lain via Android Beam / mode P2P.

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 discovery NFC umum yang dikirim oleh plugin.

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

Event yang dipicu setiap kali ketersediaan adapter NFC berubah.

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

Struktur JSON yang merepresentasikan satu record NDEF.

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

Tipe event yang mendeskripsikan jenis discovery NFC yang terjadi.

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

Representasi informasi tag lengkap yang dikembalikan oleh native layer.

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 dibuat dari plugin src/definitions.ts. Jalankan ulang sync saat API publik berubah di upstream.

Jika Anda menggunakan Panduan Memulai untuk merencanakan operasi dashboard dan API, hubungkan dengan Menggunakan @capgo/capacitor-nfc untuk kapabilitas native saat menggunakan @capgo/capacitor-nfc, Ringkasan API untuk detail implementasi di API Overview, Pendahuluan untuk detail implementasi di Introduction, Kunci API untuk detail implementasi di API Keys, dan Perangkat untuk detail implementasi di Devices.