SlideShare una empresa de Scribd logo
1 de 38
Descargar para leer sin conexión
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/
NEAR FIELD COMMUNICATION
Open NFC with NDEF
Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 2
NFC?
3
< 1 cm
(tap)
Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
NFC?
4
< 424 kbit / s
Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
NFC?
5Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
NFC Modes of Operation
Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 6
Peer to PeerReader / Writer Card Emulation
NFC Tags
7
Tag memory size:
48 byte – few kB
Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
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
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
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
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
Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 14
Source: O2, https://www.youtube.com/watch?v=WVhyCWO3Y0w
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
Exercise: Create URL Tag (“Link”)
Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 16
ANDROID & NFC
Reading Tags
Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 17
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")
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" />
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))
Launching Apps with NFC
Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 21
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
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">
…
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)
…
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
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)
}
}
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
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
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!
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!
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
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
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
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
SMART CARDS & HOST CARD
EMULATION
Low Level NFC Interaction
Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 35
Smart Cards
▪ Interface
▪ Contact
(chip)
▪ Contactless
(eg NFC compatible)
36Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten Image credits: Maestro paypass
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
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
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
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

Más contenido relacionado

La actualidad más candente

Android seminar-presentation
Android seminar-presentationAndroid seminar-presentation
Android seminar-presentation
connectshilpa
 
Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)
Ahsanul Karim
 

La actualidad más candente (20)

Android studio ppt
Android studio pptAndroid studio ppt
Android studio ppt
 
Introduction to Mobile Development
Introduction to Mobile DevelopmentIntroduction to Mobile Development
Introduction to Mobile Development
 
Android app development ppt
Android app development pptAndroid app development ppt
Android app development ppt
 
android phone ppt
android phone pptandroid phone ppt
android phone ppt
 
An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...
 
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
Android | Android Activity Launch Modes and Tasks | Gonçalo SilvaAndroid | Android Activity Launch Modes and Tasks | Gonçalo Silva
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
 
Mobile phone applications
Mobile phone applicationsMobile phone applications
Mobile phone applications
 
Android app ppt
Android app pptAndroid app ppt
Android app ppt
 
TO DO list APP Called Do It
TO DO list APP Called Do ItTO DO list APP Called Do It
TO DO list APP Called Do It
 
Top 11 Mobile App Development Frameworks
Top 11 Mobile App Development FrameworksTop 11 Mobile App Development Frameworks
Top 11 Mobile App Development Frameworks
 
Mobile application development ppt
Mobile application development pptMobile application development ppt
Mobile application development ppt
 
Android architecture
Android architectureAndroid architecture
Android architecture
 
Hybrid mobile app development
Hybrid mobile app developmentHybrid mobile app development
Hybrid mobile app development
 
Android - A brief introduction
Android - A brief introductionAndroid - A brief introduction
Android - A brief introduction
 
Mobile application development
Mobile application developmentMobile application development
Mobile application development
 
Android seminar-presentation
Android seminar-presentationAndroid seminar-presentation
Android seminar-presentation
 
Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)
 
Flutter Bootcamp
Flutter BootcampFlutter Bootcamp
Flutter Bootcamp
 
Android Application Development Using Java
Android Application Development Using JavaAndroid Application Development Using Java
Android Application Development Using Java
 
Location-Based Services on Android
Location-Based Services on AndroidLocation-Based Services on Android
Location-Based Services on Android
 

Similar a Android and NFC / NDEF (with Kotlin)

Pradeep_iOS_Developer
Pradeep_iOS_DeveloperPradeep_iOS_Developer
Pradeep_iOS_Developer
Pradeep kn
 

Similar a Android and NFC / NDEF (with Kotlin) (20)

Android Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App ManagementAndroid Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App Management
 
GDG Oslo: Hidden Android features
GDG Oslo: Hidden Android featuresGDG Oslo: Hidden Android features
GDG Oslo: Hidden Android features
 
NFC and the Salesforce Mobile SDK
NFC and the Salesforce Mobile SDKNFC and the Salesforce Mobile SDK
NFC and the Salesforce Mobile SDK
 
Introduction to Digital Analytics for Apps - Trusted Conf
Introduction to Digital Analytics for Apps - Trusted ConfIntroduction to Digital Analytics for Apps - Trusted Conf
Introduction to Digital Analytics for Apps - Trusted Conf
 
Nfc sfdc mobile_sdk
Nfc sfdc mobile_sdkNfc sfdc mobile_sdk
Nfc sfdc mobile_sdk
 
Discover Meteor
Discover MeteorDiscover Meteor
Discover Meteor
 
Kuldeep_IOS
Kuldeep_IOSKuldeep_IOS
Kuldeep_IOS
 
Android Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSONAndroid Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSON
 
Snap4City November 2019 Course: Smart City IOT platform installation, deploy,...
Snap4City November 2019 Course: Smart City IOT platform installation, deploy,...Snap4City November 2019 Course: Smart City IOT platform installation, deploy,...
Snap4City November 2019 Course: Smart City IOT platform installation, deploy,...
 
