Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

NFC on Android - Near Field Communication

27.166 visualizaciones

Publicado el

Introduces Near Field Communication, NFC Modes, Android APIs and mysterious NDEF Format (NDEFMessages, NDEFRecord, RTC, TNF, etc.)

Publicado en: Tecnología, Empresariales
  • Inicia sesión para ver los comentarios

NFC on Android - Near Field Communication

  1. 1. NFC
  2. 2. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
  3. 3. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de NFC
  4. 4. Basics Card Emulation Reader/Writer P2P NFC TagTechnologiesTag NDEFDispatch APIs Android
  5. 5. Basics
  6. 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. 7. NFC• “transfer data at low speed, without security, over short distances”• ~10 centimeters, in reality less• 424 kBit/s• 13.56 MHz
  8. 8. NFC Devices• First mainstream Android smartphone: Nexus S• Common in new top Android phones• New RIM / Nokia devices• Apple iPhone?
  9. 9. Nokia 6131 • released 2006 • early 2007: iPhone! • J2ME Spec: JSR 257 Contactless Communication API • NFC is not new!
  10. 10. Forecast NFC handsets (k units)600000450000300000150000 0 2010 2011(e) 2012(e) 2013(e) 2014(e) 2015(e) Source:IHS iSuppli,compiled by Digitimes,May 2011
  11. 11. Payment
  12. 12. Tickets Coupons Smart PosterPayment
  13. 13. Tickets Coupons Smart PosterPayment Identity Keycard Personal ID
  14. 14. NFC Checkin Tickets Social CouponsGaming vCard Smart Poster Payment Identity Keycard Personal ID
  15. 15. NFC Checkin Tickets Social Coupons Gaming vCard Smart Poster Payment WiFiConnections Bluetooth Identity Keycard Personal ID
  16. 16. Transponder Format Technology/Capacity 13.56 Mhz Wristbands 125 Khz Cards LabelsSticker Keyfobs NFC Forum Tags Type 1-4
  17. 17. can be made read only!NFC Forum Type 2 Tag Mifare Ultralight - 64 bytes Mifare Ultralight C - 192 bytes
  18. 18. Mifare UltralightAll Surface Tag
  19. 19. Mifare UltralightAll Surface Tag Mifare Ultralight Paper Ticket
  20. 20. Mifare UltralightAll Surface Tag Mifare Ultralight Paper Ticket Mifare Ultralight Square Label
  21. 21. Mifare Ultralight All Surface Tag Mifare Ultralight Paper TicketMifare Classic Mifare UltralightKeyfob Square Label
  22. 22. Shops• tagage.net• rfidshop.com• smartcardfocus.com• and many more...
  23. 23. Comparing NFC Source:NFC Forum
  24. 24. NFC Modes
  25. 25. NFC Modes• Peer-to-Peer Mode• Reader/Writer Mode• Card Emulation Mode
  26. 26. Android NFC Support• Peer-to-Peer Mode: limited• Reader/Writer Mode: excellent support• Card Emulation Mode: no support
  27. 27. NFC & Android
  28. 28. 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
  29. 29. 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" />
  30. 30. 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>
  31. 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. 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. 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. 34. NFCManager• NFCManager used to get the NFCAdapter• Boring• Shortcut for getSystemService(NFC_SERVICE)
  35. 35. NFCAdapter• the real thing• Control Foreground Dispatch and P2P NDEF Push• check if NFC is turned on
  36. 36. Checking for NFCif (!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) );}
  37. 37. Reading a Tag• Unlock phone, touch tag :-)• Most likely, the intent chooser will open
  38. 38. 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()
  39. 39. Foreground DispatchnfcAdapter.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 techsintent = 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() } };
  40. 40. Receiving a Tagpublic 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())); }}
  41. 41. Tag TechnologiesTag Technologies map totech specifications or topseudo-technologie orcapabilities like NDEF orNDEFFormatable TagTechnologyMifareClassic MifareUltralight NdefFormatable Ndef NfcA NfcB NfcF NfcV IsoDep
  42. 42. Tag TechnologiesTag Technologies map totech specifications or topseudo-technologie orcapabilities like NDEF orNDEFFormatable TagTechnologyMifareClassic MifareUltralight NdefFormatable Ndef NfcA NfcB NfcF NfcV IsoDep
  43. 43. NDEF• NFC Data Exchange Format• NFC Forum• Records can be MIME-type media, URIs or RTDs (Record Type Definitions) 1 *Ndef-Tag NdefMessage NdefRecord
  44. 44. NdefRecordTNF Type ID Payload
  45. 45. NdefRecord TNF Type ID Payload URI URI :-)NdefRecord WELL_KNOWN SMART_POSTER MIME TEXT
  46. 46. Reading Tag Content//r = NdefRecordif (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())));}
  47. 47. Tag Dispatch
  48. 48. Tag Dispatch Foreground Activity Dispatch NDEF Data Dispatch: ACTION_NDEF_DISCOVEREDTag Technology Dispatch: ACTION_TECH_DISCOVERED Fall-back Dispatch: ACTION_TAG_DISCOVERED
  49. 49. Direct Start• Write custom NDEF MIME-type media messages• Use custom IntentFilter to bind to your messages
  50. 50. Writing NDEF MIMENdefMessage msg = NFCUtil.getNdefMimeMessage("application/json", "{"key":"value"}");Intent i = new Intent(this, WriteActivity.class);i.putExtra(WriteActivity.NDEF_MESSAGE, msg);startActivity(i);
  51. 51. Writing NDEF MIMENdefMessage msg = NFCUtil.getNdefMimeMessage("application/json", "{"key":"value"}");Intent i = new Intent(this, WriteActivity.class);i.putExtra(WriteActivity.NDEF_MESSAGE, msg);startActivity(i);
  52. 52. Writing NDEF MIMEprivate static NdefRecord getMimeRecord(String mimeType, String content) { NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeType.getBytes(), getRandomIdBytes(), content.getBytes()); return record;}
  53. 53. Writing NDEF MIMENdef ndef = Ndef.get(tag);if (ndef.isWritable() && ndef.getMaxSize() > this.msg.toByteArray().length){ ndef.connect(); ndef.writeNdefMessage(this.msg); ndef.close();}else //show toast?
  54. 54. 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)
  55. 55. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de
  56. 56. aboutSven Haiges, hybris GmbH Twitter @hansamann Android, HTML5, Groovy & Grails sven.haiges@hybris.de NFC

×