async internal void testconnectAsync()
        {
            string result         = "";
            string deviceSelector = WiFiDirectDevice.GetDeviceSelector(WiFiDirectDeviceSelectorType.AssociationEndpoint);
            DeviceInformationCollection devInfoCollection = await DeviceInformation.FindAllAsync(deviceSelector);

            String deviceId = devInfoCollection[0].Id;

            wfdDevice = await Windows.Devices.WiFiDirect.WiFiDirectDevice.FromIdAsync(deviceId);

            if (wfdDevice == null)
            {
                result = "Connection to " + deviceId + " failed.";
            }

            // Register for connection status change notification.
            wfdDevice.ConnectionStatusChanged += new TypedEventHandler <Windows.Devices.WiFiDirect.WiFiDirectDevice, object>(OnConnectionChanged);

            // Get the EndpointPair information.
            var EndpointPairCollection = wfdDevice.GetConnectionEndpointPairs();

            if (EndpointPairCollection.Count > 0)
            {
                var endpointPair = EndpointPairCollection[0];
                result = "Local IP address " + endpointPair.LocalHostName.ToString() +
                         " connected to remote IP address " + endpointPair.RemoteHostName.ToString();
            }
            else
            {
                result = "Connection to " + deviceId + " failed.";
            }
        }
        private async System.Threading.Tasks.Task <String> Connect(string deviceId)
        {
            string result = "";

            try
            {
                // No device Id specified.
                if (String.IsNullOrEmpty(deviceId))
                {
                    return("Please specify a Wi- Fi Direct device Id.");
                }

                // Connect to the selected Wi-Fi Direct device.
                wfdDevice = await Windows.Devices.WiFiDirect.WiFiDirectDevice.FromIdAsync(deviceId);

                if (wfdDevice == null)
                {
                    result = "Connection to " + deviceId + " failed.";
                }

                // Register for connection status change notification.
                wfdDevice.ConnectionStatusChanged += new TypedEventHandler <Windows.Devices.WiFiDirect.WiFiDirectDevice, object>(OnConnectionChanged);

                // Get the EndpointPair information.
                var EndpointPairCollection = wfdDevice.GetConnectionEndpointPairs();

                if (EndpointPairCollection.Count > 0)
                {
                    var endpointPair = EndpointPairCollection[0];
                    result = "Local IP address " + endpointPair.LocalHostName.ToString() +
                             " connected to remote IP address " + endpointPair.RemoteHostName.ToString();
                }
                else
                {
                    result = "Connection to " + deviceId + " failed.";
                }
            }
            catch (Exception err)
            {
                // Handle error.
                result = "Error occurred: " + err.Message;
            }

            return(result);
        }
        async void Connect(object sender, RoutedEventArgs e)
        {
            rootPage.NotifyUser("", NotifyType.ErrorMessage);
            DeviceInformation chosenDevInfo = null;
            EndpointPair      endpointPair  = null;

            try
            {
                // If nothing is selected, return
                if (FoundDevicesList.SelectedIndex == -1)
                {
                    rootPage.NotifyUser("Please select a device", NotifyType.StatusMessage);
                    return;
                }
                else
                {
                    chosenDevInfo = devInfoCollection[FoundDevicesList.SelectedIndex];
                }

                rootPage.NotifyUser("Connecting to " + chosenDevInfo.Name + "(" + chosenDevInfo.Id + ")" + "....", NotifyType.StatusMessage);


                // Connect to the selected WiFiDirect device
                wfdDevice = await Windows.Devices.WiFiDirect.WiFiDirectDevice.FromIdAsync(chosenDevInfo.Id);

                if (wfdDevice == null)
                {
                    rootPage.NotifyUser("Connection to " + chosenDevInfo.Name + " failed.", NotifyType.StatusMessage);
                    return;
                }

                // Register for Connection status change notification
                wfdDevice.ConnectionStatusChanged += new TypedEventHandler <Windows.Devices.WiFiDirect.WiFiDirectDevice, object>(DisconnectNotification);

                // Get the EndpointPair collection
                var EndpointPairCollection = wfdDevice.GetConnectionEndpointPairs();
                if (EndpointPairCollection.Count > 0)
                {
                    endpointPair = EndpointPairCollection[0];
                }
                else
                {
                    rootPage.NotifyUser("Connection to " + chosenDevInfo.Name + " failed.", NotifyType.StatusMessage);
                    return;
                }

                PCIpAddress.Text            = "PC's IP Address: " + endpointPair.LocalHostName.ToString();
                DeviceIpAddress.Text        = "Device's IP Address: " + endpointPair.RemoteHostName.ToString();
                PCIpAddress.Visibility      = Visibility.Visible;
                DeviceIpAddress.Visibility  = Visibility.Visible;
                DisconnectButton.Visibility = Visibility.Visible;
                ConnectButton.Visibility    = Visibility.Collapsed;
                FoundDevicesList.Visibility = Visibility.Collapsed;
                GetDevicesButton.Visibility = Visibility.Collapsed;

                rootPage.NotifyUser("Connection succeeded", NotifyType.StatusMessage);
            }
            catch (Exception err)
            {
                rootPage.NotifyUser("Connection to " + chosenDevInfo.Name + " failed: " + err.Message, NotifyType.ErrorMessage);
            }
        }