Bitcraft - Portfolio
Bitcraft - PortfolioBitcraft - Portfolio
Bitcraft - Portfolio
 
Activity counts
Activity countsActivity counts
Activity counts
 
Accemy projects portfolio 20 jan20
Accemy projects portfolio 20 jan20Accemy projects portfolio 20 jan20
Accemy projects portfolio 20 jan20
 
Pradeep_iOS_Developer
Pradeep_iOS_DeveloperPradeep_iOS_Developer
Pradeep_iOS_Developer
 
Android Context Browser- DroidDevCPH
Android Context Browser- DroidDevCPHAndroid Context Browser- DroidDevCPH
Android Context Browser- DroidDevCPH
 
ITCamp 2018 - Dan Ardelean - CI/CD for mobile development using Visual Studio...
ITCamp 2018 - Dan Ardelean - CI/CD for mobile development using Visual Studio...ITCamp 2018 - Dan Ardelean - CI/CD for mobile development using Visual Studio...
ITCamp 2018 - Dan Ardelean - CI/CD for mobile development using Visual Studio...
 
CI/CD for mobile development using Visual Studio App Center
CI/CD for mobile development using Visual Studio App CenterCI/CD for mobile development using Visual Studio App Center
CI/CD for mobile development using Visual Studio App Center
 
Nfc on Android
Nfc on AndroidNfc on Android
Nfc on Android
 
Create Event-Driven iOS Apps Using IBM Mobile Foundation, OpenWhisk Runtime a...
Create Event-Driven iOS Apps Using IBM Mobile Foundation, OpenWhisk Runtime a...Create Event-Driven iOS Apps Using IBM Mobile Foundation, OpenWhisk Runtime a...
Create Event-Driven iOS Apps Using IBM Mobile Foundation, OpenWhisk Runtime a...
 
RakeshKushwaha
RakeshKushwahaRakeshKushwaha
RakeshKushwaha
 
Company2
Company2Company2
Company2
 

Más de Andreas Jakl

Más de Andreas Jakl (20)

Create Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented RealityCreate Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented Reality
 
AR / VR Interaction Development with Unity
AR / VR Interaction Development with UnityAR / VR Interaction Development with Unity
AR / VR Interaction Development with Unity
 
Basics of Web Technologies
Basics of Web TechnologiesBasics of Web Technologies
Basics of Web Technologies
 
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & MoreBluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
 
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
 
Mobile Test Automation
Mobile Test AutomationMobile Test Automation
Mobile Test Automation
 
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
 
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows PhoneWinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
 
Nokia New Asha Platform Developer Training
Nokia New Asha Platform Developer TrainingNokia New Asha Platform Developer Training
Nokia New Asha Platform Developer Training
 
Windows Phone 8 NFC Quickstart
Windows Phone 8 NFC QuickstartWindows Phone 8 NFC Quickstart
Windows Phone 8 NFC Quickstart
 
Windows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App ScenariosWindows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App Scenarios
 
Windows 8 Platform NFC Development
Windows 8 Platform NFC DevelopmentWindows 8 Platform NFC Development
Windows 8 Platform NFC Development
 
NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)
 
06 - Qt Communication
06 - Qt Communication06 - Qt Communication
06 - Qt Communication
 
05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics
 
04 - Qt Data
04 - Qt Data04 - Qt Data
04 - Qt Data
 
03 - Qt UI Development
03 - Qt UI Development03 - Qt UI Development
03 - Qt UI Development
 
02 - Basics of Qt
02 - Basics of Qt02 - Basics of Qt
02 - Basics of Qt
 
Basics of WRT (Web Runtime)
Basics of WRT (Web Runtime)Basics of WRT (Web Runtime)
Basics of WRT (Web Runtime)
 
Java ME - Introduction
Java ME - IntroductionJava ME - Introduction
Java ME - Introduction
 

Último

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Último (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Android and NFC / NDEF (with Kotlin)

  • 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. NEAR FIELD COMMUNICATION Open NFC with NDEF Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 2
  • 3. NFC? 3 < 1 cm (tap) Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
  • 4. NFC? 4 < 424 kbit / s Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
  • 5. NFC? 5Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
  • 6. NFC Modes of Operation Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 6 Peer to PeerReader / Writer Card Emulation
  • 7. NFC Tags 7 Tag memory size: 48 byte – few kB Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten
  • 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. 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. 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. 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. Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 14 Source: O2, https://www.youtube.com/watch?v=WVhyCWO3Y0w
  • 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. Exercise: Create URL Tag (“Link”) Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 16
  • 15. ANDROID & NFC Reading Tags Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 17
  • 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. 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. 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. Launching Apps with NFC Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 21
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. SMART CARDS & HOST CARD EMULATION Low Level NFC Interaction Native Mobile App Development | 2018 | Andreas Jakl | FH St. Pölten 35
  • 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. 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. 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. 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. 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