NFC(近距離無線通信)
Plugin 説明内容の英語表記部分について Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。
Android および iOS で NFC タグを読み書きします。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | | |
| linux | | |
| macos | | |
| android | ||
| ios |
はじめに、「nfc」プラグインをインストールしてください。
自分のプロジェクトのパッケージ・マネージャーを使用して依存関係を追加します:
npm run tauri add nfcyarn run tauri add nfcpnpm tauri add nfcbun tauri add nfccargo tauri add nfc-
src-tauriフォルダで次のコマンドを実行して、このプラグインをCargo.toml内のプロジェクトの依存関係に追加します:cargo add tauri-plugin-nfc --target 'cfg(any(target_os = "android", target_os = "ios"))' -
追加したプラグインを初期化するために
lib.rsを修正します:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(mobile)]app.handle().plugin(tauri_plugin_nfc::init());Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
お好みの JavaScript パッケージ・マネージャーを使用して、「JavaScript Guest」バインディングをインストールします:
npm install @tauri-apps/plugin-nfcyarn add @tauri-apps/plugin-nfcpnpm add @tauri-apps/plugin-nfcdeno add npm:@tauri-apps/plugin-nfcbun add @tauri-apps/plugin-nfc
「NFC」プラグインは、iOS では「ネイティブ設定」(OS 固有の設定)が必要です。
iOS で NFC API にアクセスするには、対象の iOS バージョンに適合させ、Info.plist ファイルで使用方法の説明を設定し、アプリケーションに NFC 機能を追加する必要があります。
「NFC」プラグインには iOS 14 以降が必要です。これは Tauri CLI v2.8 以降で作成された Tauri アプリケーションではデフォルトですが、あなたの Xcode プロジェクトを編集して設定可能です。
src-tauri/gen/apple/<project-name>.xcodeproj/project.pbxproj ファイルで、すべての IPHONEOS_DEPLOYMENT_TARGET プロパティを 14.0 に設定します:
/* Begin XCBuildConfiguration section */ <random-id> /* release */ = { isa = XCBuildConfiguration; buildSettings = { ... IPHONEOS_DEPLOYMENT_TARGET = 14.0; }; name = release; }; <random-id> /* debug */ = { isa = XCBuildConfiguration; buildSettings = { ... IPHONEOS_DEPLOYMENT_TARGET = 14.0; }; name = debug; };あるいは、Xcode の General > Minimum Deployments > iOS (一般 > 最小デプロイメント > iOS)設定で「デプロイメント・ターゲット」(公開対象)を設定することもできます。
デプロイメント deployment: 「展開・配置」 開発したソフトウェアやアプリケーションなどを、ユーザーが使えるように、実際の運用環境(本番環境)に配置・公開して、利用可能な状態にする一連の作業やプロセス全般を指す用語。《参考:英辞郎、wikipedia》
iOS では、「NFC」プラグインに NFCReaderUsageDescription 情報プロパティ・リストの値が必要です。この値で、あなたのアプリがなぜ NFC タグのスキャンや書き込みを必要とするのか、その理由を説明する必要があります。
src-tauri/Info.ios.plist ファイルに次のスニペットを追加します:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"> <dict> <key>NFCReaderUsageDescription</key> <string>Read and write various NFC tags</string> // 様々な NFC タグの読み取りと書き込み </dict></plist>さらに、iOS では NFC 機能をアプリケーションに関連付ける必要があります。
この機能は、Xcode のプロジェクト設定内の「Signing & Capabilities」(署名と機能)タブで「+ Capability」(+ 機能)ボタンをクリックし、「Near Field Communication Tag Reading」(近距離無線通信タグ読み取り)機能を選択することで追加できます。
(詳細については、Add a capability to a target(ターゲットに機能を追加する)を参照してください。)
あるいは、gen/apple/<app-name>_iOS/<app-name>_iOS.entitlements ファイルに以下の設定を追加することでも追加できます:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict> <key>com.apple.developer.nfc.readersession.formats</key> <array> <string>TAG</string> </array></dict></plist>「NFC」プラグインは JavaScript と Rust の両方で利用可能で、これにより NFC タグをスキャンしたり、NFC タグに書き込んだりすることができます。
すべてのモバイル・デバイスに NFC タグのスキャン機能が搭載されているわけではありません。スキャンおよび書き込み API を使用する前に、NFC が利用できるかを確認する必要があります。
import { isAvailable } from '@tauri-apps/plugin-nfc';
const canScanNfc = await isAvailable();tauri::Builder::default() .setup(|app| { #[cfg(mobile)] { use tauri_plugin_nfc::NfcExt;
app.handle().plugin(tauri_plugin_nfc::init());
let can_scan_nfc = app.nfc().is_available()?; } Ok(()) })このプラグインは、一般的な NFC タグや、NDEF(NFCデータ交換フォーマット)メッセージを持つ NFC タグをスキャンできます。 NDEF は、入力データを NFC タグにカプセル化(格納)するための標準形式です。
import { scan } from '@tauri-apps/plugin-nfc';
const scanType = { type: 'ndef', // または 'tag',};
const options = { keepSessionAlive: false, // iOSの「NFC スキャン」ダイアログに表示されるメッセージを設定 message: 'Scan a NFC tag', successMessage: 'NFC tag successfully scanned',};
const tag = await scan(scanType, options);tauri::Builder::default() .setup(|app| { #[cfg(mobile)] { use tauri_plugin_nfc::NfcExt;
app.handle().plugin(tauri_plugin_nfc::init());
let tag = app .nfc() .scan(tauri_plugin_nfc::ScanRequest { kind: tauri_plugin_nfc::ScanKind::Ndef { mime_type: None, uri: None, tech_list: None, }, keep_session_alive: false, })? .tag; } Ok(()) })NFC スキャナーは、特定の URI 形式、MIME タイプ、または NFC タグの各技術を利用したタグもフィルタリングすることができます。 この場合、スキャンでは指定されたフィルタリング条件に一致するタグのみが検出されます。
URI 形式 Uniform Resource Identifier: 統一資源識別子(URL と URN)。
MINE タイプ Multipurpose Internet Mail Extensions: 多目的インターネットメール拡張書式。
import { scan, TechKind } from '@tauri-apps/plugin-nfc';
const techLists = [ // NfcF 方式(Felica 方式)を用いて何でもキャプチャします [TechKind.NfcF], // NDEF(NFC データ変換フォーマット)のデータ内容(ペイロード)を用いてすべての MIFARE Classics をキャプチャします [TechKind.NfcA, TechKind.MifareClassic, TechKind.Ndef],];
const tag = await scan({ type: 'ndef', // または 'tag' mimeType: 'text/plain', uri: { scheme: 'https', host: 'my.domain.com', pathPrefix: '/app', }, techLists,});tauri::Builder::default() .setup(|app| { #[cfg(mobile)] { use tauri_plugin_nfc::NfcExt;
app.handle().plugin(tauri_plugin_nfc::init());
let tag = app .nfc() .scan(tauri_plugin_nfc::ScanRequest { kind: tauri_plugin_nfc::ScanKind::Ndef { mime_type: Some("text/plain".to_string()), uri: Some(tauri_plugin_nfc::UriFilter { scheme: Some("https".to_string()), host: Some("my.domain.com".to_string()), path_prefix: Some("/app".to_string()), }), tech_list: Some(vec![ vec![tauri_plugin_nfc::TechKind::Ndef], ]), }, })? .tag; } Ok(()) })NFC の通信方式: 大きく分けると三種類あります:
- Type-A 方式(NFC-A)= 国際標準で普及版(データ暗号化なし)
- Type-B 方式(NFC-B)= 国際標準の高セキュリティ版
- Type-F 方式(NFC-F)= 日本の主流方式(Felica)
MIFARE マイフェア: NXPセミコンダクターズが開発した非接触 IC カード/RFID の通信規格(国際規格)。その中の MIFARE Classic 規格は設計が古く、セキュリティに脆弱性が発見されており、新規採用は非推奨。《参考 wikipedia》
write API を使用して、NFC タグにペイロード(デジタル・データ)を書き込むことができます。
もし keepSessionAlive: true を用いてスキャンされたタグがない場合(「NFC タグのスキャン」項の Note 欄参照)、アプリケーションは最初に NFC タグをスキャンします。
import { write, textRecord, uriRecord } from '@tauri-apps/plugin-nfc';
const payload = [uriRecord('https://tauri.app'), textRecord('some payload')];
const options = { // 「種類 kind」は、スキャンされたタグセッションが稼働していない場合にのみ必要です // その形式は scan() に渡される引数と同じです kind: { type: 'ndef', }, // iOSの「Scan NFC(NFC をスキャン)」ダイアログに表示されるメッセージを設定します message: 'Scan a NFC tag', successfulReadMessage: 'NFC tag successfully scanned', successMessage: 'NFC tag successfully written',};
await write(payload, options);tauri::Builder::default() .setup(|app| { #[cfg(mobile)] { use tauri_plugin_nfc::NfcExt;
app.handle().plugin(tauri_plugin_nfc::init());
app .nfc() .write(vec![ tauri_plugin_nfc::NfcRecord { format: tauri_plugin_nfc::NFCTypeNameFormat::NfcWellKnown, kind: vec![0x55], // URI の記録 id: vec![], payload: vec![], // ここにペイロードを挿入 } ])?; } Ok(()) })デフォルトでは、潜在的に危険なプラグイン・コマンドとそのスコープ(有効範囲)はすべてブロックされており、アクセスできません。これらを有効にするには、capabilities 設定でアクセス権限を変更する必要があります。
詳細については「セキュリティ・レベル Capabilities」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。
{ "permissions": [ ..., "nfc:default", ]}Default Permission
This permission set configures what kind of operations are available from the nfc plugin.
Granted Permissions
Checking if the NFC functionality is available and scanning nearby tags is allowed. Writing to tags needs to be manually enabled.
This default permission set includes the following:
allow-is-availableallow-scan
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the is_available command without any pre-configured scope. |
|
|
Denies the is_available command without any pre-configured scope. |
|
|
Enables the scan command without any pre-configured scope. |
|
|
Denies the scan command without any pre-configured scope. |
|
|
Enables the write command without any pre-configured scope. |
|
|
Denies the write command without any pre-configured scope. |
【※ この日本語版は、「Aug 16, 2025 英語版」に基づいています】
© 2025 Tauri Contributors. CC-BY / MIT