SlideShare a Scribd company logo
NFC
about
Sven Haiges, hybris GmbH
  Twitter @hansamann

    Android, HTML5,
    Groovy & Grails


 sven.haiges@hybris.de
about
Sven Haiges, hybris GmbH
  Twitter @hansamann

    Android, HTML5,
    Groovy & Grails


 sven.haiges@hybris.de

                           NFC
Basics                    Card
                            Emulation
                     Reader/Writer
                            P2P



                   NFC
 TagTechnologies
Tag NDEF
Dispatch                 APIs
                         Android
Basics
NFC roots: RFID

• RFID - Radio Frequency Identification
• 1983: First RFID patent granted
• Typical, well-known use-cases for RFID
  animal identification, road taxes, libraries


• 2004: Nokia, Philips and Sony established
  the NFC Forum
NFC

• “transfer data at low speed, without
  security, over short distances”
• ~10 centimeters, in reality less
• 424 kBit/s
• 13.56 MHz
NFC Devices
• First mainstream Android
  smartphone: Nexus S
• Common in new top
  Android phones
• New RIM / Nokia devices
• Apple iPhone?
Nokia 6131

   • released 2006
   • early 2007: iPhone!
   • J2ME Spec: JSR 257
     Contactless Communication API


   • NFC is not new!
Forecast
                        NFC handsets (k units)
600000

450000

300000

150000

     0
      2010   2011(e)    2012(e)        2013(e)               2014(e)                     2015(e)




                                                 Source:IHS iSuppli,compiled by Digitimes,May 2011
Payment
Tickets
      Coupons
           Smart Poster



Payment
Tickets
       Coupons
            Smart Poster



Payment

          Identity
     Keycard
               Personal ID
NFC Checkin
                             Tickets
   Social                 Coupons
Gaming     vCard               Smart Poster



                   Payment

                             Identity
                        Keycard
                                  Personal ID
NFC Checkin
                              Tickets
    Social                 Coupons
 Gaming     vCard               Smart Poster



                    Payment
      WiFi
Connections
     Bluetooth                Identity
                         Keycard
                                   Personal ID
Transponder
     Format          Technology/Capacity


                      13.56 Mhz
 Wristbands                  125 Khz
      Cards Labels
Sticker Keyfobs         NFC Forum
      Tags               Type 1-4
can be
                                   made read
                                     only!
NFC Forum Type 2 Tag
  Mifare Ultralight - 64 bytes
 Mifare Ultralight C - 192 bytes
NFC on Android - Near Field Communication
Mifare Ultralight
All Surface Tag
Mifare Ultralight
All Surface Tag

                    Mifare Ultralight
                    Paper Ticket
Mifare Ultralight
All Surface Tag

                      Mifare Ultralight
                      Paper Ticket




  Mifare Ultralight
     Square Label
Mifare Ultralight
                 All Surface Tag

                                       Mifare Ultralight
                                       Paper Ticket




Mifare Classic
                   Mifare Ultralight
Keyfob
                      Square Label
Shops

• tagage.net
• rfidshop.com
• smartcardfocus.com
• and many more...
Comparing NFC




         Source:NFC Forum
NFC Modes
NFC Modes

• Peer-to-Peer Mode
• Reader/Writer Mode
• Card Emulation Mode
Android NFC Support

• Peer-to-Peer Mode: limited
• Reader/Writer Mode: excellent support
• Card Emulation Mode: no support
NFC & Android
NFC & Android

• Tag-Reading ab Android 2.3, API Level 9
• Tag-Writing ab Android 2.3.3, API Level 10
• Core classes: NFCAdapter, NFCManager,
  Tag Technology Classes
Android Manifest

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



• API Level
  <uses-sdk android:minSdkVersion="10" />


• NFC Feature
  <uses-feature android:name="android.hardware.nfc"
  android:required="true" />
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.flavor.android.nfc"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
	   	    <activity android:name=".MimeActivity" android:label="MIME">
	           <intent-filter>
	   	    	      <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
	   	    	      <data android:mimeType="application/json" />
	   	    	      <category android:name="android.intent.category.DEFAULT" />
	   	       </intent-filter>	
        </activity>
    </application>
    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:minSdkVersion="10" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
