Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

phonegap-nfc

> 编程语言
Open source

PhoneGap NFC Plugin

738 stars0 likes0 views
WebsiteGitHub

About

PhoneGap NFC Plugin

PhoneGap NFC Plugin

The NFC plugin allows you to read and write NFC tags. You can also beam to, and receive from, other NFC enabled devices.

Use to

  • read data from NFC tags
  • write data to NFC tags
  • send data to other NFC enabled devices
  • receive data from NFC devices
  • send raw commands (ISO 14443-3A, ISO 14443-3A, ISO 14443-4, JIS 6319-4, ISO 15693) to NFC tags

This plugin uses NDEF (NFC Data Exchange Format) for maximum compatibilty between NFC devices, tag types, and operating systems.

Supported Platforms

  • Android
  • iOS 11
  • Windows (includes Windows Phone 8.1, Windows 8.1, Windows 10)
  • BlackBerry 10
  • Windows Phone 8
  • BlackBerry 7

Contents

  • Installing
  • NFC
  • NDEF
    • NdefMessage
    • NdefRecord
  • Events
  • Platform Differences
  • BlackBerry 10 Invoke Target
  • Launching Application when Scanning a Tag
  • Testing
  • Sample Projects
  • Host Card Emulation (HCE)
  • Book
  • License

Installing

Cordova

$ cordova plugin add phonegap-nfc

PhoneGap

$ phonegap plugin add phonegap-nfc

PhoneGap Build

Edit config.xml to install the plugin for PhoneGap Build.

Windows Phone 8.1 should use the windows platform. The Silverlight based Windows Phone 8 code is no longer being maintained.

BlackBerry 7 support is only available for Cordova 2.x. For applications targeting BlackBerry 7, you may need to use an older version of phonegap-nfc.

See Getting Started and Getting Started BlackBerry 10for more details.

iOS Notes

Reading NFC NDEF tags is supported on iPhone 7 (and newer) since iOS 11. iOS 13 added support for writing NDEF messages to NFC tags. iOS 13 also adds the ability to get the UID from some NFC tags. On iOS, the user must start a NFC session to scan for a tag. This is different from Android which can constantly scan for NFC tags. The nfc.scanNdef and nfc.scanTag functions start a NFC scanning session. The NFC tag is returned to the caller via a Promise. If your existing code uses the deprecated nfc.beginSession, update it to use nfc.scanNdef.

The scanNdef function uses NFCNDEFReaderSession to detect NFC Data Exchange Format (NDEF) tags. scanTag uses the newer NFCTagReaderSession available in iOS 13 to detect ISO15693, FeliCa, and MIFARE tags. The scanTag function will include the tag UID and tag type for some NFC tags along with the NDEF messages. scanTag can also read some RFID tags without NDEF messsages. scanTag will not scan some NDEF tags including Topaz and Mifare Classic.

You must call nfc.scanNdef and nfc.scanTag before every scan.

Writing NFC tags on iOS uses the same nfc.write function as other platforms. Although it's the same function, the behavior is different on iOS. Calling nfc.write on an iOS device will start a new scanning session and write data to the scanned tag.

NFC

The nfc object provides access to the device's NFC sensor.

Methods

  • nfc.addNdefListener
  • nfc.addTagDiscoveredListener
  • nfc.addMimeTypeListener
  • nfc.addNdefFormatableListener
  • nfc.write
  • nfc.makeReadOnly
  • nfc.share
  • nfc.unshare
  • nfc.erase
  • nfc.handover
  • nfc.stopHandover
  • nfc.enabled
  • nfc.showSettings
  • nfc.beginSession
  • nfc.invalidateSession
  • nfc.scanNdef
  • nfc.scanTag
  • nfc.cancelScan

ReaderMode

  • nfc.readerMode
  • nfc.disableReaderMode

Tag Technology Functions

  • nfc.connect
  • nfc.transceive
  • nfc.close
  • ISO-DEP example

nfc.addNdefListener

Registers an event listener for any NDEF tag.

nfc.addNdefListener(callback, [onSuccess], [onFailure]);

Parameters

  • callback: The callback that is called when an NDEF tag is read.
  • onSuccess: (Optional) The callback that is called when the listener is added.
  • onFailure: (Optional) The callback that is called if there was an error.

Description

Function nfc.addNdefListener registers the callback for ndef events.

A ndef event is fired when a NDEF tag is read.

For BlackBerry 10, you must configure the type of tags your application will read with an invoke-target in config.xml.

On Android registered mimeTypeListeners takes precedence over this more generic NDEF listener.

On iOS you must call beingSession before scanning a tag.

Supported Platforms

  • Android
  • iOS
  • Windows
  • BlackBerry 7
  • BlackBerry 10
  • Windows Phone 8

nfc.removeNdefListener

Removes the previously registered event listener for NDEF tags added via nfc.addNdefListener.

nfc.removeNdefListener(callback, [onSuccess], [onFailure]);

Removing listeners is not recommended. Instead, consider that your callback can ignore messages you no longer need.

Parameters

  • callback: The previously registered callback.
  • onSuccess: (Optional) The callback that is called when the listener is successfully removed.
  • onFailure: (Optional) The callback that is called if there was an error during removal.

