メインコンテンツへスキップ

はじめに

GitHub

AIアシストセットアップを使ってプラグインをインストールできます。以下のコマンドで、お使いのAIツールに Capgo スキルを追加してください:

ターミナルウィンドウ
bunx 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';

クイックスタート

「Quick start」セクション
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],
},
],
});

よくある落とし穴

「Common pitfalls」セクション
  • リスナー: 画面のアンマウント時にハンドルを削除してください。
  • ネイティブ同期: 実行 bunx cap sync プラグインのアップグレード後。
  • 信頼できる情報源: 信頼する src/definitions.ts GitHub リポジトリ内に

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({
records: [
{
tnf: 1,
type: [1],
id: [1],
payload: [1],
},
],
});

空のNDEFメッセージを書き込むことで、最後に検知したタグの消去を試みます。

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

最後に検出されたタグを読み取り専用に設定します。

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

ピア・ツー・ピア経由で別のデバイスと NDEF メッセージを共有します(Android のみ)。

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

以前に提供した NDEF メッセージの共有を停止します(Android のみ)。

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

現在の NFC アダプターの状態を返します。

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

ユーザーがNFCを有効にできるシステム設定ページを開きます。

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

デバイスがNFCハードウェアをサポートしているか確認します。

を返します true NFCが現在有効か無効かに関係なく、デバイスにNFCハードウェアが搭載されている場合は、次を返します: false デバイスにNFCハードウェアが搭載されていない場合。

アプリのUIにNFC機能を表示すべきかどうかを判断するには、このメソッドを使用してください。NFCが現在有効かどうかを確認するには、次を使用します: getStatus().

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

. の動作を制御するオプション。

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

現在のタグに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;
}

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 が変更されたら、sync を再実行してください。

以下を使用している場合: はじめに ダッシュボードと API の操作を計画するには、以下と連携してください @capgo/capacitor-nfc の使用方法 @capgo/capacitor-nfc でネイティブ機能を使用する場合、 API の概要 API Overview の実装の詳細については、 はじめに Introduction の実装の詳細については、 API キー API Keys の実装の詳細については、 デバイス Devices の実装の詳細については。