Esempio n. 4
0
        async void Connect(object sender, RoutedEventArgs e)
        {
            rootPage.NotifyUser("", NotifyType.ErrorMessage);
            DeviceInformation chosenDevInfo = null;
            EndpointPair      endpointPair  = null;

            try
            {
                // If nothing is selected, return
                if (FoundDevicesList.SelectedIndex == -1)
                {
                    rootPage.NotifyUser("Please select a device", NotifyType.StatusMessage);
                    return;
                }
                else
                {
                    chosenDevInfo = devInfoCollection[FoundDevicesList.SelectedIndex];
                }

                rootPage.NotifyUser("Connecting to " + chosenDevInfo.Name + "....", NotifyType.StatusMessage);


                // Connect to the selected WiFiDirect device
                wfdDevice = await Windows.Devices.WiFiDirect.WiFiDirectDevice.FromIdAsync(chosenDevInfo.Id);

                if (wfdDevice == null)
                {
                    rootPage.NotifyUser("Connection to " + chosenDevInfo.Name + " failed.", NotifyType.StatusMessage);
                    return;
                }

                // Register for Connection status change notification
                wfdDevice.ConnectionStatusChanged += new TypedEventHandler <Windows.Devices.WiFiDirect.WiFiDirectDevice, object>(DisconnectNotification);

                // Get the EndpointPair collection
                var EndpointPairCollection = wfdDevice.GetConnectionEndpointPairs();
                if (EndpointPairCollection.Count > 0)
                {
                    endpointPair = EndpointPairCollection[0];
                }
                else
                {
                    rootPage.NotifyUser("Connection to " + chosenDevInfo.Name + " failed.", NotifyType.StatusMessage);
                    return;
                }

                PCIpAddress.Text           = "PC's IP Address: " + endpointPair.LocalHostName.ToString();
                DeviceIpAddress.Text       = "Device's IP Address: " + endpointPair.RemoteHostName.ToString();
                PCIpAddress.Visibility     = Visibility.Visible;
                DeviceIpAddress.Visibility = Visibility.Visible;
                //SendTextButton.Visibility = Visibility.Visible;
                DisconnectButton.Visibility = Visibility.Visible;
                ConnectButton.Visibility    = Visibility.Collapsed;
                FoundDevicesList.Visibility = Visibility.Collapsed;
                GetDevicesButton.Visibility = Visibility.Collapsed;

                rootPage.NotifyUser("Connection succeeded", NotifyType.StatusMessage);

                //Server Socket open
                StreamSocketListener listener = new StreamSocketListener();
                listener.ConnectionReceived += OnConnection;

                // Start listen operation.
                try
                {
                    // bind to any
                    // Don't limit traffic to an address or an adapter.
                    await listener.BindServiceNameAsync("9190");

                    rootPage.NotifyUser("Listening", NotifyType.StatusMessage);

                    // bind to specific address
                    // Try to bind to a specific address.
                    //BindEndpointAsync(Target IPAddr, Port Number)
                    //HostName DeviceIPHostName = new HostName(endpointPair.RemoteHostName.ToString());
                    //await listener.BindEndpointAsync(DeviceIPHostName, "9190");
                    //rootPage.NotifyUser(
                    //   "Listening on address " + DeviceIPHostName.ToString(), NotifyType.StatusMessage);
                }
                catch (Exception exception)
                {
                    // If this is an unknown status it means that the error is fatal and retry will likely fail.
                    if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                    {
                        throw;
                    }

                    rootPage.NotifyUser(
                        "Start listening failed with error: " + exception.Message, NotifyType.ErrorMessage);
                }
            }
            catch (Exception err)
            {
                rootPage.NotifyUser("Connection to " + chosenDevInfo.Name + " failed: " + err.Message, NotifyType.ErrorMessage);
            }
        }
