Add Nfc Csharp guide 14/137914/2
authorjh8801.jung <jh8801.jung@samsung.com>
Mon, 10 Jul 2017 08:38:45 +0000 (17:38 +0900)
committerEditor Lionbridge <TizenEditor.SEL@lionbridge.com>
Tue, 11 Jul 2017 10:30:28 +0000 (13:30 +0300)
PS2: Reviewed

Signed-off-by: jh8801.jung <jh8801.jung@samsung.com>
Change-Id: I6bd2e1ab94b3a6fa8ac7b44a3d67b0e4364cfb93

org.tizen.guides/html/dotnet/connectivity/nfc_cs.htm [new file with mode: 0755]

diff --git a/org.tizen.guides/html/dotnet/connectivity/nfc_cs.htm b/org.tizen.guides/html/dotnet/connectivity/nfc_cs.htm
new file mode 100755 (executable)
index 0000000..0c7dbac
--- /dev/null
@@ -0,0 +1,848 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+    <meta http-equiv="X-UA-Compatible" content="IE=9" />
+    <link rel="stylesheet" type="text/css" href="../../css/styles.css" />
+    <link rel="stylesheet" type="text/css" href="../../css/snippet.css" />
+    <script type="text/javascript" src="../../scripts/snippet.js"></script>
+    <script type="text/javascript" src="../../scripts/jquery.util.js" charset="utf-8"></script>
+    <script type="text/javascript" src="../../scripts/common.js" charset="utf-8"></script>
+    <script type="text/javascript" src="../../scripts/core.js" charset="utf-8"></script>
+    <script type="text/javascript" src="../../scripts/search.js" charset="utf-8"></script>
+
+    <title>Near Field Communication (NFC)</title>
+
+</head>
+
+<body onload="prettyPrint()" style="overflow: auto;">
+
+
+<div id="toc-navigation">
+
+    <div id="profile">
+        <p><img alt="Mobile C#" src="../../images/mobile_s_cs.png" />
+        </p>
+    </div>
+
+
+    <div id="toc_border"><div id="toc">
+        <p class="toc-title">Dependencies</p>
+        <ul class="toc">
+            <li>Tizen 4.0 and Higher for Mobile</li>
+        </ul>
+
+        <p class="toc-title">Content</p>
+            <ul class="toc">
+                <li><a href="#card_emulation">Card Emulation</a></li>
+                <li><a href="#prerequisites">Prerequisites</a></li>
+                <li>NFC Management
+                    <ul class="toc">
+                        <li><a href="#manager_activating_nfc_device">Activating and Deactivating NFC</a></li>
+                        <li><a href="#manager_events">Monitoring NFC State Changes</a></li>
+                        <li><a href="#manager_get_adapters">Retrieving the Connection Adapter</a></li>
+                    </ul>
+                </li>
+                <li>NDEF Support
+                    <ul class="toc">
+                        <li><a href="#ndef_record">Creating NDEF Records and Messages</a></li>
+                    </ul>
+                </li>
+                <li>Tag Mode
+                    <ul class="toc">
+                        <li><a href="#tag_get_tag_event">Managing Tag Events</a></li>
+                        <li><a href="#tag_read_ndef">Reading and Writing NDEF Messages</a></li>
+                    </ul>
+                </li>
+                <li>P2P Mode
+                    <ul class="toc">
+                        <li><a href="#p2p_get_p2p_event">Managing P2P Events</a></li>
+                        <li><a href="#p2p_receive">Sending and Receiving NDEF Messages Between Peer Devices</a></li>
+                    </ul>
+                </li>
+                <li>Card Emulation Mode
+                    <ul class="toc">
+                        <li><a href="#ce_enable">Enabling and Disabling the Card Emulation Mode</a></li>
+                        <li><a href="#ce_register_aid">Registering AID</a></li>
+                        <li><a href="#ce_ohce_status">Managing Secure Element Events</a></li>
+                        <li><a href="#ce_hce_event">Managing HCE Events from the NFC Reader</a></li>
+                        <li><a href="#ce_hce_send_response">Sending a HCE Response to the NFC Reader</a></li>
+                    </ul>
+                </li>
+            </ul>
+
+        <p class="toc-title">Related Info</p>
+        <ul class="toc">
+            <li><a href="https://developer.tizen.org/dev-guide/csapi/namespaceTizen_1_1Network_1_1Nfc.html">Tizen.Network.Nfc Namespace</a></li>
+        </ul>
+    </div></div>
+</div>
+
+<div id="container"><div id="contents"><div class="content">
+
+    <h1>Near Field Communication (NFC)</h1>
+
+    <p>Tizen enables you to use Near Field Communication (NFC) functionalities, such as reading and writing tags, and emulating a smartcard. NFC is an international standard (ISO/IEC 18092) that specifies an interface and a protocol for simple wireless interconnection of closely coupled devices. For more information, see the <a href="http://nfc-forum.org/" target="blank">NFC Forum</a>.</p>
+
+    <p>The main features of the Tizen.Network.Nfc namespace include:</p>
+    <ul>
+        <li>NFC management
+            <p>You can <a href="#manager_activating_nfc_device">activate or deactivate NFC</a>, <a href="#manager_events">monitor NFC events</a>, and <a href="#manager_get_adapters">retrieve adapters for various NFC modes</a>.</p>
+        </li>
+        <li>NDEF support
+            <p>The NFC Data Exchange Format (NDEF) is a packet message format used in the reader/writer and peer-to-peer modes.</p>
+            <p>You can <a href="#ndef_record">create NDEF records and messages</a>.</p>
+        </li>
+        <li>Tag mode
+            <p>You can <a href="#tag_get_tag_event">manage tag events</a> and <a href="#tag_read_ndef">read and write NDEF messages</a> from and to an NFC tag.</p>
+        </li>
+        <li>Peer-to-peer mode
+            <p>You can <a href="#p2p_get_p2p_event">manage P2P events</a> and <a href="#p2p_receive">send and receive NDEF messages</a> between peer devices.</p>
+        </li>
+        <li>Card emulation mode
+            <p>The <a href="#card_emulation">card emulation</a> mode allows an NFC device to function as a smart card.</p>
+            <p>You can <a href="#ce_enable">enable and disable the card emulation mode</a>, <a href="#ce_register_aid">register an AID value</a>, <a href="#ce_ohce_status">manage secure element events</a>, <a href="#ce_hce_event">manage HCE events from the NFC reader</a>, and <a href="#ce_hce_send_response">send HCE responses to the NFC reader</a>.</p>
+        </li>
+    </ul>
+
+<h2 id="card_emulation">Card Emulation</h2>
+<p>The card emulation mode can be broadly divided into two categories, traditional card emulation and host card emulation (HCE):</p>
+        <ul>
+            <li>Traditional card emulation
+                <p>In traditional card emulation, the NFC controller in the device routes data from the NFC reader directly to the secure element (SE), and an applet handles all of the data in the SE. The user does not participate at all in the operation.</p>
+                <p>Tizen supports eSE and UICC as Secure Elements.</p>
+                <p align="center"><strong>Figure: Traditional card emulation</strong></p>
+                <p align="center"><img alt="Traditional card emulation" src="../../images/nfc_card_emulation.png" /></p>
+                </li>
+
+            <li>Host-based card emulation (HCE)
+                <p>HCE is an on-device technology that permits a phone to perform card emulation on an NFC-enabled device without relying on access to a secure element (SE). The data is routed to the user space on which Tizen applications reside, instead of routing the data to a secure element.</p>
+
+                <p align="center"><strong>Figure: Card emulation with HCE</strong></p>
+                <p align="center"><img alt="Card emulation with HCE" src="../../images/nfc_card_emulation_hce.png" /></p>
+                <p>HCE allows you to create your own card emulation system and bypass the SE. This approach brings 2 advantages:</p>
+                <ul>
+                    <li>For UICC Secure Elements, the mobile service provider is involved in the card emulation behavior. With HCE, you are independent of the service provider.</li>
+                    <li>You do not need SE hardware chips within the device.</li>
+                </ul>
+
+                <p>To understand HCE behavior, mainly how data is internally routed to the correct application, consider how Tizen handles NFC routing:</p>
+                <ol>
+                    <li>Assume that the user has an HCE application or installs one.
+                    <p>The application has an AID value, which is stored in NFC routing table (blue arrow in the following figure).</p>
+                    </li>
+                    <li>When the NFC reader attempts card emulation, the NFC controller checks the routing table to find the application to which the data is sent for emulation (red arrows in the following figure).</li>
+                    <li>When the application is uninstalled, the AID value is deleted from the routing table.</li>
+                </ol>
+
+                <p align="center"><strong>Figure: HCE routing</strong></p>
+                <p align="center"> <img alt="HCE routing" src="../../images/nfc_hce_routing.png" /> </p>
+            </li>
+        </ul>
+
+
+    <h2 id="prerequisites">Prerequisites</h2>
+    <p>To enable your application to use the NFC functionality:</p>
+    <ol>
+        <li>To use the <a href="https://developer.tizen.org/dev-guide/csapi/namespaceTizen_1_1Network_1_1Nfc.html">Tizen.Network.Nfc</a>
+        namespace, the application has to request permission by adding the following privileges to the <code>tizen-manifest.xml</code> file:
+<pre class="prettyprint">
+&lt;privileges&gt;
+   &lt;privilege&gt;http://tizen.org/privilege/nfc&lt;/privilege&gt;
+   &lt;privilege&gt;http://tizen.org/privilege/nfc.cardemulation&lt;/privilege&gt;
+&lt;/privileges&gt;
+</pre>
+        </li>
+        <li>To use the methods and properties of the <code>Tizen.Network.Nfc</code> namespace, include it in your application:
+<pre class="prettyprint">
+using Tizen.Network.Nfc;
+</pre>
+        </li>
+    </ol>
+
+
+    <h2 id="manager_activating_nfc_device">Activating and Deactivating NFC</h2>
+        <p>To activate and deactivate NFC:</p>
+        <ol>
+            <li>To use this NFC activation method, the application has to request permission by adding the following privileges to the <code>tizen-manifest.xml</code> file:
+<pre class="prettyprint">
+&lt;privileges&gt;
+   &lt;privilege&gt;http://tizen.org/privilege/nfc.admin&lt;/privilege&gt;
+&lt;/privileges&gt;
+</pre>
+            <div class="note">
+                <strong>Note</strong>
+                To be able to use this privilege, your application needs to be signed with a partner-level certificate.
+            </div>
+            </li>
+            <li>To activate NFC, use the <code>SetActivationAsync()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcManager.html">Tizen.Network.Nfc.NfcManager</a> class with the parameter <code>true</code>:
+<pre class="prettyprint">
+NfcManager.SetActivationAsync(true);
+</pre>
+            </li>
+            <li>To deactivate NFC, use the <code>SetActivationAsync()</code> method with the parameter <code>false</code>:
+<pre class="prettyprint">
+NfcManager.SetActivationAsync(false);
+</pre>
+            </li>
+        </ol>
+
+
+    <h2 id="manager_events">Monitoring NFC State Changes</h2>
+
+        <p>To monitor NFC state changes using event handlers:</p>
+        <ol>
+            <li>Define an event handler to be triggered when the NFC activation state changes:
+<pre class="prettyprint">
+public static void ActivationStatusChanged(object sender, ActivationChangedEventArgs e)
+{
+    isActivated = e.Activated;
+    flagStateChanged = true;
+}
+</pre>
+            </li>
+            <li>Register the event handler for the <code>ActivationChanged</code> event of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcManager.html">Tizen.Network.Nfc.NfcManager</a> class:
+<pre class="prettyprint">
+NfcManager.ActivationChanged += ActivationStatusChanged;
+</pre>
+            </li>
+            <li>When it is no longer needed, deregister the event handler:
+<pre class="prettyprint">
+NfcManager.ActivationChanged -= ActivationStatusChanged;
+</pre>
+            </li>
+        </ol>
+
+
+        <h2 id="manager_get_adapters">Retrieving the Connection Adapter</h2>
+    <p>Before any NFC operations, retrieve the appropriate connection adapter with the corresponding <code>GetXXXAdapter()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcManager.html">Tizen.Network.Nfc.NfcManager</a> class.</p>
+    <p>For example, to retrieve the NFC tag mode adapter, use the <code>GetTagAdapter()</code> method:</p>
+<pre class="prettyprint">
+public static void GetTagAdapter_RETURN_VALUE()
+{
+    try
+    {
+        /// Test code
+        Assert.IsInstanceOf&lt;NfcTagAdapter&gt;(NfcManager.GetTagAdapter(), "GetTagAdapter return value is not of the type NfcTagAdapter");
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(isNfcSupported == false || isTagSupported == false, "GetTagAdapter throws the NotSupportedException, but Tizen supports the NFC Tag operation");
+    }
+}
+</pre>
+
+
+    <h2 id="ndef_record">Creating NDEF Records and Messages</h2>
+<p>To create an NDEF record and attach it to an NDEF message:</p>
+    <ol>
+    <li>To create an NDEF record, create a new instance of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcNdefRecord.html">Tizen.Network.Nfc.NfcNdefRecord</a> class and give it the values required.
+    <p>In the following example, a record is created with 3 bytes of binary data as a payload.</p>
+<pre class="prettyprint">
+public static void AppendRecord_RETURN_VALUE()
+{
+    /// Test code
+    try
+    {
+        byte[] type = new byte[1]{0x02};
+        byte[] id = new byte[1]{0x01};
+        byte[] payload = new byte[3]{0x04, 0x05, 0x06};
+
+        <span class="highlight">NfcNdefRecord record = new NfcNdefRecord(NfcRecordTypeNameFormat.WellKnown, type, id, payload, 3);</span>
+</pre>
+<p>You can also create NDEF records to store different kinds of data, such as text or images, by using different parameters when creating the <code>Tizen.Network.Nfc.NfcNdefRecord</code> instance.</p>
+    </li>
+    <li>Create the NDEF message as a new instance of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcNdefMessage.html">Tizen.Network.Nfc.NfcNdefMessage</a> class and add the record to it using the <code>AppendRecord()</code> method:
+<pre class="prettyprint">
+
+        <span class="highlight">NfcNdefMessage msg = new NfcNdefMessage();</span>
+
+        int previousMsgRecordCount = msg.RecordCount;
+
+        <span class="highlight">bool success = msg.AppendRecord(record);</span>
+        Assert.IsTrue(success, "Failed to append record to NdefMessage");
+
+        int currentMsgRecordCount = msg.RecordCount;
+        Assert.IsTrue(previousMsgRecordCount + 1 == currentMsgRecordCount, "currentMsgRecordCount should be greater than previousMsgRecordCount");
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(isSupported == false, "Method throws the NotSupportedException, but Tizen supports the NFC operation");
+    }
+}
+</pre>
+        </li>
+        </ol>
+
+    <h2 id="tag_get_tag_event">Managing Tag Events</h2>
+    <p>To keep track of NFC tag events through event handlers:</p>
+    <ol>
+        <li>Define an event handler to trigger whenever a new NFC tag is found:
+<pre class="prettyprint">
+public static void TagDiscoveredCallback(object sender, TagDiscoveredEventArgs e)
+{
+    _eventArgs = e;
+}
+</pre>
+    </li>
+    <li>Register the event handler for the <code>TagDiscovered</code> event of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcTagAdapter.html">Tizen.Network.Nfc.NfcTagAdapter</a> class:
+<pre class="prettyprint">
+public static async Task TagDiscovered_EVENT_LISTEN()
+{
+    try
+    {
+        /// Precondition
+        /// 1. Retrieve the tag adapter
+        /// 2. Register the event handler
+
+        _tagAdapter = NfcManager.GetTagAdapter();
+        <span class="highlight">_tagAdapter.TagDiscovered += TagDiscoveredCallback;</span>
+
+        ///  Test code
+        await WaitTagDiscovered();
+</pre>
+    </li>
+    <li>When it is no longer needed, deregister the event handler.
+<pre class="prettyprint">
+        Assert.IsNotNull(_eventArgs, "_eventArgs should not be null");
+        Assert.IsNotNull(_eventArgs.Tag, "Tag should not be null");
+
+        /// Postcondition
+        /// 1. Unregister the event handler
+        /// 2. Reset variables
+
+        Assert.IsNotNull(_tagAdapter, "_tagAdapter should not be null");
+        <span class="highlight">_tagAdapter.TagDiscovered -= TagDiscoveredCallback;</span>
+
+        _tagAdapter = null;
+        _eventArgs = null;
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(_nfcFeature == false || _tagFeature == false,
+            "Method throws the NotSupportedException, but Tizen supports the NFC Tag operation");
+    }
+}
+</pre>
+    </li>
+</ol>
+
+<h2 id="tag_read_ndef">Reading and Writing NDEF Messages</h2>
+<p>To read NDEF messages from an NFC tag and write them to a tag:</p>
+<ul>
+<li>To read an NDEF message:
+<ol>
+<li>Retrieve the NFC tag adapter and register a <code>TagDiscovered</code> event handler for it:
+<pre class="prettyprint">
+public static async Task ReadNdefMessageAsync_METHOD_RETURN()
+{
+    try
+    {
+        /// Precondition
+        /// 1. Retrieve the tag adapter
+        /// 2. Register the event handler
+
+        _tagAdapter = NfcManager.GetTagAdapter();
+        _tagAdapter.TagDiscovered += TagDiscoveredCallback;
+</pre>
+</li>
+<li>Once the tag is discovered, read the message with the <code>ReadNdefMessageAsync()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcTag.html">Tizen.Network.Nfc.NfcTag</a> class:
+<pre class="prettyprint">
+        /// Test code
+        await WaitTagDiscovered();
+
+        Assert.IsNotNull(_eventArgs, "_eventArgs should not be null");
+        Assert.IsNotNull(_eventArgs.Tag, "Tag should not be null");
+
+        <span class="highlight">NfcNdefMessage ndefMessage = await _eventArgs.Tag.ReadNdefMessageAsync();</span>
+
+        Assert.IsTrue(ndefMessage.RecordCount &gt; 0, "RecordCount should be greater than 0");
+
+        /// Postcondition
+        /// 1. Unregister the event handler
+        /// 2. Reset variables
+
+        Assert.IsNotNull(_tagAdapter, "_tagAdapter should not be null");
+        _tagAdapter.TagDiscovered -= TagDiscoveredCallback;
+
+        _tagAdapter = null;
+        _eventArgs = null;
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(_nfcFeature == false || _tagFeature == false,
+                    "Method throws the NotSupportedException, but Tizen supports the NFC Tag operation");
+    }
+}
+</pre>
+</li>
+</ol>
+</li>
+<li>To write an NDEF message:
+<ol>
+<li>Retrieve the NFC tag adapter and register a <code>TagDiscovered</code> event handler for it:
+<pre class="prettyprint">
+public static async Task WriteNdefMessageAsync_METHOD_RETURN()
+{
+    try
+    {
+        /// Precondition
+        /// 1. Retrieve the tag adapter
+        /// 2. Register the event handler
+
+        _tagAdapter = NfcManager.GetTagAdapter();
+        _tagAdapter.TagDiscovered += TagDiscoveredCallback;
+</pre>
+</li>
+<li>Once the tag is discovered, create a new NDEF message as an instance of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcNdefMessage.html">Tizen.Network.Nfc.NfcNdefMessage</a> class and a new NDEF record as an instance of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcNdefRecord.html">Tizen.Network.Nfc.NfcNdefRecord</a> class. Then append the record into the message with the <code>AppendRecord()</code> method of the <code>Tizen.Network.Nfc.NfcNdefMessage</code> class:
+<pre class="prettyprint">
+        /// Test code
+        await WaitTagDiscovered();
+
+        Assert.IsNotNull(_eventArgs, "_eventArgs should not be null");
+        Assert.IsNotNull(_eventArgs.Tag, "Tag should not be null");
+
+        <span class="highlight">NfcNdefMessage ndefMessage = new NfcNdefMessage();</span>
+        <span class="highlight">NfcNdefRecord ndefRecord = new NfcNdefRecord("123", "KR", NfcEncodeType.Utf8);</span>
+        <span class="highlight">ndefMessage.AppendRecord(ndefRecord);</span>
+</pre>
+</li>
+<li>Write the message to the NFC tag with the <code>WriteNdefMessageAsync()</code> method of the <code>Tizen.Network.Nfc.NfcTag</code> class:
+<pre class="prettyprint">
+        <span class="highlight">NfcError nfcError = await _eventArgs.Tag.WriteNdefMessageAsync(ndefMessage);</span>
+
+        Assert.IsTrue(nfcError == NfcError.None, "nfcError should be no error");
+
+        /// Postcondition
+        /// 1. Unregister the event handler
+        /// 2. Reset variables
+
+        Assert.IsNotNull(_tagAdapter, "_tagAdapter should not be null");
+        _tagAdapter.TagDiscovered -= TagDiscoveredCallback;
+
+        _tagAdapter = null;
+        _eventArgs = null;
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(_nfcFeature == false || _tagFeature == false,
+            "Method throws the NotSupportedException, but Tizen supports the NFC Tag operation");
+    }
+}
+</pre>
+</li>
+</ol>
+</li>
+</ul>
+
+    <h2 id="p2p_get_p2p_event">Managing P2P Events</h2>
+    <p>You can keep track of P2P events through event handlers.</p>
+
+    <ol>
+<li>Define an event handler to trigger whenever a new P2P target has been found:
+<pre class="prettyprint">
+public static void P2pTargetDiscoveredCallback(object sender, P2pTargetDiscoveredEventArgs e)
+{
+    _p2pTargetDiscoveredEventArgs = e;
+}
+</pre>
+</li>
+<li>Register the event handler for the <code>P2pTargetDiscovered</code> event of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcP2pAdapter.html">Tizen.Network.Nfc.NfcP2pAdapter</a> class:
+<pre class="prettyprint">
+public static async Task P2pTargetDiscovered_EVENT_LISTEN()
+{
+    try
+    {
+        /// Precondition
+        /// 1. Retrieve the P2P adapter
+        /// 2. Register the event handler
+
+        _p2pAdapter = NfcManager.GetP2pAdapter();
+        <span class="highlight">_p2pAdapter.P2pTargetDiscovered += P2pTargetDiscoveredCallback;</span>
+
+        /// Test code
+        await WaitTargetDiscovered();
+</pre>
+</li>
+<li>When it is no longer needed, deregister the event handler:
+<pre class="prettyprint">
+        Assert.IsNotNull(_p2pTargetDiscoveredEventArgs, "P2pTargetDiscoveredEventArgs should not be null");
+
+        /// Postcondition
+        /// 1. Unregister event handler
+        /// 2. Reset variables
+
+        Assert.IsNotNull(_p2pAdapter, "_tagAdapter should not be null");
+        <span class="highlight">_p2pAdapter.P2pTargetDiscovered -= P2pTargetDiscoveredCallback;</span>
+
+        _p2pAdapter = null;
+        _p2pTargetDiscoveredEventArgs = null;
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(_nfcFeature == false || _p2pFeature == false,
+            "Method throws the NotSupportedException, but Tizen supports the NFC P2P operation");
+    }
+}
+</pre>
+</li>
+</ol>
+
+    <h2 id="p2p_receive">Sending and Receiving NDEF Messages Between Peer Devices</h2>
+    <p>You can both send and receive NDEF devices between peer devices in P2P mode.</p>
+    <ul>
+<li>To send an NDEF message to a peer device:
+<ol>
+<li>Retrieve the P2P adapter and register a <code>P2pTargetDiscovered</code> event handler for it:
+<pre class="prettyprint">
+public static async Task SendNdefMessageAsync_METHOD_RETURN()
+{
+    try
+    {
+        /// Precondition
+        /// 1. Retrieve the P2P adapter
+        /// 2. Register the event handler
+
+        <span class="highlight">_p2pAdapter = NfcManager.GetP2pAdapter();</span>
+        <span class="highlight">_p2pAdapter.P2pTargetDiscovered += P2pTargetDiscoveredCallback;</span>
+</pre>
+</li>
+<li>Create a new NDEF message as an instance of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcNdefMessage.html">Tizen.Network.Nfc.NfcNdefMessage</a> class and a new NDEF record as an instance of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcNdefRecord.html">Tizen.Network.Nfc.NfcNdefRecord</a> class. Then append the record into the message with the <code>AppendRecord()</code> method of the <code>Tizen.Network.Nfc.NfcNdefMessage</code> class.
+<pre class="prettyprint">
+        /// Test code
+        await WaitTargetDiscovered();
+
+        Assert.IsNotNull(_p2pTargetDiscoveredEventArgs, "P2pTargetDiscoveredEventArgs should not be null");
+
+        NfcP2p currentP2p = _p2pAdapter.GetConnectedTarget();
+
+        Assert.IsNotNull(currentP2p, "currentP2p should not be null");
+
+        <span class="highlight">NfcNdefMessage ndefMessage = new NfcNdefMessage();</span>
+        <span class="highlight">NfcNdefRecord ndefRecord = new NfcNdefRecord("123", "KR", NfcEncodeType.Utf8);</span>
+        <span class="highlight">ndefMessage.AppendRecord(ndefRecord);</span>
+</pre>
+</li>
+<li>Send the NDEF message with the <code>SendNdefMessageAsync()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcP2p.html">Tizen.Network.Nfc.NfcP2p</a> class:
+<pre class="prettyprint">
+        <span class="highlight">NfcError nfcError = await currentP2p.SendNdefMessageAsync(ndefMessage);</span>
+
+        Assert.IsTrue(nfcError == NfcError.None, "nfcError should be no error");
+
+        /// Postcondition
+        /// 1. Unregister the event handler
+        /// 2. Reset variables
+
+        Assert.IsNotNull(_p2pAdapter, "_tagAdapter should not be null");
+        _p2pAdapter.P2pTargetDiscovered -= P2pTargetDiscoveredCallback;
+
+        _p2pAdapter = null;
+        _p2pTargetDiscoveredEventArgs = null;
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(_nfcFeature == false || _p2pFeature == false,
+            "Method throws the NotSupportedException, but Tizen supports the NFC P2P operation");
+    }
+}
+</pre>
+</li>
+</ol>
+</li>
+    <li>To receive an NDEF message from a peer device:
+    <ol>
+<li>Define an event handler to trigger whenever P2P data is received:
+<pre class="prettyprint">
+public static void P2pDataReceivedCallback(object sender, P2pDataReceivedEventArgs e)
+{
+    _p2pDataReceivedEventArgs = e;
+}
+</pre>
+</li>
+<li>Once a P2P connection has been made, retrieve the connected P2P target with the <code>GetConnectedTarget()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcP2pAdapter.html">Tizen.Network.Nfc.NfcP2pAdapter</a> class, and add the new event handler for the <code>P2pDataReceived</code> event of the newly-created instance of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcP2p.html">Tizen.Network.Nfc.NfcP2p</a> class:
+<pre class="prettyprint">
+public static async Task NdefMessage_READ_ONLY()
+{
+    try
+    {
+        /// Precondition
+        /// 1. Retrieve the P2P adapter
+        /// 2. Register the event handler
+
+        _p2pAdapter = NfcManager.GetP2pAdapter();
+        _p2pAdapter.P2pTargetDiscovered += P2pTargetDiscoveredCallback;
+
+        /// Test code
+        await WaitTargetDiscovered();
+
+        Assert.IsNotNull(_p2pTargetDiscoveredEventArgs, "P2pTargetDiscoveredEventArgs should not be null");
+        <span class="highlight">NfcP2p p2pTarget = _p2pAdapter.GetConnectedTarget();</span>
+        <span class="highlight">p2pTarget.P2pDataReceived += P2pDataReceivedCallback;</span>
+</pre>
+</li>
+<li>The received NDEF message can be read through the newly-created instance of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1P2pDataReceivedEventArgs.html">Tizen.Network.Nfc.P2pDataReceivedEventArgs</a> class:
+<pre class="prettyprint">
+        await WaitDataReceived();
+
+        Assert.IsNotNull(_p2pDataReceivedEventArgs, "P2pDataReceivedEventArgs should not be null");
+
+        <span class="highlight">Assert.IsInstanceOf&lt;NfcNdefMessage&gt;(_p2pDataReceivedEventArgs.NdefMessage, "NdefMessage value is not of the type NfcNdefMessage");</span>
+
+        /// Postcondition
+        /// 1. Unregister event handlers
+        /// 2. Reset variables
+
+        Assert.IsNotNull(p2pTarget, "p2pTarget should not be null");
+        p2pTarget.P2pDataReceived -= P2pDataReceivedCallback;
+        Assert.IsNotNull(_p2pAdapter, "_p2pAdapter should not be null");
+        _p2pAdapter.P2pTargetDiscovered -= P2pTargetDiscoveredCallback;
+
+        _p2pAdapter = null;
+        _p2pTargetDiscoveredEventArgs = null;
+        _p2pDataReceivedEventArgs = null;
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(_nfcFeature == false || _p2pFeature == false,
+            "Method throws the NotSupportedException, but Tizen supports the Nfc P2P operation");
+    }
+}
+</pre>
+</li>
+</ol>
+</li>
+</ul>
+
+    <h2 id="ce_enable">Enabling and Disabling the Card Emulation Mode</h2>
+<p>To enable and disable the smart card emulation mode:</p>
+<ol>
+    <li>Retrieve the card emulation adapter with the <code>GetCardEmulationAdapter()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcManager.html">Tizen.Network.Nfc.NfcManager</a> class:
+<pre class="prettyprint">
+public static void EnableCardEmulation_CHECK_CARD_EMULATION_STATUS()
+{
+    try
+    {
+        NfcCardEmulationAdapter ceAdapter = NfcManager.GetCardEmulationAdapter();
+</pre>
+</li>
+<li>Enable and disable the card emulation mode with the <code>EnableCardEmulation()</code> and <code>DisableCardEmulation()</code> methods of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcCardEmulationAdapter.html">Tizen.Network.Nfc.NfcCardEmulationAdapter</a> class:
+<pre class="prettyprint">
+        <span class="highlight">ceAdapter.DisableCardEmulation();</span>
+
+        <span class="highlight">ceAdapter.EnableCardEmulation();</span>
+
+        NfcSecureElementCardEmulationMode ceMode = ceAdapter.GetCardEmulationMode();
+        Assert.IsTrue(ceMode == NfcSecureElementCardEmulationMode.On, "The card emulation mode should be true.");
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(isNfcSupported == false || isCeSupported == false, "Method throws the NotSupportedException, but Tizen supports the NFC operation");
+    }
+}
+</pre>
+</li>
+</ol>
+
+    <h2 id="ce_register_aid">Registering AID</h2>
+    <p>To register an AID value:</p>
+
+<ol>
+    <li>Retrieve the card emulation adapter with the <code>GetCardEmulationAdapter()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcManager.html">Tizen.Network.Nfc.NfcManager</a> class:
+<pre class="prettyprint">
+public static void RegisterAid_METHOD_CALL_WITH_HCE_PAYMENT()
+{
+    try
+    {
+        NfcCardEmulationAdapter ceAdapter = NfcManager.GetCardEmulationAdapter();
+</pre>
+</li>
+<li>Register the AID with the <code>RegisterAid()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcCardEmulationAdapter.html">Tizen.Network.Nfc.NfcCardEmulationAdapter</a> class:
+<pre class="prettyprint">
+        <span class="highlight">ceAdapter.RegisterAid(NfcSecureElementType.Hce, NfcCardEmulationCategoryType.Payment, "325041592E5359532E4444463031");</span>
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(isNfcSupported == false || isHceSupported == false, "Method throws the NotSupportedException, but Tizen supports the NFC operation");
+    }
+}
+</pre>
+</li>
+</ol>
+
+    <h2 id="ce_ohce_status">Managing Secure Element Events</h2>
+    <p>To manage Secure Element events:</p>
+    <ol>
+    <li>Define an event handler to be triggered when a Secure Element event occurs:
+<pre class="prettyprint">
+public static void SecureElementCallback(object sender, SecureElementEventArgs e)
+{
+    _seEventArgs = e;
+}
+</pre>
+</li>
+<li>Retrieve the card emulation adapter with the <code>GetCardEmulationAdapter()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcManager.html">Tizen.Network.Nfc.NfcManager</a> class, and register the Secure Element event handler for its <code>SecureElementEvent</code> event:
+<pre class="prettyprint">
+public static async Task SecureElementEvent()
+{
+    try
+    {
+        /// Precondition
+        /// 1. Retrieve the card emulation adapter
+        /// 2. Register the SE event handler
+
+        <span class="highlight">_cardEmulationAdapter = NfcManager.GetCardEmulationAdapter();</span>
+        <span class="highlight">_cardEmulationAdapter.SecureElementEvent += SecureElementCallback;</span>
+
+        /// Test code
+        await WaitSeEvent();
+
+        Assert.IsNotNull(_seEventArgs, "The _seEventArgs should not be null when HCE event callback function is called");
+        Assert.IsTrue(_checkFlag == true, "isChecked should be true");
+</pre>
+</li>
+<li>When it is no longer needed, deregister the event handler:
+<pre class="prettyprint">
+        /// Postcondition
+        /// 1. Unregister the SE event handler
+        /// 2. Reset variables
+
+        Assert.IsNotNull(_cardEmulationAdapter, "_cardEmulationAdapter should not be null");
+        <span class="highlight">_cardEmulationAdapter.SecureElementEvent -= SecureElementCallback;</span>
+
+        _cardEmulationAdapter = null;
+        _seEventArgs = null;
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(_nfcFeature == false || _ceFeature == false || _hceFeature == false,
+            "Method throws the NotSupportedException, but Tizen supports the NFC card emulation operation");
+    }
+}
+</pre>
+</li>
+</ol>
+
+        <h2 id="ce_hce_event">Managing HCE Events from the NFC Reader</h2>
+<p>To manage HCE events from the NFC reader:</p>
+<ol>
+    <li>Define an event handler to be triggered when a HCE event occurs:
+<pre class="prettyprint">
+public static void HostCardEmulationCallback(object sender, HostCardEmulationEventArgs e)
+{
+    _hceEventArgs = e;
+}
+</pre>
+</li>
+<li>Retrieve the card emulation adapter with the <code>GetCardEmulationAdapter()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcManager.html">Tizen.Network.Nfc.NfcManager</a> class, and register the HCE event handler for its <code>HostCardEmulationEvent</code> event:
+<pre class="prettyprint">
+public static async Task HostCardEmulationEvent_HOST_CARD_EMULATION_EVENT_LISTEN_TYPE_ACTIVATED()
+{
+    try
+    {
+        /// Precondition
+        /// 1. Retrieve the card emulation adapter
+        /// 2. Register the HCE event handler
+
+        <span class="highlight">_cardEmulationAdapter = NfcManager.GetCardEmulationAdapter();</span>
+        <span class="highlight">_cardEmulationAdapter.HostCardEmulationEvent += HostCardEmulationCallback;</span>
+
+        /// Test code
+        await WaitHceEventActivated();
+
+        Assert.IsNotNull(_hceEventArgs, "The _hceEventArgs should not be null when HCE event callback function is called");
+        Assert.IsTrue(_checkFlag == true, "isChecked should be true");
+</pre>
+</li>
+<li>When it is no longer needed, deregister the event handler:
+<pre class="prettyprint">
+        /// Postcondition
+        /// 1. Unregister the HCE event handler
+        /// 2. Reset variables
+
+        Assert.IsNotNull(_cardEmulationAdapter, "_cardEmulationAdapter should not be null");
+        <span class="highlight">_cardEmulationAdapter.HostCardEmulationEvent -= HostCardEmulationCallback;</span>
+
+        _cardEmulationAdapter = null;
+        _hceEventArgs = null;
+        _seEventArgs = null;
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(_nfcFeature == false || _ceFeature == false || _hceFeature == false,
+            "Method throws the NotSupportedException, but Tizen supports the NFC host card emulation operation");
+    }
+}
+</pre>
+</li>
+</ol>
+
+    <h2 id="ce_hce_send_response">Sending a HCE Response to the NFC Reader</h2>
+<p>To send HCE responses to the NFC reader:</p>
+<ol>
+<li>Retrieve the card emulation adapter with the <code>GetCardEmulationAdapter()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcManager.html">Tizen.Network.Nfc.NfcManager</a> class, and register the HCE event handler for its <code>HostCardEmulationEvent</code> event:
+<pre class="prettyprint">
+public static async Task HceSendApduResponse_METHOD_RETURN()
+{
+    try
+    {
+        /// Precondition
+        /// 1. Retrieve the card emulation adapter
+        /// 2. Register the HCE event handler
+
+        <span class="highlight">_cardEmulationAdapter = NfcManager.GetCardEmulationAdapter();</span>
+        <span class="highlight">_cardEmulationAdapter.HostCardEmulationEvent += HostCardEmulationCallback;</span>
+</pre>
+</li>
+<li>Once the HCE event triggers, send the response with the <code>HceSendApduResponse()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Network_1_1Nfc_1_1NfcSecureElement.html">Tizen.Network.Nfc.NfcSecureElement</a> class:
+<pre class="prettyprint">
+        /// Test code
+
+        byte[] responseBuffer = {0x90, 0x00};
+
+        await WaitHceEvent();
+
+        Assert.IsNotNull(_eventArgs, "The _eventArgs should not be null when Hce event callback function is called");
+        Assert.IsNotNull(_eventArgs.SecureElement, "The SecureElement property of _eventArgs should not be null when HCE event callback function is called");
+        Assert.IsInstanceOf&lt;NfcSecureElement&gt;(_eventArgs.SecureElement, "SecureElement value is not of the type NfcSecureElement");
+
+        <span class="highlight">_eventArgs.SecureElement.HceSendApduResponse(responseBuffer, 2);</span>
+
+        /// Postcondition
+        /// 1. Unregister the HCE event handler
+
+        _cardEmulationAdapter.HostCardEmulationEvent -= HostCardEmulationCallback;
+    }
+    catch (NotSupportedException)
+    {
+        Assert.IsTrue(_nfcFeature == false || _ceFeature == false || _hceFeature == false,
+            "Method throws the NotSupportedException, but Tizen supports the NFC host card emulation operation");
+    }
+}
+</pre>
+</li>
+</ol>
+
+
+<script type="text/javascript" src="../../scripts/jquery.zclip.min.js"></script>
+<script type="text/javascript" src="../../scripts/showhide.js"></script>
+
+
+<a class="top sms" href="#"><img src="../../images/btn_top.gif" alt="Go to top" /></a>
+
+<div id="footer">
+<p class="footer">Except as noted, this content - excluding the Code Examples - is licensed under <a href="http://creativecommons.org/licenses/by/3.0/legalcode" target="_blank">Creative Commons Attribution 3.0</a> and all of the Code Examples contained herein are licensed under <a href="https://www.tizen.org/bsd-3-clause-license" target="_blank">BSD-3-Clause</a>.<br/>For details, see the <a href="https://www.tizen.org/content-license" target="_blank">Content License</a>.</p>
+</div>
+
+<script type="text/javascript">
+var _gaq = _gaq || [];
+_gaq.push(['_setAccount', 'UA-25976949-1']);
+_gaq.push(['_trackPageview']);
+(function() {
+var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+})();
+</script>
+
+
+</div></div></div></body></html>