Copyright © 2015 the Contributors to the Web NFC API Specification, published by the Web NFC Community Group under the W3C Community Contributor License Agreement (CLA). A human-readable summary is available.
Near Field Communication (NFC) enables wireless communication between two devices at close proximity, usually less than a few centimeters. NFC is an international standard (ISO/IEC 18092) defining an interface and protocol for simple wireless interconnection of closely coupled devices operating at 13.56 MHz (see https://www.nfc-forum.org/specs/spec_list/).
This document defines an API to enable selected use-cases based on NFC technology.
This specification was published by the Web NFC Community Group. It is not a W3C Standard nor is it on the W3C Standards Track. Please note that under the W3C Community Contributor License Agreement (CLA) there is a limited opt-out and other conditions apply. Learn more about W3C Community and Business Groups.
Implementers need to be aware that this specification is considered unstable. Implementers who are not taking part in the discussions will find the specification changing out from under them in incompatible ways. Vendors interested in implementing this specification before it eventually reaches the Candidate Recommendation phase should subscribe to the repository on GitHub and take part in the discussions.
Significant changes to this document since last publication are documented in the Changes section.
If you wish to make comments regarding this document, please send them to public-web-nfc@w3.org (subscribe, archives).
As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.
The key words MAY, MUST, MUST NOT, SHOULD, and SHOULD NOT are to be interpreted as described in [RFC2119].
This document defines conformance criteria that apply to a single product: the UA (user agent) that implements the interfaces it contains.
Implementations that use ECMAScript to implement the APIs defined in this document MUST implement them in a manner consistent with the ECMAScript Bindings defined in the Web IDL specification [WEBIDL], as this document uses that specification and terminology.
The terms URL and URL path are defined in [URL].
The following terms are defined in [HTML5]: browsing context, top-level browsing context, global object, incumbent settings object, script execution environment, Document, document base URL, Window, WindowProxy, origin, ASCII serialized origin, executing algorithms in parallel, queue a task, task source, iframe, valid MIME type.
A browsing context refers to the environment in which
Document objects are presented to the user. A given
browsing context has a single WindowProxy
object,
but it can have many Document
objects, with their associated
Window
objects. The script execution environment
associated with the browsing context identifies the entity which
invokes this API, which can be a web app, a web page, or an
iframe.
The term secure context is defined in [WEBAPPSEC].
Inspired by the Streams specification, we use the notation x@[[y]] to refer to internal slots of an object, instead of saying "the [[y]] internal slot of x".
Internal slots are used only as a notation in this specification, and implementations do not necessarily have to map them to explicit internal properties.
The Augmented Backus-Naur Form (ABNF) notation used is specified in [RFC5234].
DOMString
,
ArrayBuffer
,
BufferSource
and
any
are defined in [WEBIDL].
DOMException, AbortError, SyntaxError, NotSupportedError, NotFoundError, NetworkError, NoModificationAllowedError, SecurityError are defined in [DOM4].
Promise, JSON, JSON.stringify and JSON.parse are defined in [ECMASCRIPT].
The algorithms utf-8 encode, and utf-8 decode are defined in [ENCODING].
IANA media types (formerly known as MIME types) are defined in RFC2046.
NFC stands for Near Field Communications, short-range wireless technology operating at 13.56 MHz which enables communication between devices at a distance less than 10 cm. The NFC communications protocols and data exchange formats, and are based on existing radio-frequency identification (RFID) standards, including ISO/IEC 14443 and FeliCa. The NFC standards include ISO/IEC 18092[5] and those defined by the NFC Forum. See https://www.nfc-forum.org/specs/spec_list/ for a complete listing.
An NFC adapter is the software entity in the underlying platform which provides access to NFC functionality implemented in a given hardware element (NFC chip). A device may have multiple NFC adapters, for instance a built-in one, and one or more attached via USB.
An NFC tag is a passive NFC device. The NFC tag is powered by magnetic induction when an active NFC device is in proximity range. An NFC tag contains a single NDEF message.
The way of reading the message may happen through proprietary technologies, which require the reader and the tag to be of the same manufacturer. Implementations are expected to encapsulate this.
An NFC peer is an active, powered device, which can interact with other devices in order to exchange data using NFC.
An NFC device is either an NFC peer, or an NFC tag.
An NDEF message encapsulates one or more application-defined NDEF records. NDEF is an abbreviation for NFC Forum Data Exchange Format, a lightweight binary message format. NDEF messages can be stored on an NFC tag or exchanged between NFC-enabled devices.
The term NFC content is a synonym for NDEF message, which can originate either from an NFC tag or an NFC peer.
An NDEF record is a part of an NDEF message that has a single associated type information to its payload. It contains a Type Name Format (TNF) field, the payload size, the payload type, an optional identifier which is a URL, and a payload of maximum size of 2^32-1 bytes. The NFC Forum has standardized a small set of useful data types for use in NDEF records, for instance text, URL, and binary data such as media. In addition, there are record types designed for more complex interactions, such as Smart Poster, and handover records.
The TNF (Type Name Format) field of the NDEF record can take binary values denoting the following NDEF record payload types:
TNF value | NDEF record type |
---|---|
0 | Empty |
1 | NFC Forum Well-Known Type |
2 | Media Type |
3 | Absolute URI |
4 | NFC Forum External Type |
5 | Unknown |
6 | Unchanged |
7 | Reserved |
An NFC watch is a mechanism used to watch for available Web NFC messages fulfilling certain criteria. It is defined in more detail in The watch() method section.
A Web NFC message consists of a set of NDEF records, one of which is a Web NFC record.
The Web NFC message origin is an ASCII serialized origin
with "https"
scheme, stored in the Web NFC record.
For NFC content that is not a Web NFC message, it is
null
.
The Web NFC Id is a URL according to
[RFC3986], specifically the Web NFC message origin optionally
followed by a URL path, stored in the Web NFC Record.
This enables matching Web NFC content with URL pattern
s
specified by NFC watches.
A Web NFC record is an NDEF record of External Type, specific to Web NFC. It indicates that the containing NDEF message is targeted for browsing contexts using this API and contains information useful for handling the NDEF message with the algorithms defined in this specification. The format of a Web NFC record is as follows:
urn:nfc:ext:w3.org:webnfc
.
The term Web NFC content denotes all Web NFC messages contained within an NDEF message, identified by the Web NFC Id within the NDEF message. This version of the specification supports one Web NFC message per NDEF message.
As part of the NDEF record, an NDEF Id field may be present for application specific usages. According to the [NFC-STANDARDS] it contains a URL with the maximum length of 256 octets. This URL is used for identifying the NDEF record payload in an application specific way. This version of the specification does not use NDEF Id for NDEF record level payload identification, since NDEF message level identification is used.
An NFC handover defines NFC Forum Well Known Types and the corresponding message structure that allows negotiation and activation of an alternative communication carrier, such as Bluetooth or WiFi. The negotiated communication carrier would then be used (separately) to perform certain activities between the two devices, such as sending photos to the other device, printing to a Bluetooth printer or streaming video to a television set.
This section is non-normative.
In general, there are following groups of user scenarios for NFC:
NFC works using magnetic induction, meaning that the reader will emit a small electric charge which then creates a magnetic field. This field powers the passive device which turns it into electrical impulses to communicate data. Thus, when the devices are within range, a read is always performed (see NFC Analog Specification and NFC Digital Protocol, NFC Forum, 2006). The peer-to-peer connection works in a similar way, as the device periodically switches into a so-called initiator mode in order to scan for targets, then later to fall back into target mode. If a target is found, the data is read the same way as for tags.
As NFC is based on existing RFID standards, many NFC chipsets support reading RFIDs tags, but many of these are only supported by single vendors and not part of the NFC standards. Though certain devices support reading and writing to these, it is not a goal of this document to support proprietary tags or support interoperability with legacy systems.
The NFC Forum has mandated the support of four different tag types to be operable with NFC devices. The same is required on operating systems such as Android.
In addition to data types standardized for NDEF records by the NFC Forum, many commercial products such as bus cards, door openers etc use different card specific data and protocol extensions which require specific NFC chips (same vendor of card and reader) in order to function.
Card emulation mode capabilities also depend on the NFC chip in the device. For payments, a Secure Element is often needed.
This document does not aim supporting all possible use cases of NFC technology, but only a few use cases which are considered relevant to be used by web pages in browsers, using the browser security model.
This section is non-normative.
This section shows how developers can make use of the various features of this specification.
This section is non-normative.
A few Web NFC user scenarios are described in the Use Cases document. These user scenarios can be grouped by criteria based on security, privacy and feature categories, resulting in generic flows as follows.
The user opens a web page which can write an NFC tag. The write operations may be one of the following:
Note that an NFC write operation to an NFC tag always involves also a read operation.
In general, pushing data to another Web NFC capable device requires that
on the initiating device the user would first have to navigate to a web
site. The user would then touch the device against another Web NFC
equipped device, and data transfer would occur. On the receiving device
the UA will dispatch the content to an application registered
and eligible to handle the content, and if that application is a browser
which has a web page open and in focus that uses the Web NFC API and has
set up a NFC watch to listen to Web NFC content, then the
content is delivered to the web page through the parameters of an
NFCMessageCallback
.
NFC supports handover protocols to Bluetooth or WiFi connectivity for the purpose of larger volume data transfer. The user touches another NFC capable device, and as a result configuration data is sent for a new Bluetooth or WiFi connection, which is then established between the devices. This use case is not supported in this version of the specification.
The user buys goods in a store, and payments options include NFC. In general, touching the device to the point of sales terminal receiver area will result in a transaction between the secure element from the device and the point of sales terminal. With the Web NFC API, if the user navigates to a web site before paying, there may be interaction with that site regarding the payment, e.g. the user could get points and discounts, or get delivered application or service specific data (e.g. tickets, keys, etc) to the device. This use case is not supported in this version of the specification.
Users may attach one or more external NFC adapters to their devices, in addition to a built-in adapter. Users may use either NFC adapter.
This section is non-normative.
High level features for the Web NFC specification include the following:
This specification makes a few simplifications in what use cases and data types the Web NFC API can handle:
The trust model, attacker model, threat model and possible mitigation proposals for the Web NFC API are presented in the Security and Privacy document. This section presents the chosen security and privacy model through normative requirements to implementations.
Web pages using the Web NFC API are not trusted. This means that the user needs to be aware of exactly what a web page is intending to do with NFC at any given moment. Implementations need to make sure that when the user authorizes a method of this API, then only that action is run, without side effects, and exactly in the context and the number of times the user allows the execution of NFC related operations, according to the algorithmic steps detailed in this specification.
The integrity of NFC content SHOULD NOT be trusted when used for implementing security policies, for instance the authenticity of origins saved in the Web NFC Id, unless a prearranged trust relationship exists.
The main threats are summarized in the Security and Privacy document.
In this specification the following threats are handled with the highest priority:
This specification attempts to minimize user prompting and uses implicit security policies to address the threats. However, this specification does not describe, nor does it mandate specific user prompting policies. The term obtain permission is used for acquiring trust for a given operation.
The Permissions API is suggested to be used by UAs for implementing NFC related [permissions] in order to minimize the need for user prompting.
All expressed permissions that are preserved beyond the current browsing session MUST be revocable.
This section is non-normative.
This section summarizes the security policies which are specified as normative requirements in the respective algorithms of this specification:
Document
s with a browsing context, and an
incumbent settings object which is a secure context are
allowed to access NFC content.
Browsers may ignore this rule for development purposes only.
Window
object associated
with the Document
contained by the
top level browsing context using
the Web NFC API must be
visible and in
focus.
This also means that UAs should block access to the NFC radio if
the display is off or the device is locked.
For backgrounded web pages, receiving and pushing NFC content
must be suspended.
The content of any NDEF record is exposed by the
NFCRecord
dictionary:
enum NFCRecordType { "empty", "text", "url", "json", "opaque" }; typedef (DOMString
or unrestricted double orArrayBuffer
or Dictionary) NFCRecordData; dictionaryNFCRecord
{NFCRecordType
recordType
; USVStringmediaType
;NFCRecordData
data
; };
The NFCRecord.mediaType
property represents the IANA media type of the NDEF record
payload.
The NFCRecord.data
property represents the
payload data of the NDEF record with an appropriate ECMAScript
type, which depends on the IANA media type.
The NFCRecord.recordType
property represents the
NDEF record types.
The mapping from data types of an
to and
from NDEF record types is presented in the algorithmic steps which
handle the data and described in the
Receiving and parsing content
and Writing or pushing content sections.
NFCRecord
The content of any Web NFC message is exposed by the
NFCMessage
dictionary:
dictionary NFCMessage
{
sequence<NFCRecord
> data
;
USVString url
;
};
The NFCMessage.url
property represents the Web NFC Id of a received
Web NFC message. When used in the
method, it represents a URL path used for constructing the
Web NFC Id of the pushed Web NFC content.
NFC.push
()
The NFCMessage.data
property represents the NDEF message defining the
Web NFC message.
A JSON compatible IANA media type is defined by the following ABNF:
"application/"
("json"
/ <VCHAR except '/' and "*">"+json"
)
The mapping from data types of an
to
NDEF record types, as used in the Writing or pushing content
section is as follows:
NFCRecord
NFCRecord recordType | NFCRecord mediaType | NFCRecord data | NDEF record type |
---|---|---|---|
"empty" | not used | not used | NFC Forum Empty Type (TNF=0) |
"text" | not used | DOMString |
NFC Forum Well Known Type (TNF=1) with type Text |
"url" | not used | DOMString |
NFC Forum Well Known Type (TNF=1) with type URI |
"json" | JSON compatible IANA media type |
null or DOMString or Number or
Dictionary
|
Media Type as defined in [RFC2046] (TNF=2) with
IANA media type specified in the type attribute.
|
"opaque" | IANA media type | ArrayBuffer |
Media Type as defined in [RFC2046] (TNF=2) |
"opaque" | "" (empty) |
ArrayBuffer or typed array |
NFC Forum External Type (TNF=4) |
The mapping from NDEF record types to
,
as used for incoming NDEF messages described in the
Receiving and parsing content section, is as
follows:
NFCRecord
NDEF record type | NFCRecord recordType | NFCRecord mediaType | NFCRecord data |
---|---|---|---|
NFC Forum Empty Type (TNF=0) | "empty" | "" | null |
NFC Forum Well Known Type (TNF=1) with type Text | "text" | "text/plain" | DOMString |
NFC Forum Well Known Type (TNF=1) with type URI | "url" | "text/plain" | DOMString |
NFC Forum Well Known Type (TNF=1) with type Smart Poster | "url" | "text/plain" | DOMString |
Absolute URI as defined in [RFC3986] (TNF=3) | "url" | "text/plain" | DOMString |
Media Type as defined in [RFC2046] (TNF=2) with JSON compatible IANA media type | "json" | The IANA media type used in the NDEF record |
null or DOMString or Number or
Dictionary
|
Media Type as defined in [RFC2046] (TNF=2) | "opaque" | The IANA media type used in the NDEF record | ArrayBuffer |
NFC Forum External Type (TNF=4) with type other than
urn:nfc:ext:w3.org:webnfc* |
"opaque" | "application/octet-stream" | ArrayBuffer |
Any other NDEF record type | "opaque" | "application/octet-stream" | ArrayBuffer |
The Web NFC records MUST NOT be exposed to client browsing contexts.
NFC
object provides a way for the browsing context to
use NFC functionality.
It allows for pushing Web NFC messages to NFC tags
or NFC peers within range, and to set up and cancel NFC watches
to handle incoming Web NFC messages either from an NFC tag or an
NFC peer.
typedef (DOMString
orArrayBuffer
orNFCMessage
) NFCPushMessage; interfaceNFC
{ Promise<void>push
(NFCPushMessage
message, optionalNFCPushOptions
options); Promise<void>cancelPush
(optionalNFCPushTarget
target = "any"); Promise<long>watch
(MessageCallback
callback, optionalNFCWatchOptions
options); Promise<void>cancelWatch
(optional long id); }; callback MessageCallback = void (NFCMessage
message);
In later versions cancelPush()
and cancelWatch()
may be obsoleted by
cancelable Promises used with push()
and
watch()
, respectively.
The NFC
object is created with the internal slots
described in the following table:
Internal Slot | Description (non-normative) |
---|---|
[[suspended]] |
A boolean flag indicating whether NFC functionality is
suspended or not, initially
false .
|
[[watchList]] | A list of NFC watches initially set to empty list. |
NFC
object represents all NFC hardware devices that can read and
write NFC content.
Each Window
object connected to the
script execution environment has a separate NFC
instance. The
visibility and
focus state of the
Window
object determines the
suspended state of the associated
NFC
instance.
The term suspended in this specification
refers to NFC operations being suspended, i.e. no NFC content is
pushed, and no received NFC content is presented via watches while
suspended.
However, platform level timers for the
method continue running,
and if they expire, the event should be recorded and handled
when execution next resumes, i.e. when the NFC.push
()focus
event is fired on the Window
object.
When the Window
object associated with the
Document
using the Web NFC API
becomes visible and in
focus,
then run the following steps:
false
.
When the Window
object associated with the
Document
using the Web NFC API
loses focus,
then run the following steps:
true
.
If a user agent is to
make disappear
an
object nfc, run the following
NFC release steps, given nfc:
NFC
true
.
"tag"
as parameter.
"peer"
as parameter.
The UA should run the NFC release steps, given nfc, as additional steps to the unloading document cleanup steps.
enum NFCPushTarget { "tag", "peer", "any" }; dictionary NFCPushOptions {NFCPushTarget
target
= "any"; unrestricted doubletimeout
= Infinity; booleanignoreRead
= true; };
The NFCPushOptions.target
property
denotes the intended target for the pending push()
operation.
The NFCPushOptions.timeout
property
denotes the timeout for the pending push()
operation expressed in milliseconds. The default value is
implementation-dependent. The value Infinity
means there is
no timeout, i.e. no timer is started. After the timeout
expires, the message set for pushing is cleared, an error is returned,
and a new Web NFC message can be set for pushing.
When the value of the NFCPushOptions.ignoreRead
property is
true
, the push algorithm
will skip invoking the
receiving and parsing steps for an NFC tag.
To describe which messages an application is interested in, the
NFCWatchOptions
dictionary is used:
enum NFCWatchMode { "web-nfc-only", "any" }; dictionaryNFCWatchOptions
{ USVStringurl
= "";NFCRecordType
?recordType
; USVStringmediaType
= "";NFCWatchMode
mode
= "web-nfc-only"; };
The NFCWatchOptions.url
property
denotes the URL pattern
which is used for matching the
Web NFC Id of Web NFC messages which are being read.
The default value ""
means that no matching happens.
The NFCWatchOptions.recordType
property
denotes the string value which is used for matching the
type
property of each
object in a Web NFC message.
The default value NFCRecord
""
means that no matching happens.
The NFCWatchOptions.mediaType
property
denotes the match pattern
which is used for matching the
type
property of each
object in a Web NFC message.
The default value NFCRecord
""
means that no matching happens.
The NFCWatchOptions.mode
property denotes the
value telling whether only
Web NFC content or any NFC content will be watched.
The NFCWatchMode
"web-nfc-only"
value means that only those
NDEF messages are exposed which contain a
Web NFC record, i.e. which are meant for web pages.
The "any"
value means that all NDEF messages
are exposed.
var watchOptions = { url: "https://www.w3.org/*", // any path from the domain is accepted recordType: "json", mediaType: "application/*+json" // any JSON-based IANA media type }
var watchOptions = { url: "*://*/info/restaurant/daily-menu/", // accepted from any domain recordType: "opaque", mediaType: "application/octet-stream" }
This section describes how to write an NDEF message
to an NFC tag or how to push it to an NFC peer
device when it is next time in proximity range before a timer expires.
At any time there is at maximum of two
Web NFC messages that can be set for pushing for an origin:
one targeted to NFC tags and one to NFC peers, until
the current message is sent, or a timeout happens, or the push is
canceled by the
cancelPush()
method.
The
NFC.push
(message, options)
method, when invoked, MUST run the
push a message algorithm:
Promise
promise, and
then continue running this algorithm in parallel.
"SecurityError"
and abort these steps.
"NotSupportedError"
and abort these steps.
DOMString
or ArrayBuffer
, and it is not an
instance of NFCPushMessage
, reject promise
with "SyntaxError"
, and abort these steps.
NFCPushMessage
, and message.data is an
empty sequence, reject promise with
"SyntaxError"
and abort these steps.
"SyntaxError"
and abort these steps.
"any"
, run the following
steps twice, once with slot set to the value
"tag"
, and once set to the value "peer"
;
otherwise run the following step once, with
slot set to the value of target.
"AbortError"
.
In other words, the current invocation of push()
rejects and replaces existing running invocations handling the
same slot. At any given moment there may be
maximum two instances of this algorithm running: one targeting
NFC tags, and another targeting NFC peers.
Implementations are expected to clean up state on aborting these steps, e.g. stop the related timer, clear the related push message, as well as release any resources bound to NFC functionality, so that new invocations of this algorithm do not depend on previous invocations.
Infinity
,
start a timer timer with the timeout value set to
timeout.
"TimeoutError"
and abort these steps.
cancelPush()
method is called while timer is active with
target or "any"
, then reject
promise with "AbortError"
and
abort these steps, as described in the
cancelPush()
steps.
"tag"
or "any"
"peer"
or
"any"
false
.
true
, run the
receiving steps.
null
, or it is
different than the ASCII serialized origin of the
incumbent settings object, and the
obtain push permission steps return
false
, then reject promise with
"SecurityError"
and abort these steps.
"NetworkError"
and abort these
steps.
Multiple adapters should be used sequentially by users. There is very small likelihood that a simultaneous tap will happen on two or multiple different and connected NFC adapters. If it happens, the user will likely need to repeat the taps until success, preferably one device at a time. The error here gives an indication that the operation needs to be repeated. Otherwise the user may think the operation succeeded on all connected NFC adapters.
If this@[[suspended]] is true
,
continue waiting until timer expires (if set), or
until cancelPush()
is called, or until
an NFC device comes within communication range.
To obtain push permission, run these steps:
true
.
"granted"
(i.e. an expressed permission has
been granted to the origin and global object using
the
Permissions API), return true
.
"prompt"
, then optionally
request permission from the user for the
Web NFC permission name.
If that is granted, return true
.
The
request permission steps are not yet clearly defined.
At this point the UA asks the user about the policy to be used
with the Web NFC permission name for the given
origin and global object, store the result
in the
permission store, and on success return true
.
false
.
To create Web NFC message given a message run these steps:
NFCRecord
record in the sequence
message.data, run the following steps, or make sure
that the underlying platform provides equivalent values to
ndef:
undefined
, then:
ArrayBuffer
, then
set record.mediaType to
"application/octet-stream"
and
record.recordType to "opaque"
.
"object"
, then set
record.mediaType to
"application/json"
and
record.recordType to "json"
.
"number"
or "string"
, then
set record.mediaType to "plain/text"
and record.recordType to "text"
.
"SyntaxError"
and abort these steps.
"empty"
, then
Let ndef be the result of passing record
to map empty record to NDEF.
If this throws an exception, reject promise with
that exception and abort these steps.
"text"
,
then let ndef be the result of passing
record to map text to NDEF.
If this throws an exception, reject
promise with that exception and abort these steps.
"url"
,
then let ndef the be result of passing
record to map a URL to NDEF.
If this throws an exception, reject promise with that
exception and abort these steps.
"json"
,
then let ndef the be result of passing
record to map a JSON object to NDEF.
If this throws an exception, reject promise with that
exception and abort these steps.
"opaque"
,
then let ndef the be result of passing
record to map binary data to NDEF.
If this throws an exception, reject promise with that
exception and abort these steps.
Implementations may choose the location of the Web NFC record within the NDEF message.
To map empty record to NDEF given a record, run these steps:
To map text to NDEF given a record, run these steps:
This is useful when clients specifically want to write an
NDEF Well Known Type Text record. Other options would be to
use the value "opaque"
with an explicit
IANA media type text type, which allows for better
differentiation, e.g. when using "text/xml", or "text/vcard".
"SyntaxError"
exception and abort these steps.
"text/"
, then throw a "SyntaxError"
exception
and abort these steps.
In addition, UAs MAY check that
record.mediaType is an IANA registered media type
for
text.
If not, then the UA MAY throw a "SyntaxError"
exception and abort these steps.
"en"
.
If record.mediaType includes a lang=
parameter, detailing the language encoding (e.g.
"text/plain; lang=fr"), then set language to
the language code extracted from record.mediaType.
If language is not one of the language codes listed
in the
IANA language registry
(or [ISO-639.2]), then UAs MAY throw a "SyntaxError"
exception and abort these steps.
Note that lang=
is not standard parameter
to IANA media types, but it is used in this specification
in order to maintain compatibility with [NFC-STANDARDS].
"T"
(value 0x54 following NFC binary encoding).
To map a URL to NDEF given a record, run these steps:
"SyntaxError"
exception and abort these steps.
To map a JSON object to NDEF given a record, run these steps:
"TypeError"
exception
and abort these steps.
"application/json"
, or any other
IANA media type for JSON
(starting with "application/"
and ending with
"+json"
), throw a "SyntaxError"
exception and
abort these steps.
To map binary data to NDEF given a record, run these steps:
ArrayBuffer
, reject promise with
"SyntaxError"
and abort these steps.
"SyntaxError"
exception and abort these steps.
To create a Web NFC record given url, run these steps:
"urn:nfc:ext:w3.org:webnfc"
.
In future versions a list of "allowed-origins" may be supported. It is still up for discussions due to security issues. See the Security and Privacy document.
To create a Web NFC Id given url, run these steps:
"SyntaxError"
exception and abort these steps.
This means url should be a valid URL path.
The
NFC.cancelPush(target)
method, when invoked, MUST run cancel push algorithm:
Promise
promise and
then continue running this algorithm in parallel.
"SecurityError"
and abort these steps.
NFC.push
()
algorithm running with its options.target equal to
target or "any"
, then
"NoModificationAllowedError"
and abort these
steps.
"AbortError"
and abort the steps of the
instance.
The cancelPush()
method does not interrupt ongoing
transfers, it only cancels pushing before an NFC device
comes in range.
In order to receive NFC content, the client needs to call the
NFC.watch
() to opt into content of interest.
A match pattern
is defined by the following ABNF:
match-pattern =Atop-level type name
"/"
[ tree"."
]subtype name
["+"
suffix ] [";"
parameters ] top-level type name ="*"
/ <VCHAR except'/'
and"*"
> subtype name ="*"
/ <VCHAR except'+'
>
match pattern
is a
glob used for matching IANA media types,
for instance the pattern 'application/*+json'
matches
'application/calendar+json'
, but does not match
'application/json'
. The pattern
'*/*json'
, on the other hand, matches both.
A URL pattern
is a URL which may contain the '*'
wildcard character.
It consists of a scheme
, a
host
and (optionally) a
path
, defined by the following ABNF:
url-pattern =scheme
"://"
host
path
scheme ="*"
/"https"
host ="*"
/"*."
<VCHAR except"/"
and"*"
> path ="/"
<VCHAR>
Most characters match themselves, however '*'
(wildcard)
matches a sequence of visible characters. A URL pattern
that
doesn't conform to the above ABNF is a non-match.
Though '*'
matches a sequence of characters, only URLs
with scheme "https"
are accepted in the algorithms
taking a URL pattern
.
For example, '*://*.mydomain.com/*'
will match
'https://services.mydomain.com/myservice1/myapp2/'
and
'https://info.mydomain.com/general/'
.
This method enables listening to incoming NDEF messages.
The NFC content to which clients listen can be filtered based on
its data type, and based on the URL path of the
browsing context which has been saved to the Web NFC record
of the NFC content. The latter is matched against
URL pattern
s used in NFC watches.
An NFC watch is referring to a
filter saved together with the
NFCWatchOptions
instance it belongs
to, and a locally unique identifier which is used for cancellation.
The section
Receiving and parsing content uses NFC watches to match
incoming NFC content.
NFC
Multiple consecutive calls to the watch()
method from the
same origin create filters which are in OR relationship.
When the
NFC.watch(options, callback)
method is invoked, the UA MUST run the following
NFC watch algorithm:
Promise
object.
"SecurityError"
and abort these steps.
"NotSupportedError"
and
abort these steps.
false
, then reject promise with
"SecurityError"
and abort these steps.
"NotSupportedError"
and abort these steps.
To obtain watch permission, run these steps:
true
.
watch()
as well,
then return false
.
The ask for forgiveness interaction might show choices like "block now" or "block forever", etc. If the user has chosen to "block forever" the given origin, it is the responsibility of the UA to remember these user choices for each origin, regardless of which NFC adapter is used, and consult them on later invocations.
In this step UAs are advised to notify users about
that reading NFC content may indirectly reveal the physical
location of the user. In addition, if "any"
is used, then also include in this
information that the origin is requesting to read not only
NFC content meant for web pages, but any NFC content.
NFCWatchMode
true
.
When the NFC.cancelWatch(id)
method is
invoked, the UA MUST return a Promise
promise and run the following steps in parallel.
"SecurityError"
and abort these steps.
undefined
, then
remove all watches and filters set by successive calls of the
NFC watch()
method on all NFC adapters.
"NotFoundError"
and abort these steps.
If there are any NFC watches set up in NFC@[[watchList]], then UAs MUST listen to NDEF messages, according to step 7 of the NFC watch algorithm.
The UA SHOULD represent an unformatted NFC tag as an NDEF message containing a single empty NDEF record.
true
, abort
these steps.
NFCMessage
object, with
message.url set to null
and
message.data set to empty sequence.
NFCRecord
object.
"empty"
and set
record.mediaType to ""
.
"T"
(value 0x54 following NFC
binary encoding), then run the following sub-steps for
parsing NDEF Text record, or ensure that the
underlying platform provides equivalent values to the
record object properties:
"text"
."text/plain"
.
";lang="
and then the value of
language to record.mediaType.
"U"
(value 0x55 in NFC binary
encoding), then run the following sub-steps for
parsing NDEF URL record, or make sure that the
underlying platform provides equivalent values to the
record object properties:
"url"
."text/plain"
.
"url"
,
set record.mediaType to "text/plain"
and
set record.data to the string converted from
ndef.PAYLOAD.
match pattern
"application/*json"
, then
"json"
."opaque"
.ArrayBuffer
object constructed from the octets of
ndef.PAYLOAD.
urn:nfc:ext:w3.org:webnfc
,
then set message.url to the value decoded from
ndef.PAYLOAD in UTF-16.
"opaque"
."application/octet-stream
.
ArrayBuffer
object constructed from the octets of ndef.PAYLOAD.
false
and
message.data is not empty, run the
dispatch NFC content steps given message.
To dispatch NFC content given a message of type
run these steps:
NFCMessage
watch()
method in
this@[[watchList]], run the following sub-steps:
NFCWatchOptions
saved with watch, and let callback be the
registered callback function.
"web-nfc-only"
and
message.url is null, skip to the next
NFC watch.
"any"
,
set options.url to ""
.
URL pattern
options.url is not
""
and it does not match message.url,
skip to the next NFC watch.
""
and it is not
equal to any record.recordType where record is an
element of message, skip to the next NFC watch.
""
and it is not
equal to any record.mediaType where record is
an element of message, skip to the next NFC watch.
MessageCallback
var>callback
with message as its argument.
The following is a list of substantial changes to the document. For a complete list of changes, see the change log on Github. You can also view the recently closed bugs.
NFCRecordData
as explicit union.NFCMessage
as a dictionary.NFC
.The following problems are being discussed and need most attention:
This version addresses the issues above, and also other issues.The editors would like to thank Jeffrey Yasskin, Anne van Kesteren, Anssi Kostiainen, Alexander Shalamov, Domenic Denicola, Jonas Sicking, Don Coleman, Salvatore Iovene and Rijubrata Bhaumik for their contributions to this document. Special thanks goes to the former editors Luc Yriarte and Samuel Ortiz for their initial work on exposing NFC to the Web Platform, and for their supports for our new approach.