Tizen NFC Card Emulation Mode
PUBLISHED
Introduction
Near field communication (NFC) is a form of contactless communication between devices or between a device and a chip/tag. Using NFC, a user can transfer/receive small amount information from a short distance (example, 10 cm).
Card-emulation mode, as the name suggests, makes the device behave like a contactless smart card. Using this mode we can develop virtual credit cards, debit cards, transit cards, and access cards. In this tip document, sample code from a Web app is shown for understanding.
- NFC API does not provide any methods to turn NFC on/off directly in the device. To use NFC, user must turn it on/off physically from the Tizen OS settings.
Steps to create a simple web app for card emulation mode
Step-1: Add permissions
In the tizen-manifest.xml, the required privileges should be added:
http://tizen.org/privilege/nfc.cardemulation http://tizen.org/privilege/nfc.common http://tizen.org/privilege/application.launch http://tizen.org/privilege/nfc.p2p http://tizen.org/privilege/nfc.tag http://tizen.org/privilege/nfc.admin
Step-2: Check if NFC adapter is supported o not
var adapter = tizen.nfc.getDefaultAdapter();
Step-3: Turning card emulation mode ON/OFF
To enable card emulation mode use:
adapter.cardEmulationMode = "ALWAYS_ON";
To disable card emulation mode use:
adapter.cardEmulationMode = "OFF";
Step-4: Turning on listener to receive data
To check the current card emulation mode, register a listener “modeListenerId”:
var modeListenerId = 0; modeListenerId = adapter.addCardEmulationModeChangeListener(function(mode){ if (mode === "ALWAYS_ON"){ console.log("We are ready to go now"); } });
Step-5: Check if listener detects expected NFC element
To detect changes in an active NFC secure element, register a listener “aseListenerId”:
var aseListenerId = 0; aseListenerId = adapter.addActiveSecureElementChangeListener(function(seType) { console.log("Active secure element is " + seType); });
Step-6: Event handler of NFC data exchange
To detect exchange of a NFC secure element transaction data, register a listener “transListenerId”:
var transListenerId = 0; function onDetected(appletId, data){ console.log("NFC secure element transaction detected. Application: " + appletId + ". Protocol data: " + data); }); transListenerId = adapter.addTransactionEventListener("UICC", onDetected);
Step-7: Clearing up the resources used in card emulation
adapter.removeActiveSecureElementChangeListener(aseListenerId); adapter.removeTransactionEventListener(transListenerId); adapter.removeCardEmulationModeChangeListener(modeListenerId); adapter.cardEmulationMode = "OFF";
Step-8: Running the example application
Please check the attached sample code.
For testing purpose, we have used an android app named NFC Reader (https://play.google.com/store/apps/details?id=se.anyro.nfc_reader) as the reader app.
The copywrite ownership of this "NFC Reader" belongs to Adam Nybäck (https://play.google.com/store/apps/developer?id=Adam+Nyb%C3%A4ck).
In addition, we deployed our example app in a Gear S2 to act as the emulated card.
Once we start the example app in gear S2 (after enabling NFC from settings), it shows the following message:
Fig 1: Start up view of card emulation mode
Then, when we try to read from the gear S2 using the NFC reader, it shows the following:
Fig 2: Screenshot when NFC reader reads the tag
Useful references:
[1] https://developer.tizen.org/development/tutorials/web-application/tizen-...
[2] https://developer.tizen.org/dev-guide/2.4/org.tizen.tutorials/html/web/t...
[3] https://developer.tizen.org/dev-guide/2.4/org.tizen.web.apireference/htm...
[4] https://developer.tizen.org/dev-guide/2.4/org.tizen.web.apireference/htm...
- The NFC Card Emulation API of Tizen 2.3 only supports enabling and disabling the card emulation mode, and retrieving the card emulation status.