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 NFC P2P(Peer to Peer) mode. It allows applications to handle P2P informations.
26 /// <since_tizen> 3 </since_tizen>
27 /// <privilege>http://tizen.org/privilege/nfc</privilege>
28 public class NfcP2pAdapter : IDisposable
30 private NfcP2p _p2pTarget;
31 private bool disposed = false;
33 private event EventHandler<P2pTargetDiscoveredEventArgs> _p2pTargetDiscovered;
35 private Interop.Nfc.P2pTargetDiscoveredCallback _p2pTargetDiscoveredCallback;
38 /// The event for receiving NFC peer-to-peer target discovered notification.
40 /// <since_tizen> 3 </since_tizen>
41 public event EventHandler<P2pTargetDiscoveredEventArgs> P2pTargetDiscovered
45 _p2pTargetDiscovered += value;
49 _p2pTargetDiscovered -= value;
53 internal NfcP2pAdapter()
55 RegisterP2pTargetDiscoveredEvent();
66 GC.SuppressFinalize(this);
69 private void Dispose(bool disposing)
76 // Free managed objects.
77 UnregisterP2pTargetDiscoveredEvent();
79 //Free unmanaged objects
84 /// Gets current connected p2p target.
86 /// <since_tizen> 3 </since_tizen>
87 /// <returns>NfcP2p object.</returns>
88 /// <privilege>http://tizen.org/privilege/nfc</privilege>
89 /// <exception cref="NotSupportedException">Thrown when Nfc is not supported.</exception>
90 /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
91 public NfcP2p GetConnectedTarget()
93 IntPtr targetHandle = IntPtr.Zero;
94 int ret = Interop.Nfc.GetConnectedTarget(out targetHandle);
96 if (ret != (int)NfcError.None)
98 Log.Error(Globals.LogTag, "Failed to get connected p2p target, Error - " + (NfcError)ret);
99 NfcErrorFactory.ThrowNfcException(ret);
101 _p2pTarget = new NfcP2p(targetHandle);
106 private void RegisterP2pTargetDiscoveredEvent()
108 _p2pTargetDiscoveredCallback = (int type, IntPtr p2pTargetHandle, IntPtr userData) =>
110 NfcDiscoveredType tagType = (NfcDiscoveredType)type;
111 P2pTargetDiscoveredEventArgs e = new P2pTargetDiscoveredEventArgs(tagType, p2pTargetHandle);
112 _p2pTargetDiscovered.SafeInvoke(null, e);
115 int ret = Interop.Nfc.SetP2pTargetDiscoveredCallback(_p2pTargetDiscoveredCallback, IntPtr.Zero);
116 if (ret != (int)NfcError.None)
118 Log.Error(Globals.LogTag, "Failed to set p2p target discovered callback, Error - " + (NfcError)ret);
122 private void UnregisterP2pTargetDiscoveredEvent()
124 Interop.Nfc.UnsetP2pTargetDiscoveredCallback();