</manifest>
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.flavor.android.nfc"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
	   	    <activity android:name=".MimeActivity" android:label="MIME">
	           <intent-filter>
	   	    	      <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
	   	    	      <data android:mimeType="application/json" />
	   	    	      <category android:name="android.intent.category.DEFAULT" />
	   	       </intent-filter>	
        </activity>
    </application>
    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:minSdkVersion="10" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
</manifest>
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.flavor.android.nfc"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
	   	    <activity android:name=".MimeActivity" android:label="MIME">
	           <intent-filter>
	   	    	      <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
	   	    	      <data android:mimeType="application/json" />
	   	    	      <category android:name="android.intent.category.DEFAULT" />
	   	       </intent-filter>	
        </activity>
    </application>
    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:minSdkVersion="10" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
</manifest>
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.flavor.android.nfc"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
	   	    <activity android:name=".MimeActivity" android:label="MIME">
	           <intent-filter>
	   	    	      <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
	   	    	      <data android:mimeType="application/json" />
	   	    	      <category android:name="android.intent.category.DEFAULT" />
	   	       </intent-filter>	
        </activity>
    </application>
    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:minSdkVersion="10" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
</manifest>
NFCManager

• NFCManager used to get the NFCAdapter
• Boring
• Shortcut for
  getSystemService(NFC_SERVICE)
NFCAdapter

• the real thing
• Control Foreground Dispatch and
  P2P NDEF Push
• check if NFC is turned on
Checking for NFC
if (!getNFCAdapter().isEnabled())
{
	   Toast.makeText(getApplicationContext(),
	   "Please activate NFC and press back to return to the application!",
	   Toast.LENGTH_LONG).show();
	
	   startActivity(
       new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)
    );
}
Reading a Tag

• Unlock phone,
  touch tag :-)
• Most likely, the
  intent chooser will
  open
Foreground Dispatch

• Foreground Activity may redirect all intents
  related to tag, technology or NDEF
  discovery to itself
• Getting all tags while activity is visible:
  enable in onResume(), disable in onPause()
Foreground Dispatch
nfcAdapter.enableForegroundDispatch(                 techListsArray does not matter
   activity,                                         here, as ACTION_TECH_DISCOVERED
                                                     is not in intentFilters array. It can
   pendingIntent,                                    be null :-)
   intentFiltersArray,
                                                     Most basic TAG-catch-all:
   techListsArray);                                  use ACTION_TAG_DISCOVERED in
                                                     filterList, no techs


intent = PendingIntent.getActivity(activity, 0,
              new Intent(activity, activity.getClass())
              .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
ndef.addDataType("*/*");
intentFiltersArray = new IntentFilter[] { ndef };

techListsArray = new String[][] { new String[] { NfcA.class.getName() } };
Receiving a Tag
public void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
...
tag.getId() //returns id as byte[]

String techs[] = tag.getTechList();
for (String tech : techs) {
	   if (tech.equals("android.nfc.tech.Ndef"))
	   {
	   	    Ndef ndef = Ndef.get(tag);
	   	    ndef.getType()
	   	    ndef.getMaxSize()
	   	    ndef.canMakeReadOnly()
	   	    ndef.isWritable()
	   	    NFCUtil.printNdefMessageDetails(ndef.getCachedNdefMessage()));	 	
	   }
}
Tag Technologies
Tag Technologies map to
tech specifications or to
pseudo-technologie or
capabilities like NDEF or
NDEFFormatable

                                      TagTechnology



MifareClassic               MifareUltralight   NdefFormatable   Ndef

       NfcA                      NfcB              NfcF         NfcV

                                                  IsoDep
Tag Technologies
Tag Technologies map to
tech specifications or to
pseudo-technologie or
capabilities like NDEF or
NDEFFormatable

                                      TagTechnology



MifareClassic               MifareUltralight   NdefFormatable   Ndef

       NfcA                      NfcB              NfcF         NfcV

                                                  IsoDep
