private void RegisterPeerFoundEvent() { _peerFoundCallback = (int result, WiFiDirectDiscoveryState stateInfo, string address, IntPtr userData) => { if (_peerFound != null && stateInfo == WiFiDirectDiscoveryState.Found) { WiFiDirectError error = (WiFiDirectError)result; WiFiDirectDiscoveryState state = stateInfo; IntPtr peer; Interop.WiFiDirect.GetDiscoveredPeerInfo(address, out peer); DiscoveredPeerStruct peerStruct = (DiscoveredPeerStruct)Marshal.PtrToStructure(peer, typeof(DiscoveredPeerStruct)); _peerFound(null, new PeerFoundEventArgs(error, state, WiFiDirectUtils.ConvertStructToDiscoveredPeer(peerStruct))); } }; int ret = Interop.WiFiDirect.SetPeerFoundCallback(_peerFoundCallback, IntPtr.Zero); if (ret != (int)WiFiDirectError.None) { Log.Error(Globals.LogTag, "Failed to set Wi-Fi Direct discovery state changed callback, Error - " + (WiFiDirectError)ret); } }
internal IEnumerable <WiFiDirectPeer> GetDiscoveredPeers() { List <WiFiDirectPeer> discoveredPeerList = new List <WiFiDirectPeer>(); Interop.WiFiDirect.DiscoveredPeerCallback callback = (ref DiscoveredPeerStruct peer, IntPtr userData) => { if (!peer.Equals(null)) { discoveredPeerList.Add(WiFiDirectUtils.ConvertStructToDiscoveredPeer(peer)); } return(true); }; int ret = Interop.WiFiDirect.GetDiscoveredPeers(callback, IntPtr.Zero); if (ret != (int)WiFiDirectError.None) { Log.Error(Globals.LogTag, "Failed to get information of discovered peers, Error - " + (WiFiDirectError)ret); WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); } return(discoveredPeerList); }
private void RegisterServiceStateChangedEvent() { _serviceStateChangedCallback = (int result, WiFiDirectServiceDiscoveryState stateInfo, WiFiDirectServiceType typeInfo, IntPtr responseData, string address, IntPtr userData) => { if (_serviceStateChanged != null) { WiFiDirectError error = (WiFiDirectError)result; WiFiDirectServiceDiscoveryState state = stateInfo; WiFiDirectServiceType type = typeInfo; string response = Marshal.PtrToStringAnsi(responseData); IntPtr peer; Interop.WiFiDirect.GetDiscoveredPeerInfo(address, out peer); DiscoveredPeerStruct peerStruct = (DiscoveredPeerStruct)Marshal.PtrToStructure(peer, typeof(DiscoveredPeerStruct)); _serviceStateChanged(null, new ServiceStateChangedEventArgs(error, state, type, response, WiFiDirectUtils.ConvertStructToDiscoveredPeer(peerStruct))); } }; int ret = Interop.WiFiDirect.SetServiceStateChangedCallback(_serviceStateChangedCallback, IntPtr.Zero); if (ret != (int)WiFiDirectError.None) { Log.Error(Globals.LogTag, "Failed to set service state changed callback, Error - " + (WiFiDirectError)ret); } }