How AI, OpenAI, and ChatGPT impact business and software.
NFC Bootcamp Seattle Day 2
1. How to Build an
NFC
Application
Richard Grundy
@grundyoso
http://flomio.com
How to Build an NFC Application 1
2. Agenda
• Anatomy of an NFC App
• NFC Interface
o Technologies
• Learn the specs
o Tags (Demo 1)
• Learn the types
o NDEC (Demo 2 and 3)
• Learn to format/read/write
o Infrastructure
• There’s a stack for that
o Application
• Give it logic
How to Build an NFC Application 2
3. Anatomy
Web Service Web Service Web Service
Middleware
Reader Reader Reader Reader Reader
NFC App
Tag Tag Tag Tag Tag Tag Tag
Tag Tag Tag Tag Tag Tag Tag
Passive Active
How to Build an NFC Application 3
4. Anatomy
Middleware
NFC Interface
How to Build an NFC Application 4
6. Interface
13.56Mhz
1 wavelength = 7.37e-8secs of
22.1m
Theoretical range = 3.5m
How to Build an NFC Application 6
7. Interface
• NFC International Specifications*
NDEF
TAGS
TECH
*http://www.flomio.com/docs/ > NFC International Standards
How to Build an NFC Application 7
8. Technology
• ISO 14443-3A: NFC-A
• ISO 14443-3B: NFC-B
• JIS 6319-4: NFC-F
• ISO 15693: NFC-V
• Terms
o ASK
o PCD
o PICC
o PDU
o REQ
o ATQ
o UID/UUID
o RFU
• APDU
o CLA, INS, P1, P2, L(c), L(e)
o SW1, SW2, L(e)
• ATR = Answer to Reset
o MFR
• PTS = Protocol Type
Selection
o T=0 Byte level tx
o T=1 Block level tx
How to Build an NFC Application 8
9. Tags
• NFC Forum: Types 1
o Jewel Topaz
• NFC Forum: Types 2
o Mifare Ultralight S,C
• NFC Forum: Types 3
o Felicia
• NFC Forum: Types 4
o Mifare DESfire S, EV1
o Mifare Classic 1k, 4k
o Mifare Plus 1k, 4k
Options:
• ISO 15693 Media
o I-Code SLI Ferrite/Metal Mount
o Tag IT
• Proprietary: Many more
How to Build an NFC Application 9
10. Demo
1. Requirement
1. Android 4.0 NFC-enabled handset
2. Download from Google Play:
1. NFC TagInfo by Michael Roland
3. Enable NFC (phone boots with NFC off):
1. Menu System Settings Wireless & Networks More
2. Check NFC box
4. Scan some tags and study ―Tag Information …‖
1. UID(x)
2. RF Technology
3. Tag Type
4. Manufacturer
5. Target technology classes (Android)
How to Build an NFC Application 10
11. NDEF
• Mifare Memory Structure
o Sectors
o Blocks
o Keys
o Access Bits
How to Build an NFC Application 11
12. NDEF
• Get ATR – Is MiFare 1k
• Set Reader Read Key (FF FF FF
FF FF)
• Authenticate Reader to Tag
Sector 0
o Is NFC Initialized? How do you
know?
• Set write key (FF FF FF FF FF)
• Write MAD
• Write Keys and Access Bits
• Write first empty record
How to Build an NFC Application 12
13. NFC Forum Defined Not NFC Forum Defined
• Text • Payment • Mime Types
o urn:nfc:wkt:T
• URI • Coupon Vendor
o urn:nfc:wkt:U • Start a Car
Application
• Smart Poster • Etc. Etc.
o urn:nfc:wkt:Sp
• Signature
o urn:nfc:wkt:Sig
• Mime Types
• Handover o Vendor
o urn:nfc:wkt:HO o Application
• … o Etc.
How to Build an NFC Application 13
14. Demo
1. Requirement
1. Android 4.0 NFC-enabled handset
2. Download from Google Play:
1. NXP TagWriter Lite Edition
3. Enable NFC (phone boots with NFC off):
1. Menu System Settings Wireless & Networks More
2. Check NFC box
4. Create and Write some tags with NDEF Records
1. Contact — Add a vcard: Richard @ 305-982-7424
2. URL — Enter a web address: http://www.flomio.com
3. Plain Text — Add this text ―Hello World!‖
4. SMS — Prepare message: 305-982-7424 — ―Sup Flomio!‖
5. Now TAP-TO-X!
How to Build an NFC Application 14
16. General Credit Card (CC) Approval/Payment Process using NFC
How to Build an NFC Application 16
17. Application
• Android offers comprehensive NFC support:
o 2.3.2 API 10 Gingerbread
o 2.3.3
o 2.3.4
o 4.0 Ice Cream Sandwitch
• Beam
o Available
• Tag Read/Write
• Tag Emulate
• P2P with another Android
o Not Available
• Smart Card Emulation
• P2P Generalized
How to Build an NFC Application 17
20. Android Setup
• Register a detailed intent filter in the app’s
AndroidManifest.xml
• Get Adapter Handle and Set Listen Filters
mAdapter = NfcAdapter.getDefaultAdapter(this);
mOutPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
// ndef.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
mFilters = new IntentFilter[] { ndef, };
mTechLists = new String[][] {new String[] { android.nfc.tech.NfcV.class.getName() },
new String[] { android.nfc.tech.IsoDep.class.getName()
}, new String[] { NfcF.class.getName() } };
How to Build an NFC Application 20
21. Android Intents
public void onResume() {
super.onResume();
if (isSending) {
if (mAdapter != null) Send P2P NDEF Message
mAdapter.enableForegroundNdefPush(this, mMessage);
} else {
mAdapter.enableForegroundDispatch(this,
mOutPendingIntent,
mFilters, mTechLists);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent()
.getAction())) {
NDEF Received
processIntent(getIntent());
} else if
(NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent()
.getAction())); {
processTagIntent(getIntent());
} Tag no NDEF
}
}
How to Build an NFC Application 21
22. Android Process Info
/** * Parses the NDEF Message from the intent and prints to the TextView */
void processIntent(Intent intent) {
mText = (TextView) findViewById(R.id.textInfo);
// get the data
Parcelable[] rawMsgs = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0];
// record 0 contains the MIME type, record 1 is the AAR, if present
mText.setText(new String(msg.getRecords()[0].getPayload()));
}
/** Show tag found **/
void processTagIntent(Intent intent) {
mText = (TextView) findViewById(R.id.textInfo);
// get the tag handle and data
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
// tell me about the tag found
mText.setText(new String("Tag discovered:" + tag.toString()));
} else {
// lost tag or not finished
Log.i(APPTAG, "Tag not ready or no longer there");
};
}
How to Build an NFC Application 22
23. Android Make NDEF
public static NdefRecord newTextRecord(String text, Locale
locale,boolean encodeInUtf8) {
byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
byte[] textBytes = text.getBytes(utfEncoding);
int utfBit = encodeInUtf8 ? 0 : (1 << 7);
char status = (char) (utfBit + langBytes.length);
byte[] data = new byte[1 + langBytes.length + textBytes.length];
data[0] = (byte) status;
System.arraycopy(langBytes, 0, data, 1, langBytes.length);
System.arraycopy(textBytes, 0, data, 1 + langBytes.length,textBytes.length);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
}
String mTextPayload = "JOHN A. DOE,YBC99999999" + mSequence;
mMessage = new NdefMessage(new NdefRecord[] {
newTextRecord(mTextPayload, Locale.ENGLISH, true) });
How to Build an NFC Application 23
25. How to – Construct NDEF
Header Location
7
Abbrev
MB
Name
Message Begin
FALSE
0
TRUE
1
6ME Message End 0 1
5CF Chunk Flag 0 1
4SR Short Record 0 1
3IL ID Length 0 1
2 0 1
1 TNF Type Name Format 0 1
0 0 1
d1 1 1 0 1 0 0 0 1 c9 1 1 0 0 1 0 0 1
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
MB ME CF SR IL TNF MB ME CF SR IL TNF
Type Length Type Length
Payload Length Payload Length 3
Payload Length 2
Type
Payload Length 1
ID
Payload Length 0
Payload
ID Length
Type
ID
How to Build an NFC Application Payload 25
26. Type Name Format TNF
Well Known Record Types
urn:nfc:wkt:T T 0x54
Type Name Format (TNF) urn:nfc:wkt:U U 0x55
Empty 0x00
Well Known Type 0x01
urn:nfc:wkt:Sp Sp 0x53 0x70
Media 0x02 urn:nfc:wkt:Sig Sig 0x53 0x69 0x67
Absolute URI 0x03
External 0x04
Unk 0x05 Language
Unchanged 0x06 en 0x65 0x6E
Reserved 0x07 Urn:nfc:mime/vnd.m360.id fr 0x66 0x72
sp 0x73 0x70
de 0x64 0x65
urn:nfc:ext:M360:id
Uri Identifier Codes
None 0x00
http://www. 0x01
https://www. 0x02
http:// 0x03
https:// 0x04
tel: 0x05
mailto: 0x06
How to Build an NFC Application 26
27. Smart Poster NDEF Message
0 &HD1 209 0xD1 record header 12 17 &H77 119 0x77 w 29 34 &H54 84 0x54 T
1 &H2 2 0x02 type length 13 18 &H77 119 0x77 w 30 35 &H2 2 0x02 language lnght
2 &H28 40 0x28 length 14 19 &H77 119 0x77 w 31 36 &H65 101 0x65 e
3 &H53 83 0x53 S 15 20 &H2E 46 0x2E . 32 37 &H6E 110 0x6E n
4 &H70 112 0x70 p 16 21 &H6D 109 0x6D m 33 38 &H4D 77 0x4D M
0 5 &HD1 209 0xD1 record header 17 22 &H6F 111 0x6F o 34 39 &H6F 111 0x6F o
1 6 &H1 1 0x01 type length 18 23 &H62 98 0x62 b 35 40 &H62 98 0x62 b
2 7 &H16 22 0x16 length 19 24 &H69 105 0x69 i 36 41 &H69 105 0x69 i
3 8 &H55 85 0x55 U 20 25 &H74 116 0x74 t 37 42 &H74 116 0x74 t
4 9 &H0 0 0x00 prefix 21 26 &H61 97 0x61 a 38 43 &H61 97 0x61 a
5 10 &H68 104 0x68 h 22 27 &H6F 111 0x70 p 39 44 &H70 112 0x70 p
6 11 &H74 116 0x74 t 23 28 &H2E 46 0x2E . 40 45 &HFE 254 0xFE end
7 12 &H74 116 0x74 t 24 29 &H6D 109 0x6D m
8 13 &H70 112 0x70 p 25 30 &H65 101 0x65 e
9 14 &H3A 58 0x3A : 26 31 &HD1 209 0xD1 record header
10 15 &H2F 47 0x2F / 27 32 &H1 1 0x01 type length
11 16 &H2F 47 0x2F / 28 33 &HA 10 0x0A length
How to Build an NFC Application 27