NDEF
• NFC Data Exchange Format
• NFC Forum
• Records can be MIME-type media, URIs or
   RTDs (Record Type Definitions)

             1                 *
Ndef-Tag         NdefMessage       NdefRecord
NdefRecord
TNF    Type   ID   Payload
NdefRecord
         TNF    Type      ID        Payload




                          URI                 URI :-)


NdefRecord             WELL_KNOWN        SMART_POSTER


                         MIME                 TEXT
Reading Tag Content
//r = NdefRecord
if (r.getTnf() == NdefRecord.TNF_ABSOLUTE_URI) {
	   b.append(String
	   	    	   .format("TNF_ABSOLUTE_URI: type(%1$s), id(%2$s), payload(%3$s)n",
	   	    	   	   	    new String(r.getType()), idBytes.toString(),
	   	    	   	   	    new String(r.getPayload())));
}
else if (r.getTnf() == NdefRecord.TNF_MIME_MEDIA)
{
	   b.append(String
	   	    	   .format("TNF_MIME_MEDIA: type(%1$s), id(%2$s), payload(%3$s)n",
	   	    	   	   	    new String(r.getType()), idBytes.toString(),
	   	    	   	   	    new String(r.getPayload())));
}
Tag Dispatch
Tag Dispatch
           Foreground Activity Dispatch



 NDEF Data Dispatch: ACTION_NDEF_DISCOVERED



Tag Technology Dispatch: ACTION_TECH_DISCOVERED



   Fall-back Dispatch: ACTION_TAG_DISCOVERED
Direct Start

• Write custom NDEF
  MIME-type media
  messages
• Use custom
  IntentFilter to bind to
  your messages
Writing NDEF MIME

NdefMessage msg =
   NFCUtil.getNdefMimeMessage("application/json", "{"key":"value"}");
Intent i = new Intent(this, WriteActivity.class);
i.putExtra(WriteActivity.NDEF_MESSAGE, msg);
startActivity(i);
Writing NDEF MIME

NdefMessage msg =
   NFCUtil.getNdefMimeMessage("application/json", "{"key":"value"}");
Intent i = new Intent(this, WriteActivity.class);
i.putExtra(WriteActivity.NDEF_MESSAGE, msg);
startActivity(i);
Writing NDEF MIME

private static NdefRecord getMimeRecord(String mimeType, String content) {
	   NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
	   	    	   mimeType.getBytes(), getRandomIdBytes(), content.getBytes());
	   return record;
}
Writing NDEF MIME
Ndef ndef = Ndef.get(tag);
if (ndef.isWritable() && ndef.getMaxSize() > this.msg.toByteArray().length)
{
	    ndef.connect();
	    ndef.writeNdefMessage(this.msg);
	    ndef.close();
}
else
	    //show toast?
P2P: NDEF Push

• Create NDEF Message which is pushed to
  another active device once the other
  device is close
  NfcAdapter.enableForegroundNdefPush(activity, ndefmessage)
  NfcAdapter.disableForegroundNdefPush(activity)
about
Sven Haiges, hybris GmbH
  Twitter @hansamann

    Android, HTML5,
    Groovy & Grails


 sven.haiges@hybris.de
about
Sven Haiges, hybris GmbH
  Twitter @hansamann

    Android, HTML5,
    Groovy & Grails


 sven.haiges@hybris.de

                           NFC

More Related Content

KEY
Nfc Overview
PPTX
Near Field Communication by Mohammed Mudassir
PDF
Emerging Technologies in Payment Industry
PPTX
NFC: Shaping the Future of the Connected Customer Experience
PDF
VISIONFC - an NFC Forum event: NFC Payment & beyond presentations
PDF
Co Je A Bude Nfc Jan Nemec Gemalto
PDF
RFID/NFC for the Masses
PPTX
NEAR FIELD COMMUNICATION (NFC)
Nfc Overview
Near Field Communication by Mohammed Mudassir
Emerging Technologies in Payment Industry
NFC: Shaping the Future of the Connected Customer Experience
VISIONFC - an NFC Forum event: NFC Payment & beyond presentations
Co Je A Bude Nfc Jan Nemec Gemalto
RFID/NFC for the Masses
NEAR FIELD COMMUNICATION (NFC)

