Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@aya-mash/web-nfc-api

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aya-mash/web-nfc-api

A TypeScript library for working with the Web NFC API

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

@aya-mash/web-nfc-api

A TypeScript library for working with the Web NFC API in modern browsers.

Installation

npm install @aya-mash/web-nfc-api

Usage

import { WebNFC } from '@aya-mash/web-nfc-api';

const nfc = WebNFC.getInstance();

// Check if NFC is supported
if (nfc.isSupported()) {
  try {
    // Request permission
    const permission = await nfc.requestPermission();
    
    // Start scanning
    await nfc.startScanning();
    
    // Listen for NFC readings
    nfc.onReading((message) => {
      console.log('NFC message:', message);
    });
    
    // Handle reading errors
    nfc.onReadingError((error) => {
      console.error('NFC error:', error);
    });
    
    // Write to NFC tag
    await nfc.write({
      records: [{
        recordType: 'text',
        data: 'Hello from Web NFC!'
      }]
    });
  } catch (error) {
    console.error('NFC operation failed:', error);
  }
}

Features

  • 🔒 TypeScript support with full type definitions
  • 🎯 Simple, intuitive API
  • 🔄 Singleton pattern for consistent NFC access
  • ⚡ Async/await support
  • 🛡️ Error handling
  • 📱 Permission management

Requirements

  • Chrome for Android (version 89 or later)
  • HTTPS connection (except for localhost)
  • Device with NFC hardware

API Reference

WebNFC

getInstance()

Returns the singleton instance of the WebNFC class.

const nfc = WebNFC.getInstance();

isSupported()

Checks if Web NFC API is supported in the current browser.

const supported = nfc.isSupported();

requestPermission()

Requests NFC permission from the user.

const permission = await nfc.requestPermission();

startScanning(options?: NFCReadOptions)

Starts scanning for NFC tags.

await nfc.startScanning();

write(message: NFCMessage, options?: NFCWriteOptions)

Writes data to an NFC tag.

await nfc.write({
  records: [{
    recordType: 'text',
    data: 'Hello, NFC!'
  }]
});

onReading(callback: (message: NFCMessage) => void)

Sets up a callback for when an NFC tag is read.

nfc.onReading((message) => {
  console.log('Tag read:', message);
});

onReadingError(callback: (error: Error) => void)

Sets up a callback for handling reading errors.

nfc.onReadingError((error) => {
  console.error('Reading error:', error);
});

License

MIT

Keywords

nfc

FAQs

Package last updated on 12 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts