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 Message information. It allows applications to use the NDEF Message information.
26 /// <since_tizen> 3 </since_tizen>
27 public class NfcNdefMessage : IDisposable
29 private bool disposed = false;
30 private IntPtr _messageHandle = IntPtr.Zero;
31 private List<NfcNdefRecord> _recordList = new List<NfcNdefRecord>();
34 /// The number of records in the NDEF message.
36 /// <since_tizen> 3 </since_tizen>
37 public int RecordCount
42 int ret = Interop.Nfc.NdefMessage.GetRecordCount(_messageHandle, out recordCount);
43 if (ret != (int)NfcError.None)
45 Log.Error(Globals.LogTag, "Failed to get record count, Error - " + (NfcError)ret);
52 /// Creates an object for the access point.
54 /// <since_tizen> 3 </since_tizen>
55 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
56 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
57 public NfcNdefMessage()
59 int ret = Interop.Nfc.NdefMessage.Create(out _messageHandle);
61 if (ret != (int)NfcError.None)
63 Log.Error(Globals.LogTag, "Failed to create Ndef message, Error - " + (NfcError)ret);
64 NfcErrorFactory.ThrowNfcException(ret);
68 internal NfcNdefMessage(IntPtr messageHandle)
70 _messageHandle = messageHandle;
81 GC.SuppressFinalize(this);
84 private void Dispose(bool disposing)
91 // Free managed objects.
92 int ret = Interop.Nfc.NdefMessage.Destroy(_messageHandle);
94 if (ret != (int)NfcError.None)
96 Log.Error(Globals.LogTag, "Failed to destroy ndef message, Error - " + (NfcError)ret);
99 //Free unmanaged objects
104 /// Appends a record into the NDEF message.
106 /// <since_tizen> 3 </since_tizen>
107 /// <returns>Whether the record is appended successfully.</returns>
108 /// <param name="record">The NfcNdefRecord object that will be appended into the NDEF message.</param>
109 public bool AppendRecord(NfcNdefRecord record)
111 bool isSuccess = true;
113 int ret = Interop.Nfc.NdefMessage.AppendRecord(_messageHandle, record.GetHandle());
114 if (ret != (int)NfcError.None)
116 Log.Error(Globals.LogTag, "Failed to append record, Error - " + (NfcError)ret);
121 _recordList.Add(record);
128 /// Inserts a record at the index into the NDEF message.
130 /// <since_tizen> 3 </since_tizen>
131 /// <returns>Whether inserting the record succeeded.</returns>
132 /// <param name="index">The index of a record ( starts from 0 ).</param>
133 /// <param name="record">The NfcNdefRecord object that will be appended into the NDEF message.</param>
134 public bool InsertRecord(int index, NfcNdefRecord record)
136 bool isSuccess = true;
138 int ret = Interop.Nfc.NdefMessage.InsertRecord(_messageHandle, index, record.GetHandle());
139 if (ret != (int)NfcError.None)
141 Log.Error(Globals.LogTag, "Failed to insert record, Error - " + (NfcError)ret);
146 _recordList.Add(record);
153 /// Removes a record at the index into the NDEF message.
155 /// <since_tizen> 3 </since_tizen>
156 /// <returns>Whether removing the record succeeded.</returns>
157 /// <param name="index">The index of a record ( starts from 0 ).</param>
158 public bool RemoveRecord(int index)
160 bool isSuccess = true;
162 int ret = Interop.Nfc.NdefMessage.RemoveRecord(_messageHandle, index);
163 if (ret != (int)NfcError.None)
165 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
173 /// Gets a record by the index.
175 /// <since_tizen> 3 </since_tizen>
176 /// <returns>The NfcNdefRecord object.</returns>
177 /// <param name="index">The index of a record ( starts from 0 ).</param>
178 public NfcNdefRecord GetRecord(int index)
181 NfcNdefRecord recordObject = null;
183 int ret = Interop.Nfc.NdefMessage.GetRecord(_messageHandle, index, out recordHandle);
184 if (ret != (int)NfcError.None)
186 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
189 foreach (NfcNdefRecord recordElement in _recordList)
191 if(recordElement.GetHandle() == recordHandle)
193 Log.Debug(Globals.LogTag, "Find record handle");
194 recordObject = recordElement;
202 internal IntPtr GetHandle()
204 return _messageHandle;