示例#1
0
        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);
            }
        }
示例#2
0
        internal IEnumerable <WiFiDirectPeer> GetConnectedPeers()
        {
            List <WiFiDirectPeer> connectedPeerList = new List <WiFiDirectPeer>();

            Interop.WiFiDirect.ConnectedPeerCallback callback = (ref ConnectedPeerStruct peer, IntPtr userData) =>
            {
                if (!peer.Equals(null))
                {
                    connectedPeerList.Add(WiFiDirectUtils.ConvertStructToConnectedPeer(peer));
                }

                return(true);
            };
            int ret = Interop.WiFiDirect.GetConnectedPeers(callback, IntPtr.Zero);

            if (ret != (int)WiFiDirectError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get information of connected peers, Error - " + (WiFiDirectError)ret);
                WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
            }

            return(connectedPeerList);
        }
示例#3
0
        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);
            }
        }