How NFC Work? Development on NFC

Ishrat khan
3 min readDec 15, 2019

--

NFC is a short-range wireless technologies, Required a distance of 4cm or less to make connection. NFC can share a small amount of data between NFC tag and Android powered devices.
In Android most of API store data in the NFC Tag based on standard called NDEF ( NFC Data Exchange Format )

Android devices support three main modes of operation:

Reader/Writer mode, to read and write passive NFC Tags
P2P mode, to exchange data between NFC peers,
Card emulation mode, to allow NFC enabled device to act as an NFC card.

NDEF communication:

There are two major use cases when working with NDEF
Reading NDEF from and NFC TAG handled by Tag Dispatch System
Beaming NDEF message rom on device to another with android Beam

NFC enabled android devices start scanning NFC tags when the screen is unlocked, When an NFC tag discovered by device, this opened most appropriate activity to handle intent without giving chooser activity, for this Android provides a special tag dispatch system.

“How Tag dispatcher system work?
Step 1 — Parse the NFC tag and figuring out the MIME type or URI that identifies the data payload in the tag.
Step 2 — Encapsulating the MIMe type or URI and payload into the intent.
Step 3 — Start an Activity based on the intent.

NDEF data is encapsulated inside a message NdefMessage that contain one or more NdefRecord.
NdefRecord contain following fields:
3-bit TNF (Type Name Format) — Indicate how to interpret the variable length type field.Valid values are described here.

Variable length type — Describes the type of the record. If using TNF_WELL_KNOWN, use this field to specify the Record Type Definition (RTD). Valid RTD values are described here.

Variable length ID
A unique identifier for the record. This field is not used often, but if you need to uniquely identify a tag, you can create an ID for it.

Variable length payload — The actual data payload that you want to read or write.

Programming

before moving to programming we must understand following diagram

The tag dispatch system defines these three intents

When Tag Dispatch System done above mentioned three steps, it send intent to the application with intent filter as described in diagram.

ACTION_NDEF_DISCOVERED — tag dispatch system try to find activity with this intent filter and open it

ACTION_TECH_DISCOVERED — If ACTION_NDEF_DISCOVERED not found , tag dispatch system try to find activity to handle this filter and open or for Non-NDEF format.

ACTION_TAG_DISCOVERED — This intent is started if no activities handle the ACTION_TECH_DISCOVERED or ACTION_NDEF_DISCOVERED intents.

Permission
<uses-permission android:name=”android.permission.NFC” />

Hardware feature
<uses-feature android:name=”android.hardware.nfc” android:required=”true” />

ACTION_NDEF_DISCOVERED Intent Filter to handle URI
<intent-filter>
<action android:name=”android.nfc.action.NDEF_DISCOVERED”/>
<category android:name=”android.intent.category.DEFAULT”/>
<data android:scheme=”http”
android:host=”developer.android.com”
android:pathPrefix=”/index.html” />
</intent-filter>

ACTION_TECH_DISCOVERED <project-root>/res/tech_list.xml
<resources xmlns:xliff=”urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.NfcB</tech>
<tech>android.nfc.tech.NfcF</tech>
<tech>android.nfc.tech.NfcV</tech>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>

<activity>
...
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>

<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
...
</activity
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
...
if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) {
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)?.also { rawMessages ->
val messages: List<NdefMessage> = rawMessages.map { it as NdefMessage }
// Process the messages array.
...
}
}
}

This is all about receiving NdefMEssage. Google mentioned a lot on the documentation page, just go and read all , click here

Thank you

--

--