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;
20 using System.Threading.Tasks;
22 namespace Tizen.Network.Nfc
25 /// A class for managing the Tag information.
27 public class NfcTag : IDisposable
29 private bool disposed = false;
30 private IntPtr _tagHandle = IntPtr.Zero;
33 /// The type of NFC tag.
35 public NfcTagType Type
40 int ret = Interop.Nfc.Tag.GetType(_tagHandle, out type);
41 if (ret != (int)NfcError.None)
43 Log.Error(Globals.LogTag, "Failed to get tag type, Error - " + (NfcError)ret);
45 return (NfcTagType)type;
50 /// Whether the given NFC tag supports NDEF messages.
52 public bool IsSupportNdef
57 int ret = Interop.Nfc.Tag.IsSupportNdef(_tagHandle, out isSupport);
58 if (ret != (int)NfcError.None)
60 Log.Error(Globals.LogTag, "Failed to get support state, Error - " + (NfcError)ret);
68 /// The maximum NDEF message size that can be stored in NFC tag.
70 public uint MaximumNdefSize
75 int ret = Interop.Nfc.Tag.GetMaximumNdefSize(_tagHandle, out maxSize);
76 if (ret != (int)NfcError.None)
78 Log.Error(Globals.LogTag, "Failed to get max ndef size, Error - " + (NfcError)ret);
85 /// The size of NDEF message that stored in the tag.
92 int ret = Interop.Nfc.Tag.GetNdefSize(_tagHandle, out ndefSize);
93 if (ret != (int)NfcError.None)
95 Log.Error(Globals.LogTag, "Failed to get ndef size, Error - " + (NfcError)ret);
101 internal NfcTag(IntPtr handle)
111 public void Dispose()
114 GC.SuppressFinalize(this);
117 private void Dispose(bool disposing)
124 // Free managed objects.
126 //Free unmanaged objects
131 /// Retrieves all tag information.
133 /// <returns>List of NfcTagInformation objects.</returns>
134 public IEnumerable<NfcTagInformation> ForeachInformation()
136 List<NfcTagInformation> infoList = new List<NfcTagInformation>();
137 Interop.Nfc.TagInformationCallback callback = (IntPtr key, IntPtr infoValue, int valueSize, IntPtr userData) =>
139 if (key != IntPtr.Zero && infoValue != IntPtr.Zero)
141 NfcTagInformation tagInfo = new NfcTagInformation(Marshal.PtrToStringAnsi(key), new byte[valueSize]);
143 Marshal.Copy(infoValue, tagInfo.InformationValue, 0, valueSize);
145 infoList.Add(tagInfo);
152 int ret = Interop.Nfc.Tag.ForeachInformation(_tagHandle, callback, IntPtr.Zero);
153 if (ret != (int)NfcError.None)
155 Log.Error(Globals.LogTag, "Failed to get all Tag information, Error - " + (NfcError)ret);
156 NfcErrorFactory.ThrowNfcException(ret);
163 /// Transceives the data of the raw format card.
165 /// <param name="buffer">The binary data for parameter or additional commands.</param>
166 public Task<byte[]> TransceiveAsync(byte[] buffer)
168 var task = new TaskCompletionSource<byte[]>();
170 byte[] resultBuffer = null;
171 Interop.Nfc.TagTransceiveCompletedCallback callback = (int result, IntPtr resultData, int dataSize, IntPtr userData) =>
173 if (result == (int)NfcError.None)
175 resultBuffer = new byte[dataSize];
176 Marshal.Copy(resultData, resultBuffer, 0, dataSize);
177 task.SetResult(resultBuffer);
182 int ret = Interop.Nfc.Tag.Transceive(_tagHandle, buffer, buffer.Length, callback, IntPtr.Zero);
183 if (ret != (int)NfcError.None)
185 Log.Error(Globals.LogTag, "Failed to transceive data, Error - " + (NfcError)ret);
186 NfcErrorFactory.ThrowNfcException(ret);
193 /// Reads NDEF formatted data from NFC tag.
195 public Task<NfcNdefMessage> ReadNdefMessageAsync()
197 var task = new TaskCompletionSource<NfcNdefMessage>();
199 NfcNdefMessage ndefMsg = null;
200 Interop.Nfc.TagReadCompletedCallback callback = (int result, IntPtr ndefMessage, IntPtr userData) =>
202 if (result == (int)NfcError.None)
204 ndefMsg = new NfcNdefMessage(ndefMessage);
205 task.SetResult(ndefMsg);
212 int ret = Interop.Nfc.Tag.ReadNdef(_tagHandle, callback, IntPtr.Zero);
213 if (ret != (int)NfcError.None)
215 Log.Error(Globals.LogTag, "Failed to read ndef message, Error - " + (NfcError)ret);
216 NfcErrorFactory.ThrowNfcException(ret);
223 /// Writes NDEF formatted data.
225 /// <param name="ndefMessage">The NfcNdefMessage object.</param>
226 public Task<NfcError> WriteNdefMessageAsync(NfcNdefMessage ndefMessage)
228 var task = new TaskCompletionSource<NfcError>();
230 Interop.Nfc.VoidCallback callback = (int result, IntPtr userData) =>
232 task.SetResult((NfcError)result);
236 int ret = Interop.Nfc.Tag.WriteNdef(_tagHandle, ndefMessage.GetHandle(), callback, IntPtr.Zero);
237 if (ret != (int)NfcError.None)
239 Log.Error(Globals.LogTag, "Failed to write ndef message, Error - " + (NfcError)ret);
240 NfcErrorFactory.ThrowNfcException(ret);
247 /// Formats the detected tag that can store NDEF message.
249 /// <param name="keyValue">The key value that may need to format the tag.</param>
250 public Task<NfcError> FormatNdefMessageAsync(byte[] keyValue)
252 var task = new TaskCompletionSource<NfcError>();
254 Interop.Nfc.VoidCallback callback = (int result, IntPtr userData) =>
256 task.SetResult((NfcError)result);
260 int ret = Interop.Nfc.Tag.FormatNdef(_tagHandle, keyValue, keyValue.Length, callback, IntPtr.Zero);
261 if (ret != (int)NfcError.None)
263 Log.Error(Globals.LogTag, "Failed to format ndef message, Error - " + (NfcError)ret);
264 NfcErrorFactory.ThrowNfcException(ret);