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.
84 /// <since_tizen> 3 </since_tizen>
88 GC.SuppressFinalize(this);
91 private void Dispose(bool disposing)
98 // Free managed objects.
99 int ret = Interop.Nfc.NdefMessage.Destroy(_messageHandle);
101 if (ret != (int)NfcError.None)
103 Log.Error(Globals.LogTag, "Failed to destroy ndef message, Error - " + (NfcError)ret);
106 //Free unmanaged objects
111 /// Appends a record into the NDEF message.
113 /// <since_tizen> 3 </since_tizen>
114 /// <returns>Whether the record is appended successfully.</returns>
115 /// <param name="record">The NfcNdefRecord object that will be appended into the NDEF message.</param>
116 public bool AppendRecord(NfcNdefRecord record)
118 bool isSuccess = true;
120 int ret = Interop.Nfc.NdefMessage.AppendRecord(_messageHandle, record.GetHandle());
121 if (ret != (int)NfcError.None)
123 Log.Error(Globals.LogTag, "Failed to append record, Error - " + (NfcError)ret);
128 _recordList.Add(record);
135 /// Inserts a record at the index into the NDEF message.
137 /// <since_tizen> 3 </since_tizen>
138 /// <returns>Whether inserting the record succeeded.</returns>
139 /// <param name="index">The index of a record ( starts from 0 ).</param>
140 /// <param name="record">The NfcNdefRecord object that will be appended into the NDEF message.</param>
141 public bool InsertRecord(int index, NfcNdefRecord record)
143 bool isSuccess = true;
145 int ret = Interop.Nfc.NdefMessage.InsertRecord(_messageHandle, index, record.GetHandle());
146 if (ret != (int)NfcError.None)
148 Log.Error(Globals.LogTag, "Failed to insert record, Error - " + (NfcError)ret);
153 _recordList.Add(record);
160 /// Removes a record at the index into the NDEF message.
162 /// <since_tizen> 3 </since_tizen>
163 /// <returns>Whether removing the record succeeded.</returns>
164 /// <param name="index">The index of a record ( starts from 0 ).</param>
165 public bool RemoveRecord(int index)
167 bool isSuccess = true;
169 int ret = Interop.Nfc.NdefMessage.RemoveRecord(_messageHandle, index);
170 if (ret != (int)NfcError.None)
172 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
180 /// Gets a record by the index.
182 /// <since_tizen> 3 </since_tizen>
183 /// <returns>The NfcNdefRecord object.</returns>
184 /// <param name="index">The index of a record ( starts from 0 ).</param>
185 public NfcNdefRecord GetRecord(int index)
188 NfcNdefRecord recordObject = null;
190 int ret = Interop.Nfc.NdefMessage.GetRecord(_messageHandle, index, out recordHandle);
191 if (ret != (int)NfcError.None)
193 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
196 foreach (NfcNdefRecord recordElement in _recordList)
198 if(recordElement.GetHandle() == recordHandle)
200 Log.Debug(Globals.LogTag, "Find record handle");
201 recordObject = recordElement;
209 internal IntPtr GetHandle()
211 return _messageHandle;