Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Android and NFC / NDEF (with Kotlin)

290 views

Published on

In this presentation, you will learn how to add NFC tag reading to an Android app. It registers for auto-starting when the user taps a specific NDEF NFC tag with the phone. In addition, the app reads the NDEF records from the tag.

Further instructions: https://www.andreasjakl.com/nfc-tags-ndef-and-android-with-kotlin/
Open source sample app: https://github.com/andijakl/NfcDemo

Published in: Technology
  • Be the first to comment

Android and NFC / NDEF (with Kotlin)

  1. 1. St. Pölten University of Applied SciencesSt. Pölten University of Applied Sciences Platzhalter für möglichen Bildeinsatz Android and NFC / NDEF (with Kotlin) Andreas Jakl Digital Healthcare FH St. Pölten Platzhalter für möglichen Bildeinsatz Version 1.2 https://www.andreasjakl.com/nfc-tags-ndef-and-android-with-kotlin/
  2. 2. NEAR FIELD COMMUNICATION Open NFC with NDEF Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 2
  3. 3. NFC? 3 < 1 cm (tap) Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
  4. 4. NFC? 4 < 424 kbit / s Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
  5. 5. NFC? 5Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
  6. 6. NFC Modes of Operation Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 6 Peer to PeerReader / Writer Card Emulation
  7. 7. NFC Tags 7 Tag memory size: 48 byte – few kB Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
  8. 8. NFC & NDEF Overview 8 NDEF Message NDEF Record (e.g., URL) … NDEF = NFC Data Exchange Format Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
  9. 9. NDEF 9 Smart Poster MIME Hand- over Custom Empty Uri Text Image vCard Web Sms Tel Record types Possible payloads Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
  10. 10. Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten GPS ▪ Worldwide, outdoors ▪ Uni-directional ▪ Localization Beacons ▪ Also indoors, 0-70 m ▪ Uni-directional ▪ Proximity information NFC ▪ Contact ▪ Bi-directional ▪ Pay, Access, Trigger Now with iPhone support! 11
  11. 11. Open Source NDEF Library 13 Reusable NDEF classes Create NDEF messages & records (standard compliant) Parse information from raw byte arrays Fully documented Open Source LGPL License andijakl.github.io/ndef-nfc library development supported by: Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
  12. 12. Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 14 Source: O2, https://www.youtube.com/watch?v=WVhyCWO3Y0w
  13. 13. Prepare NFC Tags Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 15 NXP TagInfo https://play.google.com/store/apps/details?id=com.nxp.taginfolite&hl=en NXP TagWriter https://play.google.com/store/apps/details?id=com.nxp.nfc.tagwriter&hl=en
  14. 14. Exercise: Create URL Tag (“Link”) Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 16
  15. 15. ANDROID & NFC Reading Tags Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 17
  16. 16. Start App ▪ Get Start App ▪ Includes scrolling message logging UI ▪ Saves TextView contents ▪ https://www.andreasjakl.com/08-android-nfcdemo-start/ ▪ Finished app: https://github.com/andijakl/NfcDemo ▪ Note: no NFC support in the emulator, use phone! Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 18 logMessage("Welcome", "App started")
  17. 17. Capabilities – Manifest ▪ Get access to NFC ▪ Declare hardware / software features used by apps ▪ required = true: app doesn’t work without ▪ Google Play Store filters apps Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 19 <uses-permission android:name="android.permission.NFC" /> <uses-feature android:name="android.hardware.nfc" android:required="true" />
  18. 18. NFC Support? Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 20 var nfcAdapter = NfcAdapter.getDefaultAdapter(this) null -> no NFC hardware nfcAdapter?.isEnabled -> false = NFC disabled in phone. Trigger settings UI: startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))
  19. 19. Launching Apps with NFC Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 21
  20. 20. URLs on NFC Tags to Launch Apps ▪ Register app for specific URL using deep links ▪ App not installed? ▪ User gets to web site with web version + link to app store ▪ App installed? ▪ Phone launches your app ▪ URL acts as deep link to your content ▪ https://developer.android.com/training/app-links/deep-linking.html ▪ Optional: bypass user consent with app links ▪ Place verification JSON on web server ▪ https://developer.android.com/training/app-links/verify-site-associations.html Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 22
  21. 21. Launch App Through NFC ▪ Add intent filter to Manifest for activity Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 23 <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED"/> <!-- Default category is required for the intent filter to work --> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE" /> <!-- Intent filters without data will never be triggered on NFC devices. Always define the specific data you expect to be present. --> <data android:scheme="https" android:host="www.andreasjakl.com" /> <!-- Additionally support http scheme. See: https://developer.android.com/training/app-links/verify-site-associations.html --> <data android:scheme="http" /> </intent-filter> <activity android:name=".MainActivity" android:launchMode="singleTop"> …
  22. 22. NFC / NDEF Intent Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 24 Intent Action: NfcAdapter.ACTION_NDEF_DISCOVERED Parcelable Array Extra: Raw NDEF message NDEF Message NDEF Record (e.g., URL) …
  23. 23. Receiving Intents Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 25 ActivityonCreate() onNewIntent() New instance of activity. Access intent through activity’s property. Activity is already open. Configure activity in manifest – sends intent to existing activity, doesn’t launch a new instance: android:launchMode="singleTop" Intent
  24. 24. App Started from NFC? ▪ App started from NFC Tag? ▪ Android calls onCreate() ▪ Intent available as property from Activity ▪ App already running in the foreground? ▪ Android calls onNewIntent() ▪ Intent available as parameter Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 26 override fun onCreate(savedInstanceState: Bundle?) { // ... if (intent != null) { processIntent(intent) } } override fun onNewIntent(intent: Intent?) { super.onNewIntent(intent) if (intent != null) { processIntent(intent) } }
  25. 25. private fun processIntent(checkIntent: Intent) { // Check if intent has the action of a discovered NFC tag // with NDEF formatted contents if (checkIntent.action == NfcAdapter.ACTION_NDEF_DISCOVERED) { // Retrieve the raw NDEF message from the tag val rawMessages = checkIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES) // ... } } Check Intent & Retrieve NDEF Message Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 27
  26. 26. private fun processIntent(checkIntent: Intent) { // Check if intent has the action of a discovered NFC tag // with NDEF formatted contents if (checkIntent.action == NfcAdapter.ACTION_NDEF_DISCOVERED) { // Retrieve the raw NDEF message from the tag val rawMessages = checkIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES) // ... } } Check Intent & Retrieve NDEF Message Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 28
  27. 27. private fun processIntent(checkIntent: Intent) { // Check if intent has the action of a discovered NFC tag // with NDEF formatted contents if (checkIntent.action == NfcAdapter.ACTION_NDEF_DISCOVERED) { // Retrieve the raw NDEF message from the tag val rawMessages = checkIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES) // ... } } Check Intent & Retrieve NDEF Message Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 29 Encoded https://www. into 1 byte (0x02) https://github.com/andijakl/ndef- nfc/blob/master/NdefLibrary/NdefLibrary/Ndef/NdefUriRecord.cs andreasjakl.com/contact Check ASCII table!
  28. 28. Convert NDEF Message ▪ Extract 1st NDEF message ▪ Get first record of message ▪ Extract information Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 30 var ndefMsg = rawMessages[0] as NdefMessage var ndefRecord = ndefMsg.records[0] if (ndefRecord.toUri() != null) { // Use Android functionality to convert payload to URI logMessage("URI detected", ndefRecord.toUri().toString()) } else { // Other NFC Tags logMessage("Payload", ndefRecord.payload.contentToString()) } Note: always check for null / array size!
  29. 29. Troubleshooting: Supported Links ▪ Check app registration ▪ Android Settings ▪ Our app: registered for links? Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 31
  30. 30. Troubleshooting: Chrome as Default ▪ Defined Chrome as default handler for all URLs? ▪ No prompt to launch our app appears ▪ Android 8 ▪ Settings > Apps & notifications > Advanced > Default apps > Opening links > Chrome > Other defaults: clear defaults Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 32
  31. 31. Android Application Record ▪ Associate tag with your app ▪ 2nd NDEF record: contains package name ▪ Android launches app / opens Google Play Store ▪ https://developer.android.com/guide/topics/conn ectivity/nfc/nfc.html#aar ▪ Include with NXP Tag Writer Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 33
  32. 32. Final App ▪ Download final app ▪ https://github.com/andijakl/NfcDemo ▪ Implements additional feature: ▪ Foreground Dispatch: gets & analyzes all NDEF messages when the app is running in the foreground Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 34 Screenshot shows solution app: - App launched through NFC (NDEF_DISCOVERED in onCreate()). - NDEF message contains 2 records: URL + Android Application Record - While running: tapped 2nd NFC tag with different (non-subscribed) URL - Delivered through onNewIntent() thanks to active Foreground Dispatch
  33. 33. SMART CARDS & HOST CARD EMULATION Low Level NFC Interaction Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 35
  34. 34. Smart Cards ▪ Interface ▪ Contact (chip) ▪ Contactless (eg NFC compatible) 36Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten Image credits: Maestro paypass
  35. 35. Smart Card Content ▪ Can be very powerful ▪ Microprocessor ▪ Non-volatile memory and cryptography ▪ Programmable apps (e.g. Java Card) ▪ Use cases ▪ Credit card ▪ Public transport cards ▪ Key card for doors ▪ … 37Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten Image source: created by Dacs, WhiteTimberwolf. License: CC BY-SA 3.0 https://en.wikipedia.org/wiki/File:SmartCardPinout.svg
  36. 36. Smart Card Communication: APDU ▪ Application Protocol Data Unit ▪ Communication protocol 38 Response APDU Status code Response data Command APDU Header (Instruction code) Parameter data Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten Image source: Google Pixel 2 Press image
  37. 37. Phone Host Card Emulation ▪ Emulate Smart Card with app ▪ NFC reader directly communicates with app ▪ Use cases: building access, loyalty, transit, … 39Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten NFC Controller Secure Element Host CPU Hardware component. SIM card or embedded Host Card Emulation Emulate smart card with an app, without the need for hardware secure element NFC Reader
  38. 38. THANK YOU! More information: https://www.andreasjakl.com/nfc-tags-ndef-and-android-with-kotlin/ Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 40

×