Supported Platforms

  • Android
  • iOS
  • Windows
  • BlackBerry 7

nfc.addTagDiscoveredListener

Registers an event listener for tags matching any tag type.

nfc.addTagDiscoveredListener(callback, [onSuccess], [onFailure]);

Parameters

  • callback: The callback that is called when a tag is detected.
  • onSuccess: (Optional) The callback that is called when the listener is added.
  • onFailure: (Optional) The callback that is called if there was an error.

Description

Function nfc.addTagDiscoveredListener registers the callback for tag events.

This event occurs when any tag is detected by the phone.

Supported Platforms

  • Android
  • Windows
  • BlackBerry 7

Note that Windows Phones need the newere NXP PN427 chipset to read non-NDEF tags. That tag will be read, but no tag meta-data is available.

nfc.removeTagDiscoveredListener

Removes the previously registered event listener added via nfc.addTagDiscoveredListener.

nfc.removeTagDiscoveredListener(callback, [onSuccess], [onFailure]);

Removing listeners is not recommended. Instead, consider that your callback can ignore messages you no longer need.

Parameters

  • callback: The previously registered callback.
  • onSuccess: (Optional) The callback that is called when the listener is successfully removed.
  • onFailure: (Optional) The callback that is called if there was an error during removal.

Supported Platforms

  • Android
  • Windows
  • BlackBerry 7

nfc.addMimeTypeListener

Registers an event listener for NDEF tags matching a specified MIME type.

nfc.addMimeTypeListener(mimeType, callback, [onSuccess], [onFailure]);

Parameters

  • mimeType: The MIME type to filter for messages.
  • callback: The callback that is called when an NDEF tag matching the MIME type is read.
  • onSuccess: (Optional) The callback that is called when the listener is added.
  • onFailure: (Optional) The callback that is called if there was an error.

Description

Function nfc.addMimeTypeListener registers the callback for ndef-mime events.

A ndef-mime event occurs when a Ndef.TNF_MIME_MEDIA tag is read and matches the specified MIME type.

This function can be called multiple times to register different MIME types. You should use the same handler for all MIME messages.

nfc.addMimeTypeListener("text/json", *onNfc*, success, failure);
nfc.addMimeTypeListener("text/demo", *onNfc*, success, failure);

On Android, MIME types for filtering should always be lower case. (See IntentFilter.addDataType())

Supported Platforms

  • Android
  • BlackBerry 7

nfc.removeMimeTypeListener

Removes the previously registered event listener added via nfc.addMimeTypeListener.

nfc.removeMimeTypeListener(mimeType, callback, [onSuccess], [onFailure]);

Removing listeners is not recommended. Instead, consider that your callback can ignore messages you no longer need.

Parameters

  • mimeType: The MIME type to filter for messages.
  • callback: The previously registered callback.
  • onSuccess: (Optional) The callback that is called when the listener is successfully removed.
  • onFailure: (Optional) The callback that is called if there was an error during removal.

Supported Platforms

  • Android
  • BlackBerry 7

nfc.addNdefFormatableListener

Registers an event listener for formatable NDEF tags.

nfc.addNdefFormatableListener(callback, [onSuccess], [onFailure]);

Parameters

  • callback: The callback that is called when NDEF formatable tag is read.
  • onSuccess: (Optional) The callback that is called when the listener is added.
  • onFailure: (Optional) The callback that is called if there was an error.

Description

Function nfc.addNdefFormatableListener registers the callback for ndef-formatable events.

A ndef-formatable event occurs when a tag is read that can be NDEF formatted. This is not fired for tags that are already formatted as NDEF. The ndef-formatable event will not contain an NdefMessage.

Supported Platforms

  • Android

nfc.write

Writes an NDEF Message to a NFC tag.

A NDEF Message is an array of one or more NDEF Records

var message = [
    ndef.textRecord("hello, world"),
    ndef.uriRecord("http://github.com/chariotsolutions/phonegap-nfc")
];

nfc.write(message, [onSuccess], [onFailure]);

Parameters

  • ndefMessage: An array of NDEF Records.
  • onSuccess: (Optional) The callback that is called when the tag is written.
  • onFailure: (Optional) The callback that is called if there was an error.

Description

Function nfc.write writes an NdefMessage to a NFC tag.

On Android this method must be called from within an NDEF Event Handler.

On iOS this method can be called outside the NDEF Event Handler, it will start a new scanning session. Optionally you can reuse the read session to write data. See example below.

On Windows this method may be called from within the NDEF Event Handler.

On Windows Phone 8.1 this method should be called outside the NDEF Event Handler, otherwise Windows tries to read the tag contents as you are writing to the tag.

Examples

Android

On Android, write must be called inside an event handler

function onNfc(nfcEvent) {

    console.log(nfcEvent.tag);
    
    var message = [
        ndef.textRecord(new String(new Date()))
    ];
    
    nfc.write(
        message,
        success => console.log('wrote data to tag'),
        error => console.log(error)
    );

nfc.addNdefListener(onNfc);

iOS - Simple

Calling nfc.write on iOS will create a new session and write data when the user taps a NF

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Java

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言