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 Ndef Message information. It allows applications to use Ndef Message information.
26 public class NfcNdefMessage : IDisposable
28 private bool disposed = false;
29 private IntPtr _messageHandle = IntPtr.Zero;
30 private List<NfcNdefRecord> _recordList = new List<NfcNdefRecord>();
33 /// The number of record in NDEF message.
35 public int RecordCount
40 int ret = Interop.Nfc.NdefMessage.GetRecordCount(_messageHandle, out recordCount);
41 if (ret != (int)NfcError.None)
43 Log.Error(Globals.LogTag, "Failed to get record count, Error - " + (NfcError)ret);
50 /// Creates a object for the access point.
52 public NfcNdefMessage()
54 int ret = Interop.Nfc.NdefMessage.Create(out _messageHandle);
56 if (ret != (int)NfcError.None)
58 Log.Error(Globals.LogTag, "Failed to create Ndef message, Error - " + (NfcError)ret);
59 NfcErrorFactory.ThrowNfcException(ret);
63 internal NfcNdefMessage(IntPtr messageHandle)
65 _messageHandle = messageHandle;
70 int ret = Interop.Nfc.NdefMessage.Destroy(_messageHandle);
72 if (ret != (int)NfcError.None)
74 Log.Error(Globals.LogTag, "Failed to destroy ndef message, Error - " + (NfcError)ret);
83 GC.SuppressFinalize(this);
86 private void Dispose(bool disposing)
93 // Free managed objects.
95 //Free unmanaged objects
100 /// Appends a record into NDEF message.
102 /// <returns>Whether appending the record succeeded.</returns>
103 /// <param name="record">The NfcNdefRecord object that will be appended into NDEF message.</param>
104 public bool AppendRecord(NfcNdefRecord record)
106 bool isSuccess = true;
108 int ret = Interop.Nfc.NdefMessage.AppendRecord(_messageHandle, record.GetHandle());
109 if (ret != (int)NfcError.None)
111 Log.Error(Globals.LogTag, "Failed to append record, Error - " + (NfcError)ret);
116 _recordList.Add(record);
123 /// Inserts a record at index into NDEF message.
125 /// <returns>Whether insterting the record succeeded.</returns>
126 /// <param name="index">The index of record ( starts from 0 ).</param>
127 /// <param name="record">The NfcNdefRecord object that will be appended into NDEF message.</param>
128 public bool InsertRecord(int index, NfcNdefRecord record)
130 bool isSuccess = true;
132 int ret = Interop.Nfc.NdefMessage.InsertRecord(_messageHandle, index, record.GetHandle());
133 if (ret != (int)NfcError.None)
135 Log.Error(Globals.LogTag, "Failed to insert record, Error - " + (NfcError)ret);
140 _recordList.Add(record);
147 /// Inserts a record at index into NDEF message.
149 /// <returns>Whether removing the record succeeded.</returns>
150 /// <param name="index">The index of record ( starts from 0 ).</param>
151 public bool RemoveRecord(int index)
153 bool isSuccess = true;
155 int ret = Interop.Nfc.NdefMessage.RemoveRecord(_messageHandle, index);
156 if (ret != (int)NfcError.None)
158 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
166 /// Gets record by index.
168 /// <returns>The NfcNdefRecord object.</returns>
169 /// <param name="index">The index of record ( starts from 0 ).</param>
170 public NfcNdefRecord GetRecord(int index)
173 NfcNdefRecord recordObject = null;
175 int ret = Interop.Nfc.NdefMessage.GetRecord(_messageHandle, index, out recordHandle);
176 if (ret != (int)NfcError.None)
178 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
181 foreach (NfcNdefRecord recordElement in _recordList)
183 if(recordElement.GetHandle() == recordHandle)
185 Log.Debug(Globals.LogTag, "Find record handle");
186 recordObject = recordElement;
194 internal IntPtr GetHandle()
196 return _messageHandle;