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 /// A class for the NDEF Record information. It allows applications to use the NDEF Record information.
26 /// <since_tizen> 3 </since_tizen>
27 public class NfcNdefRecord : IDisposable
29 private bool disposed = false;
30 private IntPtr _recordHandle = IntPtr.Zero;
35 /// <since_tizen> 3 </since_tizen>
42 int ret = Interop.Nfc.NdefRecord.GetId(_recordHandle, out id, out idLength);
43 if (ret != (int)NfcError.None)
45 Log.Error(Globals.LogTag, "Failed to get id, Error - " + (NfcError)ret);
49 return NfcConvertUtil.IntLengthIntPtrToByteArray(id, idLength);
54 /// The record payload.
56 /// <since_tizen> 3 </since_tizen>
63 int ret = Interop.Nfc.NdefRecord.GetPayload(_recordHandle, out payload, out payloadLength);
64 if (ret != (int)NfcError.None)
66 Log.Error(Globals.LogTag, "Failed to get payload, Error - " + (NfcError)ret);
70 return NfcConvertUtil.UintLengthIntPtrToByteArray(payload, payloadLength);
77 /// <since_tizen> 3 </since_tizen>
84 int ret = Interop.Nfc.NdefRecord.GetType(_recordHandle, out type, out typeSize);
85 if (ret != (int)NfcError.None)
87 Log.Error(Globals.LogTag, "Failed to get payload, Error - " + (NfcError)ret);
91 return NfcConvertUtil.IntLengthIntPtrToByteArray(type, typeSize);
96 /// The record TNF (Type Name Format) value.
98 /// <since_tizen> 3 </since_tizen>
99 public NfcRecordTypeNameFormat Tnf
104 int ret = Interop.Nfc.NdefRecord.GetTnf(_recordHandle, out tnf);
105 if (ret != (int)NfcError.None)
107 Log.Error(Globals.LogTag, "Failed to get tnf, Error - " + (NfcError)ret);
109 return (NfcRecordTypeNameFormat)tnf;
114 /// The text of the text type NDEF record.
116 /// <since_tizen> 3 </since_tizen>
122 int ret = Interop.Nfc.NdefRecord.GetText(_recordHandle, out text);
123 if (ret != (int)NfcError.None)
125 Log.Error(Globals.LogTag, "Failed to get text, Error - " + (NfcError)ret);
132 /// The language code of the text type NDEF record.
134 /// <since_tizen> 3 </since_tizen>
135 public string LanguageCode
140 int ret = Interop.Nfc.NdefRecord.GetLanguageCode(_recordHandle, out languageCode);
141 if (ret != (int)NfcError.None)
143 Log.Error(Globals.LogTag, "Failed to get language code, Error - " + (NfcError)ret);
150 /// The encoding type of the text type NDEF record.
152 /// <since_tizen> 3 </since_tizen>
153 public NfcEncodeType EncodeType
158 int ret = Interop.Nfc.NdefRecord.GetEncodeType(_recordHandle, out encodeType);
159 if (ret != (int)NfcError.None)
161 Log.Error(Globals.LogTag, "Failed to get encode type, Error - " + (NfcError)ret);
163 return (NfcEncodeType)encodeType;
168 /// The URI of the URI type NDEF record.
170 /// <since_tizen> 3 </since_tizen>
176 int ret = Interop.Nfc.NdefRecord.GetUri(_recordHandle, out uri);
177 if (ret != (int)NfcError.None)
179 Log.Error(Globals.LogTag, "Failed to get uri, Error - " + (NfcError)ret);
186 /// The mime type of the mime type NDEF record.
188 /// <since_tizen> 3 </since_tizen>
189 public string MimeType
194 int ret = Interop.Nfc.NdefRecord.GetMimeType(_recordHandle, out mimeType);
195 if (ret != (int)NfcError.None)
197 Log.Error(Globals.LogTag, "Failed to get mime type, Error - " + (NfcError)ret);
204 /// Creates a record with a given parameter value.
206 /// <since_tizen> 3 </since_tizen>
207 /// <param name="format">The type name format.</param>
208 /// <param name="type">The specified type name.</param>
209 /// <param name="id">The record ID.</param>
210 /// <param name="payload">The payload of this record.</param>
211 /// <param name="paloadLength">The byte size of the payload.</param>
212 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
213 /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
214 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
215 public NfcNdefRecord(NfcRecordTypeNameFormat format, byte[] type, byte[] id, byte[] payload, uint paloadLength)
217 int ret = Interop.Nfc.NdefRecord.Create(out _recordHandle, (int)format, type, type.Length, id, id.Length, payload, paloadLength);
219 if (ret != (int)NfcError.None)
221 Log.Error(Globals.LogTag, "Failed to create Ndef record, Error - " + (NfcError)ret);
222 NfcErrorFactory.ThrowNfcException(ret);
227 /// Creates a record with the text type payload.
229 /// <since_tizen> 3 </since_tizen>
230 /// <param name="text">The encoded text.</param>
231 /// <param name="languageCode">The language code string value followed by the IANA [RFC 3066] (ex: en-US, ko-KR).</param>
232 /// <param name="encode">The encoding type.</param>
233 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
234 /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
235 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
236 public NfcNdefRecord(string text, string languageCode, NfcEncodeType encode)
238 int ret = Interop.Nfc.NdefRecord.CreateText(out _recordHandle, text, languageCode, (int)encode);
240 if (ret != (int)NfcError.None)
242 Log.Error(Globals.LogTag, "Failed to create ndef Text record, Error - " + (NfcError)ret);
243 NfcErrorFactory.ThrowNfcException(ret);
248 /// Creates a record with the URI type payload.
250 /// <since_tizen> 3 </since_tizen>
251 /// <param name="uri">The URI string that will be stored in the payload.</param>
252 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
253 /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
254 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
255 public NfcNdefRecord(string uri)
257 int ret = Interop.Nfc.NdefRecord.CreateUri(out _recordHandle, uri);
259 if (ret != (int)NfcError.None)
261 Log.Error(Globals.LogTag, "Failed to create ndef Uri record, Error - " + (NfcError)ret);
262 NfcErrorFactory.ThrowNfcException(ret);
267 /// Creates a record with the mime type payload.
269 /// <since_tizen> 3 </since_tizen>
270 /// <param name="mimeType">The mime type [RFC 2046] (ex. text/plain, image/jpeg ). This value is stored in the type field.</param>
271 /// <param name="data">The data in the form of the bytes array.</param>
272 /// <param name="dataSize">The size of the data.</param>
273 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
274 /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
275 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
276 public NfcNdefRecord(string mimeType, byte[] data, uint dataSize)
278 int ret = Interop.Nfc.NdefRecord.CreateMime(out _recordHandle, mimeType, data, dataSize);
280 if (ret != (int)NfcError.None)
282 Log.Error(Globals.LogTag, "Failed to create ndef Mime record, Error - " + (NfcError)ret);
283 NfcErrorFactory.ThrowNfcException(ret);
292 public void Dispose()
295 GC.SuppressFinalize(this);
298 private void Dispose(bool disposing)
305 // Free managed objects.
306 int ret = Interop.Nfc.NdefRecord.Destroy(_recordHandle);
308 if (ret != (int)NfcError.None)
310 Log.Error(Globals.LogTag, "Failed to destroy ndef record, Error - " + (NfcError)ret);
313 //Free unmanaged objects
317 internal IntPtr GetHandle()
319 return _recordHandle;