适用于近场通信 API/NFC 的 NDEF 库
Easily parse and create NFC tags that contain standard-based NDEF messages.
Available in C# and JavaScript (for HTML5-based apps).
The library download comes with complete example apps that demonstrate reading and writing tags using the NDEF Library. Available for: .net Standard 1.4, Windows 10 / UWP. The Windows 8.1 / WinRT and Windows Phone 8 / Silverlight versions are deprecated and are no longer available in the latest packages.
NuGet Library Download | Platform Specific Extension Library | Windows 10 Example App Download
NFC tags as well as the content sent in device-to-device communication when tapping two phones is based on certain standards by the NFC Forum (called NDEF – NFC Data Exchange format). Luckily, these standards were well received and nearly all manufacturers are part of the standardization body. This ensures that public NFC tags can actually be read by all mobile phones today.
When it comes to storing data on NFC tags that can have as little writable storage as around 40 bytes, very efficient and complex data storage schemes are necessary. The downside is that most operating systems do integrate the NFC data transmission at the base level, but offer developers very little support for the NDEF standards on top. Obviously, reading those technical documents isn’t generally too much fun – to create an own implementation of a message that stores a simple URL on a tag, a developer would need to read and understand 59 pages of specifications.
As this is a lot work, there is the risk of people creating own solutions, leading to a fragmented NFC ecosystem
The open source NFC / NDEF Library contains a large set of classes that take care of formatting your data according to NDEF standards, so that these can be directly written to NFC tags or sent to other devices.
In your app, you choose the corresponding record type (e.g., for URLs, emails or geo tags) and provide the necessary data. The library creates an NDEF message out of the data, which you can directly send to the NFC stack in your operating system as a byte array, which takes care of writing it to a tag or publishing it to another device (using the SNEP protocol).
Additionally, the library can parse NDEF byte arrays that you read from tags or receive from other devices and create a list (NDEF Message) of data classes (NDEF records) that you can easily analyze and use in your app.
For Windows, the NFC stack is represented through the Proximity APIs - they encapsulate NFC hardware communication and basic NDEF formatting for a very limited subset of the NDEF standards. This missing part is added by this NDEF library.
The NFC / NDEF library is available in C# and JavaScript and can therefore be used on any operating system.
To keep up to date, either follow this project or follow me on Twitter.
The library is available as a ready-made portable class library, which can be used on any platform compatible to .net Standard 1.4+, like Windows 10 (UWP).
Additional platform-specific functionality is added through the the separate extension library. It integrates with the platform APIs for UWP and allows real-life tasks like creating a business card record based on a Contact from the Windows address book.
An example app is available for Windows 10. In addition to the library download on this page, you can use the NuGet package manager of Visual Studio to easily integrate the library with your project.
The JavaScript port of the library provides comprehensive NDEF support for modern web applications. It's built as ES modules and is compatible with the Web NFC API for Chrome on Android. The library includes support for common NDEF record types including social media records for modern platforms.
For C#, the library download comes with NdefDemoWin10 (for Windows 10 / UWP). This example app demonstrates some of the features of the NDEF Library. The demo is available under GPL v3 license. You can download the Windows 10 example app from the Windows Store.
For JavaScript, the NdefDemoJS directory contains a modern web application built with Vite that demonstrates the Web NFC API integration and various NDEF record types. The demo showcases social media records, URI records, and real-time NFC tag reading/writing. It requires Chrome for Android and works with physical NFC tags.
Another GPL-licensed example app for Windows Phone 8 is NfcShare, which is available together with accompanying webinar slides and a recording at the NFC developer's section at NfcInteractor.com.
Examples of apps currently using the NDEF Library and available in the public store:
…
…
Install the library using npm:
npm install ndef-library
The library is distributed as ES modules for modern JavaScript applications:
import { NdefMessage, NdefRecord, NdefUriRecord, NdefTextRecord, NdefSocialRecord, NfcSocialType } from 'ndef-library';
// Create NDEF Message
const ndefMessage = new NdefMessage();
// Create NDEF Uri Record
const ndefUriRecord = new NdefUriRecord("https://www.nfcinteractor.com");
// Add record to message
ndefMessage.push(ndefUriRecord);
// Get byte array for NFC tag
const byteArray = ndefMessage.toByteArray();
// Create social media records for modern platforms
const xRecord = new NdefSocialRecord("username", NfcSocialType.X); // X (formerly Twitter)
const linkedinRecord = new NdefSocialRecord("andreasjakl", NfcSocialType.LinkedIn);
const instagramRecord = new NdefSocialRecord("username", NfcSocialType.Instagram);
const threadsRecord = new NdefSocialRecord("username", NfcSocialType.Threads);
// Add to message
const socialMessage = new NdefMessage([xRecord, linkedinRecord]);
The library works seamlessly with the Web NFC API (Chrome for Android):
// Writing to NFC tag
const ndef = new NDEFReader();
await ndef.write(message);
// Reading from NFC tag
await ndef.scan();
ndef.onreading = event => {
const message = new NdefMessage(event.message.records.map(r =>
new NdefRecord(r.recordType, r.mediaType, r.data, r.id)));
console.log(message.records);
};
const recordType = new Array(1,3,1,3,5,6,7);
const recordPayload = new Array(1,2,1);
const id = new Array(3,3);
const ndefRecord2 = new NdefRecord(NdefRecord.TypeNameFormatType.NfcRtd, recordType);
ndefRecord2.setPayload(recordPayload);
ndefRecord2.setId(id);
const ndefMessage = new NdefMessage();
ndefMessage.push(ndefRecord2);
const byteArray = ndefMessage.toByteArray();
const ndefMessage = NdefMessage.fromByteArray(byteArray);
To try the library, you can clone the complete repository from GitHub and test the included NdefDemo example app.
If you want to use the NFC / NDEF Library from your own app, the easiest option is to use the NuGet package manager in Visual Studio to automatically download & integrate the portable library:
You can also download the complete portable library project from the source control server of this project, and build the library yourself, or directly integrate the relevant class files.
The JavaScript library is designed for modern web applications and supports ES modules.
暂无开放 Issues,或尚未同步最近议题。