What's hot (20)

PDF
Product manager for dynamic nfc id 11949
PDF
Nokia NFC Presentation
PDF
Merchant360 SP4G(tm) NFC Coverage Walls Counters and Floors
PDF
PPTX
PDF
The User Experience of Near Field Communication
PDF
Contactless NFC Tags For Mobile Loyalty
PDF
Telecommunications in the logistic sector lodz
PPTX
near field communication
PPT
Near Field Communication on iPhone with iCarte
PPTX
Rfid and the Mobile phone quiz
PDF
NFC in direct mail: The pros and cons
PDF
Leverage RFID with NFC for Better ROI - by Steve McRae
PDF
NFC Guidebook, 2011
PDF
NFC Patents for Smartphone Mobile Payments Licensing Potential Assessment
PPTX
Vodafone Cash Service - NFC tag
PPT
Civintec introduction 2015
PPTX
Near Field Communication in m-Commerce
PDF
Designing low costs sensor system for the Internet of Things
PDF
E-Catalogue Of HUAYUAN RFID Products
Product manager for dynamic nfc id 11949
Nokia NFC Presentation
Merchant360 SP4G(tm) NFC Coverage Walls Counters and Floors
The User Experience of Near Field Communication
Contactless NFC Tags For Mobile Loyalty
Telecommunications in the logistic sector lodz
near field communication
Near Field Communication on iPhone with iCarte
Rfid and the Mobile phone quiz
NFC in direct mail: The pros and cons
Leverage RFID with NFC for Better ROI - by Steve McRae
NFC Guidebook, 2011
NFC Patents for Smartphone Mobile Payments Licensing Potential Assessment
Vodafone Cash Service - NFC tag
Civintec introduction 2015
Near Field Communication in m-Commerce
Designing low costs sensor system for the Internet of Things
E-Catalogue Of HUAYUAN RFID Products
Ad

Viewers also liked (20)

PPTX
Near field communication (nfc) technology
PPTX
NFC technical presentation
PPTX
NFC Technology
PDF
NFC Everywhere Brochure 2016
PPTX
NFC(Near Field Communication)
PPT
Nfc kdr
PPT
PDF
On Relaying NFC Payment Transactions using Android devices
PPTX
Near field communication
PDF
Near Field Communication (NFC)
PPTX
Tap into NFC Meetup - Boston
PPTX
NEAR FIELD COMMUNICATION
PPTX
Nfc tutorial
PDF
Guide du tag NFC : quels usages dans quels contextes ?
PDF
droidcon 2012: What's the Hack is NFC .., Hauke Meyn, NXP
PPTX
Near Field Communication (NFC Architecture and Operating Modes)
PPSX
Nfc-Full Presentation
PPTX
Near field communication
PPTX
Nfc technology ppt
KEY
Near Field Communication & Android
Near field communication (nfc) technology
NFC technical presentation
NFC Technology
NFC Everywhere Brochure 2016
NFC(Near Field Communication)
Nfc kdr
On Relaying NFC Payment Transactions using Android devices
Near field communication
Near Field Communication (NFC)
Tap into NFC Meetup - Boston
NEAR FIELD COMMUNICATION
Nfc tutorial
Guide du tag NFC : quels usages dans quels contextes ?
droidcon 2012: What's the Hack is NFC .., Hauke Meyn, NXP
Near Field Communication (NFC Architecture and Operating Modes)
Nfc-Full Presentation
Near field communication
Nfc technology ppt
Near Field Communication & Android
Ad

Similar to NFC on Android - Near Field Communication (20)

KEY
NFC & RFID on Android
PPTX
Nfc on Android
PDF
Android NFC
PPTX
NFC Bootcamp Seattle Day 2
PDF
DefCon 2012 - Near-Field Communication / RFID Hacking - Lee
PPTX
Near field communication
PPT
Architecture and Development of NFC Applications
PDF
A 2018 practical guide to hacking RFID/NFC
PDF
CONFidence 2018: A 2018 practical guide to hacking RFID/NFC (Sławomir Jasek)
PDF
A 2018 practical guide to hacking RFID/NFC
PDF
Building a dApp on Tezos
PPTX
Neal Thompson of Maritz at MPITechCon on RFID
PPT
Near field communication (NFC) in android
PDF
ABC2011Winter デ部 NFC
PPTX
Rto automation using nfc
PPTX
Aidc technology
PPT
Outsmarting smartphones
PDF
Rfid101 rfid introduction_lr
PDF
Rfid101 rfid introduction_lr
PDF
Android HCE: An intro into the world of NFC
NFC & RFID on Android
Nfc on Android
Android NFC
NFC Bootcamp Seattle Day 2
DefCon 2012 - Near-Field Communication / RFID Hacking - Lee
Near field communication
Architecture and Development of NFC Applications
A 2018 practical guide to hacking RFID/NFC
CONFidence 2018: A 2018 practical guide to hacking RFID/NFC (Sławomir Jasek)
A 2018 practical guide to hacking RFID/NFC
Building a dApp on Tezos
Neal Thompson of Maritz at MPITechCon on RFID
Near field communication (NFC) in android
ABC2011Winter デ部 NFC
Rto automation using nfc
Aidc technology
Outsmarting smartphones
Rfid101 rfid introduction_lr
Rfid101 rfid introduction_lr
Android HCE: An intro into the world of NFC

More from Sven Haiges (11)

PDF
NFC and Commerce combined
PDF
End to End Realtime Communication using Mobiel Devices and the Web
PDF
NFC Android Introduction
PDF
Gesture-controlled web-apps
KEY
CouchDB on Android
PDF
Android UI
KEY
PDF
Grails @ Java User Group Silicon Valley
PPT
Grails and Dojo
ODP
Grails 0.3-SNAPSHOT Presentation WJAX 2006 English
ODP
Grails 0.3-SNAPSHOT Presentation WJAX 2006
NFC and Commerce combined
End to End Realtime Communication using Mobiel Devices and the Web
NFC Android Introduction
Gesture-controlled web-apps
CouchDB on Android
Android UI
Grails @ Java User Group Silicon Valley
Grails and Dojo
Grails 0.3-SNAPSHOT Presentation WJAX 2006 English
Grails 0.3-SNAPSHOT Presentation WJAX 2006

Recently uploaded (20)

PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Electronic commerce courselecture one. Pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
Teaching material agriculture food technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation_ Review paper, used for researhc scholars
Network Security Unit 5.pdf for BCA BBA.
The Rise and Fall of 3GPP – Time for a Sabbatical?
“AI and Expert System Decision Support & Business Intelligence Systems”
Dropbox Q2 2025 Financial Results & Investor Presentation
Digital-Transformation-Roadmap-for-Companies.pptx
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Electronic commerce courselecture one. Pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The AUB Centre for AI in Media Proposal.docx
Advanced methodologies resolving dimensionality complications for autism neur...
Teaching material agriculture food technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Reach Out and Touch Someone: Haptics and Empathic Computing

