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;
74 /// NfcNdefMessage destructor.
87 GC.SuppressFinalize(this);
90 private void Dispose(bool disposing)
97 // Free managed objects.
98 int ret = Interop.Nfc.NdefMessage.Destroy(_messageHandle);
100 if (ret != (int)NfcError.None)
102 Log.Error(Globals.LogTag, "Failed to destroy ndef message, Error - " + (NfcError)ret);
105 //Free unmanaged objects
110 /// Appends a record into the NDEF message.
112 /// <since_tizen> 3 </since_tizen>
113 /// <returns>Whether the record is appended successfully.</returns>
114 /// <param name="record">The NfcNdefRecord object that will be appended into the NDEF message.</param>
115 public bool AppendRecord(NfcNdefRecord record)
117 bool isSuccess = true;
119 int ret = Interop.Nfc.NdefMessage.AppendRecord(_messageHandle, record.GetHandle());
120 if (ret != (int)NfcError.None)
122 Log.Error(Globals.LogTag, "Failed to append record, Error - " + (NfcError)ret);
127 _recordList.Add(record);
134 /// Inserts a record at the index into the NDEF message.
136 /// <since_tizen> 3 </since_tizen>
137 /// <returns>Whether inserting the record succeeded.</returns>
138 /// <param name="index">The index of a record ( starts from 0 ).</param>
139 /// <param name="record">The NfcNdefRecord object that will be appended into the NDEF message.</param>
140 public bool InsertRecord(int index, NfcNdefRecord record)
142 bool isSuccess = true;
144 int ret = Interop.Nfc.NdefMessage.InsertRecord(_messageHandle, index, record.GetHandle());
145 if (ret != (int)NfcError.None)
147 Log.Error(Globals.LogTag, "Failed to insert record, Error - " + (NfcError)ret);
152 _recordList.Add(record);
159 /// Removes a record at the index into the NDEF message.
161 /// <since_tizen> 3 </since_tizen>
162 /// <returns>Whether removing the record succeeded.</returns>
163 /// <param name="index">The index of a record ( starts from 0 ).</param>
164 public bool RemoveRecord(int index)
166 bool isSuccess = true;
168 int ret = Interop.Nfc.NdefMessage.RemoveRecord(_messageHandle, index);
169 if (ret != (int)NfcError.None)
171 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
179 /// Gets a record by the index.
181 /// <since_tizen> 3 </since_tizen>
182 /// <returns>The NfcNdefRecord object.</returns>
183 /// <param name="index">The index of a record ( starts from 0 ).</param>
184 public NfcNdefRecord GetRecord(int index)
187 NfcNdefRecord recordObject = null;
189 int ret = Interop.Nfc.NdefMessage.GetRecord(_messageHandle, index, out recordHandle);
190 if (ret != (int)NfcError.None)
192 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
195 foreach (NfcNdefRecord recordElement in _recordList)
197 if(recordElement.GetHandle() == recordHandle)
199 Log.Debug(Globals.LogTag, "Find record handle");
200 recordObject = recordElement;
208 internal IntPtr GetHandle()
210 return _messageHandle;