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 /// <since_tizen> 3 </since_tizen>
28 public class NfcTag : IDisposable
30 private bool disposed = false;
31 private IntPtr _tagHandle = IntPtr.Zero;
34 /// The type of NFC tag.
36 /// <since_tizen> 3 </since_tizen>
37 public NfcTagType Type
42 int ret = Interop.Nfc.Tag.GetType(_tagHandle, out type);
43 if (ret != (int)NfcError.None)
45 Log.Error(Globals.LogTag, "Failed to get tag type, Error - " + (NfcError)ret);
47 return (NfcTagType)type;
52 /// Whether the given NFC tag supports NDEF messages.
54 /// <since_tizen> 3 </since_tizen>
55 public bool IsSupportNdef
60 int ret = Interop.Nfc.Tag.IsSupportNdef(_tagHandle, out isSupport);
61 if (ret != (int)NfcError.None)
63 Log.Error(Globals.LogTag, "Failed to get support state, Error - " + (NfcError)ret);
71 /// The maximum NDEF message size that can be stored in NFC tag.
73 /// <since_tizen> 3 </since_tizen>
74 public uint MaximumNdefSize
79 int ret = Interop.Nfc.Tag.GetMaximumNdefSize(_tagHandle, out maxSize);
80 if (ret != (int)NfcError.None)
82 Log.Error(Globals.LogTag, "Failed to get max ndef size, Error - " + (NfcError)ret);
89 /// The size of NDEF message that stored in the tag.
91 /// <since_tizen> 3 </since_tizen>
97 int ret = Interop.Nfc.Tag.GetNdefSize(_tagHandle, out ndefSize);
98 if (ret != (int)NfcError.None)
100 Log.Error(Globals.LogTag, "Failed to get ndef size, Error - " + (NfcError)ret);
106 internal NfcTag(IntPtr handle)
116 public void Dispose()
119 GC.SuppressFinalize(this);
122 private void Dispose(bool disposing)
129 // Free managed objects.
131 //Free unmanaged objects
136 /// Retrieves all tag information.
138 /// <since_tizen> 3 </since_tizen>
139 /// <returns>List of NfcTagInformation objects.</returns>
140 /// <privilege>http://tizen.org/privilege/nfc</privilege>
141 /// <exception cref="NotSupportedException">Thrown when Nfc is not supported.</exception>
142 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
143 public IEnumerable<NfcTagInformation> ForeachInformation()
145 List<NfcTagInformation> infoList = new List<NfcTagInformation>();
146 Interop.Nfc.TagInformationCallback callback = (IntPtr key, IntPtr infoValue, int valueSize, IntPtr userData) =>
148 if (key != IntPtr.Zero && infoValue != IntPtr.Zero)
150 NfcTagInformation tagInfo = new NfcTagInformation(Marshal.PtrToStringAnsi(key), new byte[valueSize]);
152 Marshal.Copy(infoValue, tagInfo.InformationValue, 0, valueSize);
154 infoList.Add(tagInfo);
161 int ret = Interop.Nfc.Tag.ForeachInformation(_tagHandle, callback, IntPtr.Zero);
162 if (ret != (int)NfcError.None)
164 Log.Error(Globals.LogTag, "Failed to get all Tag information, Error - " + (NfcError)ret);
165 NfcErrorFactory.ThrowNfcException(ret);
172 /// Transceives the data of the raw format card.
174 /// <since_tizen> 3 </since_tizen>
175 /// <param name="buffer">The binary data for parameter or additional commands.</param>
176 /// <privilege>http://tizen.org/privilege/nfc</privilege>
177 /// <exception cref="NotSupportedException">Thrown when Nfc is not supported.</exception>
178 /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
179 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
180 public Task<byte[]> TransceiveAsync(byte[] buffer)
182 var task = new TaskCompletionSource<byte[]>();
184 byte[] resultBuffer = null;
185 Interop.Nfc.TagTransceiveCompletedCallback callback = (int result, IntPtr resultData, int dataSize, IntPtr userData) =>
187 if (result == (int)NfcError.None)
189 resultBuffer = new byte[dataSize];
190 Marshal.Copy(resultData, resultBuffer, 0, dataSize);
191 task.SetResult(resultBuffer);
196 int ret = Interop.Nfc.Tag.Transceive(_tagHandle, buffer, buffer.Length, callback, IntPtr.Zero);
197 if (ret != (int)NfcError.None)
199 Log.Error(Globals.LogTag, "Failed to transceive data, Error - " + (NfcError)ret);
200 NfcErrorFactory.ThrowNfcException(ret);
207 /// Reads NDEF formatted data from NFC tag.
209 /// <since_tizen> 3 </since_tizen>
210 /// <privilege>http://tizen.org/privilege/nfc</privilege>
211 /// <exception cref="NotSupportedException">Thrown when Nfc is not supported.</exception>
212 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
213 public Task<NfcNdefMessage> ReadNdefMessageAsync()
215 var task = new TaskCompletionSource<NfcNdefMessage>();
217 NfcNdefMessage ndefMsg = null;
218 Interop.Nfc.TagReadCompletedCallback callback = (int result, IntPtr ndefMessage, IntPtr userData) =>
220 if (result == (int)NfcError.None)
222 ndefMsg = new NfcNdefMessage(ndefMessage);
223 task.SetResult(ndefMsg);
230 int ret = Interop.Nfc.Tag.ReadNdef(_tagHandle, callback, IntPtr.Zero);
231 if (ret != (int)NfcError.None)
233 Log.Error(Globals.LogTag, "Failed to read ndef message, Error - " + (NfcError)ret);
234 NfcErrorFactory.ThrowNfcException(ret);
241 /// Writes NDEF formatted data.
243 /// <since_tizen> 3 </since_tizen>
244 /// <param name="ndefMessage">The NfcNdefMessage object.</param>
245 /// <privilege>http://tizen.org/privilege/nfc</privilege>
246 /// <exception cref="NotSupportedException">Thrown when Nfc is not supported.</exception>
247 /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
248 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
249 public Task<NfcError> WriteNdefMessageAsync(NfcNdefMessage ndefMessage)
251 var task = new TaskCompletionSource<NfcError>();
253 Interop.Nfc.VoidCallback callback = (int result, IntPtr userData) =>
255 task.SetResult((NfcError)result);
259 int ret = Interop.Nfc.Tag.WriteNdef(_tagHandle, ndefMessage.GetHandle(), callback, IntPtr.Zero);
260 if (ret != (int)NfcError.None)
262 Log.Error(Globals.LogTag, "Failed to write ndef message, Error - " + (NfcError)ret);
263 NfcErrorFactory.ThrowNfcException(ret);
270 /// Formats the detected tag that can store NDEF message.
272 /// <since_tizen> 3 </since_tizen>
273 /// <param name="keyValue">The key value that may need to format the tag.</param>
274 /// <privilege>http://tizen.org/privilege/nfc</privilege>
275 /// <exception cref="NotSupportedException">Thrown when Nfc is not supported.</exception>
276 /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
277 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
278 public Task<NfcError> FormatNdefMessageAsync(byte[] keyValue)
280 var task = new TaskCompletionSource<NfcError>();
282 Interop.Nfc.VoidCallback callback = (int result, IntPtr userData) =>
284 task.SetResult((NfcError)result);
288 int ret = Interop.Nfc.Tag.FormatNdef(_tagHandle, keyValue, keyValue.Length, callback, IntPtr.Zero);
289 if (ret != (int)NfcError.None)
291 Log.Error(Globals.LogTag, "Failed to format ndef message, Error - " + (NfcError)ret);
292 NfcErrorFactory.ThrowNfcException(ret);