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:
- NFC Hardware β The physical NFC chip and antenna.
- Device Driver β Connects hardware with the Android OS.
- 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:
- Reading NFC Tags β Reading NDEF (NFC Data Exchange Format) messages stored in physical tags.
- 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
| Feature | NFC | Bluetooth |
|---|---|---|
| Range | β€ 4 cm | Up to 10 m |
| Data Rate | 424 Kbit/s | 2.1 Mbit/s |
| Pairing | Not Required | Required |
| Primary Use | Tap-and-Go / Trigger Events | Continuous 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:
- Android receives the scanned tag data as an NdefMessage.
- The message contains one or more NdefRecords.
- The first record provides MIME or URI type information.
- 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 cardsandroid.nfc.tech.Ndefβ NDEF formatted tagsandroid.nfc.tech.NfcA,NfcB,NfcF,NfcVNdefFormatable,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:
| Intent | Priority | Description |
|---|---|---|
ACTION_NDEF_DISCOVERED | 1οΈβ£ Highest | Fired when tag contains NDEF data |
ACTION_TECH_DISCOVERED | 2οΈβ£ Medium | Fired if tag isnβt NDEF but matches known technology |
ACTION_TAG_DISCOVERED | 3οΈβ£ Lowest | Generic fallback for any unhandled tag |
Summary
- If NDEF is detected β
ACTION_NDEF_DISCOVERED - If specific tech is detected β
ACTION_TECH_DISCOVERED - 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:
- Printing a photo from a mobile to an NFC-enabled printer.
- 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
| Term | Description |
|---|---|
| Alternate Carrier | The other wireless tech (e.g., Bluetooth, Wi-Fi) |
| Carrier Configuration Data | Connection settings for the alternate carrier |
| Handover Requester | Device initiating the connection |
| Handover Selector | Device responding to the handover request |
| Negotiated Handover | Dynamic exchange between two NFC devices via NDEF messages |
| Static Handover | Pre-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.