Esempio n. 5
0
        async void Connect(object sender, RoutedEventArgs e)
        {
            rootPage.NotifyUser("", NotifyType.ErrorMessage);
            DeviceInformation chosenDevInfo = null;
            EndpointPair endpointPair = null;
            try
            {
                // If nothing is selected, return
                if (FoundDevicesList.SelectedIndex == -1)
                {
                    rootPage.NotifyUser("Please select a device", NotifyType.StatusMessage);
                    return;
                }
                else
                {
                    chosenDevInfo = devInfoCollection[FoundDevicesList.SelectedIndex];
                }

                rootPage.NotifyUser("Connecting to " + chosenDevInfo.Name + "....", NotifyType.StatusMessage);


                // Connect to the selected WiFiDirect device
                wfdDevice = await Windows.Devices.WiFiDirect.WiFiDirectDevice.FromIdAsync(chosenDevInfo.Id);

                if (wfdDevice == null)
                {
                    rootPage.NotifyUser("Connection to " + chosenDevInfo.Name + " failed.", NotifyType.StatusMessage);
                    return;
                }

                // Register for Connection status change notification
                wfdDevice.ConnectionStatusChanged += new TypedEventHandler<Windows.Devices.WiFiDirect.WiFiDirectDevice, object>(DisconnectNotification);     

                // Get the EndpointPair collection
                var EndpointPairCollection = wfdDevice.GetConnectionEndpointPairs();
                if (EndpointPairCollection.Count > 0)
                {
                    endpointPair = EndpointPairCollection[0];
                }
                else
                {
                    rootPage.NotifyUser("Connection to " + chosenDevInfo.Name + " failed.", NotifyType.StatusMessage);
                    return;
                }

                PCIpAddress.Text = "PC's IP Address: " + endpointPair.LocalHostName.ToString();
                DeviceIpAddress.Text =  "Device's IP Address: " + endpointPair.RemoteHostName.ToString();
                PCIpAddress.Visibility = Visibility.Visible;
                DeviceIpAddress.Visibility = Visibility.Visible;
                DisconnectButton.Visibility = Visibility.Visible;
                ConnectButton.Visibility = Visibility.Collapsed;
                FoundDevicesList.Visibility = Visibility.Collapsed;
                GetDevicesButton.Visibility = Visibility.Collapsed;

                rootPage.NotifyUser("Connection succeeded", NotifyType.StatusMessage);
            }
            catch (Exception err)
            {
                rootPage.NotifyUser("Connection to " + chosenDevInfo.Name + " failed: " + err.Message, NotifyType.ErrorMessage);
            }
        }