npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webnfc

v1.0.0

Published

Wrapper library that makes working with NFC tags in supported browser easy and fun.

Downloads

481

Readme

Introduction

The webnfc library provides a simple and easy to user framework for reading and writing NFC tags using WebNFC.

Features

  • can check if the device supports and allows NFC usage.
  • makes it easy to request NFC permission.
  • can read and write to NFC tags.
  • handles edge-cases like duplicate reads.
  • idempotent function which can be called at any time
  • lots of events to provide good insight into inner workings.

For information on all typings, arguments and options available, read the documentation generated from the source code.

My device has support for NFC, it is enabled but your library doesn't see it!

WebNFC is only supported in a small number of mobile browsers, and is only supported when used in a secure context.

While developing your application you might not serve it under HTTPS, in which case you need to aks your browser nicely to support WebNFC despite being in an insecure context.

Chrome

Go to chrome://flags/#unsafely-treat-insecure-origin-as-secure and add an exception to localhost or the IP-number and port you want to use while developing.

Usage

Installation

Installation is easy, just get the library from NPM:

npm install webnfc

Setup

// Import library features.
import
{
	deviceSupportsNFC,
	deviceAllowsNFC,
	requestAccessToNFC,
	nfcEvents,
	listenForTags,
	readFromTag,
	stopReadingFromTags,
	writeToTag,
	stopWritingToTag
} from 'webnfc';

// Check if device supports WebNFC.
if(deviceSupportsNFC())
{
	// Great, the device does support WebNFC.
}

Requesting permission

Prior to reading or writing NFC tags, you need to ensure that the device is allowed to use NFC.

// Check if device allows WebNFC usage.
if(!deviceAllowsNFC())
{
	// No permission at this time.
}

If device does not have the appropriate permission, you need to request the permission from a user interaction, such as a button click.

// Locate a suitable button or some such in your document..
const myButton = document.querySelector('#myButton') as HTMLElement;

// .. then add an event listener that requests permission when user clicks the button.
myButton.addEventListener('click', requestAccessToNFC);

Event handling

When you interact with NFC tags, events will be emitted with various things like the status of the device, but also information on the tags being read or written to.

Detecting permission

After performing a permission request, you will either be granted or denied permission.

// Log when permission is granted.
nfcEvents.on('PermissionGranted', console.log);

// Log when permission is denied.
nfcEvents.on('PermissionDenied', console.log);

Detecting current state

While you perform various NFC related actions, the library will emit events indicating it's current state.

// Log when device is ready to read tags.
nfcEvents.on('ReadStart', console.log);

// Log when device is no longer ready to read tags.
nfcEvents.on('ReadStop', console.log);

// Log when device is ready to write to a tag.
nfcEvents.on('WriteStart', console.log);

// Log when device is no longer ready to write to a tag.
nfcEvents.on('WriteStop', console.log);

Reading events

// Log when device has read a new tag.
nfcEvents.on('TagDetected', console.log);

// Log when device has ignored a tag that was read in duplicate.
nfcEvents.on('TagIgnored', console.log);

// Log the tag records when the device has read a new tag.
nfcEvents.on('TagRecords', console.log);

Writing events

Currently, there are no event on successfully writing to a tag.

Reading tags

Before you can read any tag, you must first first set up your event listened for the TagDetected or TagRecords events.

Reading a single tag.

To read a single tag, simple call readFromTag().

await readFromTag(timeoutInSeconds);

Continuously reading tags

If you want to read multiple tags, use listenForTags().

NOTE: If you set the timeout to 0, it will listen continuously until turned off.

await listenForTags(timeoutInSeconds)

Stop reading tags

If you are listening for tags but want to stop, use stopReadingFromTags().

await stopReadingFromTags(someReason);

Writing tags

Writing a single tag

To write a tag, simply call writeToTag() with the data you want to write.

You will need to provide a mimeType (for example text/plain) and encode your data into an ArrayBuffer prior to calling.

By default the library will not overwrite tags, but you can set overwrite to true and existing data will be replaced.

NOTE: Tags that has an empty record are not empty tags, and needs overwrite to be overwritten. You can format cards to fully erase them.

await writeToTag(mimeType, data, overwrite, timeoutInSeconds);

Writing to multiple tags

This has not yet been implemented, but may come in a future version.

Stop writing to tags

If you are currently trying to write to a tag, but want to stop, you can call stopWritingToTag().

await stopWritingToTag();