Android NFC Basics – Understanding Near Field Communication in Android

Near Field Communication (NFC) is a short-range wireless communication technology that enables data exchange between compatible devices when they are brought within a few centimeters of each other (typically ≀4 cm).

NFC is widely used in contactless payments, data sharing, and smart tags, and Android has native support for it since version 2.3 (Gingerbread).


🧠 What is NFC?

NFC allows peer-to-peer communication between two devices β€” meaning, at any point, only one device can communicate with another.
It’s designed for quick, low-energy, short-distance data transfer β€” ideal for actions like pairing devices, reading NFC tags, or initiating another wireless connection such as Bluetooth or Wi-Fi.


βš™οΈ NFC Architecture in Android

Every NFC-enabled Android device consists of:

  1. NFC Hardware – The physical NFC chip and antenna.
  2. Device Driver – Connects hardware with the Android OS.
  3. Android NFC API Layer – Exposes high-level APIs to apps for reading, writing, and managing NFC data.

πŸ’‘ NFC Use Cases

Here are the most common real-world applications:

  1. Reading NFC Tags – Reading NDEF (NFC Data Exchange Format) messages stored in physical tags.
  2. Beaming NDEF Messages – Sending data between two NFC-enabled Android devices.
    • Example: Sharing contact info or URLs instantly.

πŸ“˜ NDEF Message Format:

  • NdefMessage β†’ Set of one or more NdefRecords.
  • NdefRecord contains the actual data payload, MIME type, and identifier.

🚫 NFC vs Bluetooth

FeatureNFCBluetooth
Range≀ 4 cmUp to 10 m
Data Rate424 Kbit/s2.1 Mbit/s
PairingNot RequiredRequired
Primary UseTap-and-Go / Trigger EventsContinuous Data Transfer

βœ… NFC is ideal for initiating connections (like Bluetooth pairing or Wi-Fi setup) quickly and securely.


🧩 Android Tag Dispatch System (TDS)

The Tag Dispatch System (TDS) is Android’s built-in mechanism that:

  • Detects NFC tags when scanned
  • Parses their content (NDEF format)
  • Determines MIME/URI type
  • Launches the appropriate Activity registered to handle that data

Steps in TDS Flow:

  1. Android receives the scanned tag data as an NdefMessage.
  2. The message contains one or more NdefRecords.
  3. The first record provides MIME or URI type information.
  4. The system dispatches an Intent to the registered Activity.

πŸͺͺ NFC Tag Technologies

NFC tags use different communication protocols defined by the TagTechnology interface in Android:

  • android.nfc.tech.IsoDep – Used in identification and smart cards
  • android.nfc.tech.Ndef – NDEF formatted tags
  • android.nfc.tech.NfcA, NfcB, NfcF, NfcV
  • NdefFormatable, MifareClassic, MifareUltralight

Each has its own Android implementation for reading/writing operations.


πŸ“² NFC Intent Actions

When a tag is scanned, Android dispatches one of the following intents:

IntentPriorityDescription
ACTION_NDEF_DISCOVERED1️⃣ HighestFired when tag contains NDEF data
ACTION_TECH_DISCOVERED2️⃣ MediumFired if tag isn’t NDEF but matches known technology
ACTION_TAG_DISCOVERED3️⃣ LowestGeneric fallback for any unhandled tag

Summary

  1. If NDEF is detected β†’ ACTION_NDEF_DISCOVERED
  2. If specific tech is detected β†’ ACTION_TECH_DISCOVERED
  3. If none matched β†’ ACTION_TAG_DISCOVERED

πŸš€ Foreground and Background Dispatch

To handle NFC while your app is active:

// Enable foreground dispatch
nfcAdapter.enableForegroundDispatch(
    this, pendingIntent, intentFilters, techList
);

// Disable when paused
nfcAdapter.disableForegroundDispatch(this);

This ensures your activity receives NFC intents only when it’s in the foreground.


πŸ”„ NFC Connection Handover Specification

Sometimes, NFC is used just to initiate a connection, and the actual data transfer happens over another medium (like Bluetooth or Wi-Fi).
This is known as NFC Connection Handover.

Example Use-Cases:

  1. Printing a photo from a mobile to an NFC-enabled printer.
  2. Syncing music from a phone to a home audio system.

Why Handover?

  • NFC range is very short (4 cm)
  • Transfer speed is low
  • Not suitable for large files

Hence, NFC only establishes the connection, and another carrier like Bluetooth or Wi-Fi takes over.


πŸ“˜ Key Terms in Handover

TermDescription
Alternate CarrierThe other wireless tech (e.g., Bluetooth, Wi-Fi)
Carrier Configuration DataConnection settings for the alternate carrier
Handover RequesterDevice initiating the connection
Handover SelectorDevice responding to the handover request
Negotiated HandoverDynamic exchange between two NFC devices via NDEF messages
Static HandoverPre-set carrier list stored in the NFC tag

🏁 Summary

  • NFC enables quick, contactless communication between nearby devices.
  • Android’s NFC APIs simplify reading/writing NDEF messages and handling tag interactions.
  • TDS manages which Activity receives the tag data.
  • Foreground dispatch ensures efficient handling when your app is active.
  • Connection Handover uses NFC to bootstrap Bluetooth or Wi-Fi connections for large transfers.

πŸ”— References

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top