NFC on Android - Near Field Communication

  • 1. NFC
  • 2. about Sven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
  • 3. about Sven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de NFC
  • 4. Basics Card Emulation Reader/Writer P2P NFC TagTechnologies Tag NDEF Dispatch APIs Android
  • 6. NFC roots: RFID • RFID - Radio Frequency Identification • 1983: First RFID patent granted • Typical, well-known use-cases for RFID animal identification, road taxes, libraries • 2004: Nokia, Philips and Sony established the NFC Forum
  • 7. NFC • “transfer data at low speed, without security, over short distances” • ~10 centimeters, in reality less • 424 kBit/s • 13.56 MHz
  • 8. NFC Devices • First mainstream Android smartphone: Nexus S • Common in new top Android phones • New RIM / Nokia devices • Apple iPhone?
  • 9. Nokia 6131 • released 2006 • early 2007: iPhone! • J2ME Spec: JSR 257 Contactless Communication API • NFC is not new!
  • 10. Forecast NFC handsets (k units) 600000 450000 300000 150000 0 2010 2011(e) 2012(e) 2013(e) 2014(e) 2015(e) Source:IHS iSuppli,compiled by Digitimes,May 2011
  • 12. Tickets Coupons Smart Poster Payment
  • 13. Tickets Coupons Smart Poster Payment Identity Keycard Personal ID
  • 14. NFC Checkin Tickets Social Coupons Gaming vCard Smart Poster Payment Identity Keycard Personal ID
  • 15. NFC Checkin Tickets Social Coupons Gaming vCard Smart Poster Payment WiFi Connections Bluetooth Identity Keycard Personal ID
  • 16. Transponder Format Technology/Capacity 13.56 Mhz Wristbands 125 Khz Cards Labels Sticker Keyfobs NFC Forum Tags Type 1-4
  • 17. can be made read only! NFC Forum Type 2 Tag Mifare Ultralight - 64 bytes Mifare Ultralight C - 192 bytes
  • 20. Mifare Ultralight All Surface Tag Mifare Ultralight Paper Ticket
  • 21. Mifare Ultralight All Surface Tag Mifare Ultralight Paper Ticket Mifare Ultralight Square Label
  • 22. Mifare Ultralight All Surface Tag Mifare Ultralight Paper Ticket Mifare Classic Mifare Ultralight Keyfob Square Label
  • 23. Shops • tagage.net • rfidshop.com • smartcardfocus.com • and many more...
  • 24. Comparing NFC Source:NFC Forum
  • 26. NFC Modes • Peer-to-Peer Mode • Reader/Writer Mode • Card Emulation Mode
  • 27. Android NFC Support • Peer-to-Peer Mode: limited • Reader/Writer Mode: excellent support • Card Emulation Mode: no support
  • 29. NFC & Android • Tag-Reading ab Android 2.3, API Level 9 • Tag-Writing ab Android 2.3.3, API Level 10 • Core classes: NFCAdapter, NFCManager, Tag Technology Classes
  • 30. Android Manifest • NFC Permission <uses-permission android:name="android.permission.NFC" /> • API Level <uses-sdk android:minSdkVersion="10" /> • NFC Feature <uses-feature android:name="android.hardware.nfc" android:required="true" />
  • 31. Android Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.flavor.android.nfc" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MimeActivity" android:label="MIME"> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED"/> <data android:mimeType="application/json" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.NFC" /> <uses-sdk android:minSdkVersion="10" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> </manifest>
  • 32. Android Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.flavor.android.nfc" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MimeActivity" android:label="MIME"> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED"/> <data android:mimeType="application/json" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.NFC" /> <uses-sdk android:minSdkVersion="10" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> </manifest>
  • 33. Android Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.flavor.android.nfc" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MimeActivity" android:label="MIME"> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED"/> <data android:mimeType="application/json" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.NFC" /> <uses-sdk android:minSdkVersion="10" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> </manifest>
  • 34. Android Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.flavor.android.nfc" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MimeActivity" android:label="MIME"> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED"/> <data android:mimeType="application/json" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.NFC" /> <uses-sdk android:minSdkVersion="10" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> </manifest>
  • 35. NFCManager • NFCManager used to get the NFCAdapter • Boring • Shortcut for getSystemService(NFC_SERVICE)
  • 36. NFCAdapter • the real thing • Control Foreground Dispatch and P2P NDEF Push • check if NFC is turned on
  • 37. Checking for NFC if (!getNFCAdapter().isEnabled()) { Toast.makeText(getApplicationContext(), "Please activate NFC and press back to return to the application!", Toast.LENGTH_LONG).show(); startActivity( new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS) ); }
  • 38. Reading a Tag • Unlock phone, touch tag :-) • Most likely, the intent chooser will open
  • 39. Foreground Dispatch • Foreground Activity may redirect all intents related to tag, technology or NDEF discovery to itself • Getting all tags while activity is visible: enable in onResume(), disable in onPause()
  • 40. Foreground Dispatch nfcAdapter.enableForegroundDispatch( techListsArray does not matter activity, here, as ACTION_TECH_DISCOVERED is not in intentFilters array. It can pendingIntent, be null :-) intentFiltersArray, Most basic TAG-catch-all: techListsArray); use ACTION_TAG_DISCOVERED in filterList, no techs intent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()) .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); ndef.addDataType("*/*"); intentFiltersArray = new IntentFilter[] { ndef }; techListsArray = new String[][] { new String[] { NfcA.class.getName() } };
  • 41. Receiving a Tag public void onNewIntent(Intent intent) { Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); } ... tag.getId() //returns id as byte[] String techs[] = tag.getTechList(); for (String tech : techs) { if (tech.equals("android.nfc.tech.Ndef")) { Ndef ndef = Ndef.get(tag); ndef.getType() ndef.getMaxSize() ndef.canMakeReadOnly() ndef.isWritable() NFCUtil.printNdefMessageDetails(ndef.getCachedNdefMessage())); } }
  • 42. Tag Technologies Tag Technologies map to tech specifications or to pseudo-technologie or capabilities like NDEF or NDEFFormatable TagTechnology MifareClassic MifareUltralight NdefFormatable Ndef NfcA NfcB NfcF NfcV IsoDep
  • 43. Tag Technologies Tag Technologies map to tech specifications or to pseudo-technologie or capabilities like NDEF or NDEFFormatable TagTechnology MifareClassic MifareUltralight NdefFormatable Ndef NfcA NfcB NfcF NfcV IsoDep
  • 44. NDEF • NFC Data Exchange Format • NFC Forum • Records can be MIME-type media, URIs or RTDs (Record Type Definitions) 1 * Ndef-Tag NdefMessage NdefRecord
  • 45. NdefRecord TNF Type ID Payload
  • 46. NdefRecord TNF Type ID Payload URI URI :-) NdefRecord WELL_KNOWN SMART_POSTER MIME TEXT
  • 47. Reading Tag Content //r = NdefRecord if (r.getTnf() == NdefRecord.TNF_ABSOLUTE_URI) { b.append(String .format("TNF_ABSOLUTE_URI: type(%1$s), id(%2$s), payload(%3$s)n", new String(r.getType()), idBytes.toString(), new String(r.getPayload()))); } else if (r.getTnf() == NdefRecord.TNF_MIME_MEDIA) { b.append(String .format("TNF_MIME_MEDIA: type(%1$s), id(%2$s), payload(%3$s)n", new String(r.getType()), idBytes.toString(), new String(r.getPayload()))); }
  • 49. Tag Dispatch Foreground Activity Dispatch NDEF Data Dispatch: ACTION_NDEF_DISCOVERED Tag Technology Dispatch: ACTION_TECH_DISCOVERED Fall-back Dispatch: ACTION_TAG_DISCOVERED
  • 50. Direct Start • Write custom NDEF MIME-type media messages • Use custom IntentFilter to bind to your messages
  • 51. Writing NDEF MIME NdefMessage msg = NFCUtil.getNdefMimeMessage("application/json", "{"key":"value"}"); Intent i = new Intent(this, WriteActivity.class); i.putExtra(WriteActivity.NDEF_MESSAGE, msg); startActivity(i);
  • 52. Writing NDEF MIME NdefMessage msg = NFCUtil.getNdefMimeMessage("application/json", "{"key":"value"}"); Intent i = new Intent(this, WriteActivity.class); i.putExtra(WriteActivity.NDEF_MESSAGE, msg); startActivity(i);
  • 53. Writing NDEF MIME private static NdefRecord getMimeRecord(String mimeType, String content) { NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeType.getBytes(), getRandomIdBytes(), content.getBytes()); return record; }
  • 54. Writing NDEF MIME Ndef ndef = Ndef.get(tag); if (ndef.isWritable() && ndef.getMaxSize() > this.msg.toByteArray().length) { ndef.connect(); ndef.writeNdefMessage(this.msg); ndef.close(); } else //show toast?
  • 55. P2P: NDEF Push • Create NDEF Message which is pushed to another active device once the other device is close NfcAdapter.enableForegroundNdefPush(activity, ndefmessage) NfcAdapter.disableForegroundNdefPush(activity)
  • 56. about Sven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
  • 57. about Sven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de NFC

Editor's Notes