Skip to content
/ NFC Public

Near Field Communication (NFC) is a technology that many of us use on a daily basis. Whether it is Apple Pay to buy your groceries, as a key card to enter your office building or to install your next smart home device. But how does this technology work? During the implementation, what are the challenges that developers typically encounter

Notifications You must be signed in to change notification settings

Ali-Cheikh/NFC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Understanding NFC Technology: A Developer's Guide

Near Field Communication (NFC) is a technology that many of us interact with daily—whether through Apple Pay for groceries, a key card for office access, or setting up a smart home device. But how does NFC work? What challenges do developers face during implementation, and how can they overcome them?


What is NFC?

NFC is a wireless communication technology that enables data exchange between devices in close proximity. It’s based on radio frequency identification (RFID) and uses a magnetic field to induce an electric current in nearby NFC-enabled devices.

NFC is widely adopted across industries, powering contactless payments, public transport systems, access control, and smart home automation. NFC-enabled devices include smartphones, tablets, payment cards, and other gadgets with built-in NFC chips.

Benefits of NFC

  • Ease of Use: Intuitive for users.
  • Speed: Near-instant data exchange.
  • Security: Short-range communication adds a layer of safety.
  • Versatility: Supports diverse applications across industries.

NFC Cards & Tags

A common use of NFC involves tags or cards (PICC: Proximity Inductive Coupling Card) interacting with a reader (PCD: Proximity Coupling Device). These tags are typically battery-free, powered by the electromagnetic field of the reader.

Types of NFC Tags

Tags differ in security and capabilities, depending on the application:

  • NXP NTAG-Series: Best for basic use cases.
  • MIFARE® Family: Ideal for secure access and payment.
  • Sony FeliCa™: Popular in Asia for transit and payment.

All these tags adhere to the ISO14443 standard, defined by the NFC Forum, which also provides the Data Exchange Format (NDEF) for common use cases like storing URLs.


NFC in Mobile Development

To integrate NFC into mobile apps, developers can use SDKs like:

  • Android NFC SDK
  • Apple Core NFC Framework

These SDKs support standards like NDEF, MIFARE®, and FeliCa™. On Android, Host-based Card Emulation (HCE) even allows smartphones to act as NFC cards. While iOS lacks this feature, developers can leverage the Wallet functionality for similar purposes.


Common Challenges and Solutions

UI Differences (iOS vs. Android)

Apple provides a standard NFC UI, but Android does not. To maintain consistent user experience, consider creating a custom UI on Android that mimics Apple’s flow and functionality.

Protection & Authentication

The level of security depends on the use case and the tag used. For sensitive data:

  • Read-only tags: Prevent unauthorized edits.
  • Password-protected tags: Require authentication for edits.
  • Advanced encryption (e.g., AES or DES): Used in tags like MIFARE® DESFire® EV2.

Avoid hardcoding keys in the app. Fetch keys from a backend and store them securely (e.g., iOS Keychain or Android Keystore).

sec

Communication Security

By default, commands between PICC and PCD are sent in plaintext, making them susceptible to interception. To enhance security:

  • Use encryption (e.g., AES) for data transmission.
  • Validate data integrity with methods like Cyclic Redundancy Check (CRC).

Data Handling

NFC data is often represented as byte arrays (hex or decimal). Here are some helpful functions for data conversion:

/**
 * Convert hex to decimal
 * @param hex - string
 * @returns number (0-255)
 */
export const hexToDec = (hex) => parseInt(hex, 16);

/**
 * Convert decimal to hex
 * @param value - number (0-255)
 * @returns hex - string
 */
export const decToHex = (value) => {
  const hex = value.toString(16).toUpperCase();
  return hex.length === 1 ? '0' + hex : hex;
};

//----------------------------------

export const hexToDec = (hex) => {
  if (typeof hex !== 'string' || !/^[0-9A-Fa-f]+$/.test(hex)) {
    throw new Error('Invalid hex string');
  }
  return parseInt(hex, 16);
};

export const decToHex = (value) => {
  if (typeof value !== 'number' || value < 0 || value > 255) {
    throw new Error('Invalid number: must be between 0 and 255');
  }
  const hex = value.toString(16).toUpperCase();
  return hex.length === 1 ? '0' + hex : hex;
};

//----------------------------------


(() => {
    const displayMessage = (message, isError = false) => {
        // Message logic here
    };

    const startReading = async () => {
        // NFC reading logic here
    };

    const writeToNFC = async () => {
        // NFC writing logic here
    };

    // Expose methods globally if necessary
    window.startReading = startReading;
    window.writeToNFC = writeToNFC;
})();

Additional Note

If NFC does not work as expected:

  • Test in a physical NFC-capable device, as most desktop browsers and devices do not have NFC hardware.
  • Ensure the site is served over HTTPS, as NFC functionality requires a secure context. You can test this by hosting your app on a platform like GitHub Pages or a local HTTPS server.

Let me know if you face any issues!

About

Near Field Communication (NFC) is a technology that many of us use on a daily basis. Whether it is Apple Pay to buy your groceries, as a key card to enter your office building or to install your next smart home device. But how does this technology work? During the implementation, what are the challenges that developers typically encounter

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published