2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Runtime.InteropServices;
19 using System.Collections.Generic;
21 namespace Tizen.Network.Nfc
24 /// The class for the NFC tag mode. It allows applications to handle the Tag informations.
26 /// <since_tizen> 3 </since_tizen>
27 /// <privilege>http://tizen.org/privilege/nfc</privilege>
28 public class NfcTagAdapter : IDisposable
31 private bool disposed = false;
33 private event EventHandler<TagDiscoveredEventArgs> _tagDiscovered;
35 private Interop.Nfc.TagDiscoveredCallback _tagDiscoveredCallback;
38 /// The event for receiving the tag discovered notification.
40 /// <since_tizen> 3 </since_tizen>
41 public event EventHandler<TagDiscoveredEventArgs> TagDiscovered
45 _tagDiscovered += value;
49 _tagDiscovered -= value;
53 internal NfcTagAdapter()
55 RegisterTagDiscoveredEvent();
59 /// NfcTagAdapter destructor.
72 GC.SuppressFinalize(this);
75 private void Dispose(bool disposing)
82 // Free managed objects.
83 UnregisterTagDiscoveredEvent();
85 //Free unmanaged objects
90 /// Gets the current connected tag.
92 /// <since_tizen> 3 </since_tizen>
93 /// <returns>The NfcTag object.</returns>
94 /// <privilege>http://tizen.org/privilege/nfc</privilege>
95 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
96 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
97 public NfcTag GetConnectedTag()
99 IntPtr tagHandle = IntPtr.Zero;
100 int ret = Interop.Nfc.GetConnectedTag(out tagHandle);
102 if (ret != (int)NfcError.None)
104 Log.Error(Globals.LogTag, "Failed to get connected tag, Error - " + (NfcError)ret);
105 NfcErrorFactory.ThrowNfcException(ret);
107 _tag = new NfcTag(tagHandle);
112 private void RegisterTagDiscoveredEvent()
114 _tagDiscoveredCallback = (int type, IntPtr tagHandle, IntPtr userData) =>
116 NfcDiscoveredType tagType = (NfcDiscoveredType)type;
117 TagDiscoveredEventArgs e = new TagDiscoveredEventArgs(tagType, tagHandle);
118 _tagDiscovered.SafeInvoke(null, e);
121 int ret = Interop.Nfc.SetTagDiscoveredCallback(_tagDiscoveredCallback, IntPtr.Zero);
122 if (ret != (int)NfcError.None)
124 Log.Error(Globals.LogTag, "Failed to set tag discovered callback, Error - " + (NfcError)ret);
128 private void UnregisterTagDiscoveredEvent()
130 Interop.Nfc.UnsetTagDiscoveredCallback();