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;
20 using System.Threading.Tasks;
22 namespace Tizen.Network.Nfc
25 /// The class for managing the P2P target information.
27 /// <since_tizen> 3 </since_tizen>
28 public class NfcP2p : IDisposable
30 private IntPtr _p2pTargetHandle = IntPtr.Zero;
31 private bool disposed = false;
33 private event EventHandler<P2pDataReceivedEventArgs> _p2pDataReceived;
35 private Interop.Nfc.P2pDataReceivedCallback _p2pDataReceivedCallback;
38 /// The event for receiving data from the NFC peer-to-peer target.
40 /// <since_tizen> 3 </since_tizen>
41 public event EventHandler<P2pDataReceivedEventArgs> P2pDataReceived
45 _p2pDataReceived += value;
49 _p2pDataReceived -= value;
53 internal NfcP2p(IntPtr handle)
55 _p2pTargetHandle = handle;
56 RegisterP2pDataReceivedEvent();
67 GC.SuppressFinalize(this);
70 private void Dispose(bool disposing)
77 // Free managed objects.
78 UnregisterP2pDataReceivedEvent();
80 //Free unmanaged objects
84 internal IntPtr GetHandle()
86 return _p2pTargetHandle;
90 /// Sends data to the NFC peer-to-peer target.
92 /// <since_tizen> 3 </since_tizen>
93 /// <param name="ndefMessage">The NfcNdefMessage object.</param>
94 /// <privilege>http://tizen.org/privilege/nfc</privilege>
95 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
96 /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
97 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
98 public Task<NfcError> SendNdefMessageAsync(NfcNdefMessage ndefMessage)
100 var task = new TaskCompletionSource<NfcError>();
102 Interop.Nfc.VoidCallback callback = (int result, IntPtr userData) =>
104 task.SetResult((NfcError)result);
108 int ret = Interop.Nfc.P2p.Send(_p2pTargetHandle, ndefMessage.GetHandle(), callback, IntPtr.Zero);
109 if (ret != (int)NfcError.None)
111 Log.Error(Globals.LogTag, "Failed to write ndef message, Error - " + (NfcError)ret);
112 NfcErrorFactory.ThrowNfcException(ret);
118 private void RegisterP2pDataReceivedEvent()
120 _p2pDataReceivedCallback = (IntPtr p2pTargetHandle, IntPtr ndefMessageHandle, IntPtr userData) =>
122 P2pDataReceivedEventArgs e = new P2pDataReceivedEventArgs(p2pTargetHandle, ndefMessageHandle);
123 _p2pDataReceived.SafeInvoke(null, e);
126 int ret = Interop.Nfc.P2p.SetDataReceivedCallback(_p2pTargetHandle, _p2pDataReceivedCallback, IntPtr.Zero);
127 if (ret != (int)NfcError.None)
129 Log.Error(Globals.LogTag, "Failed to set p2p target discovered callback, Error - " + (NfcError)ret);
133 private void UnregisterP2pDataReceivedEvent()
135 Interop.Nfc.P2p.UnsetDataReceivedCallback(_p2pTargetHandle);
140 /// The class for managing the SNEP (Simple NDEF Exchange Protocol) information.
142 /// <since_tizen> 3 </since_tizen>
143 public class NfcSnep : IDisposable
145 private IntPtr _snepHandle = IntPtr.Zero;
146 private bool disposed = false;
148 internal NfcSnep(IntPtr handle)
150 _snepHandle = handle;
158 public void Dispose()
161 GC.SuppressFinalize(this);
164 private void Dispose(bool disposing)
171 // Free managed objects.
173 //Free unmanaged objects
177 internal IntPtr GetHandle()