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.";
            }
        }
Ejemplo n.º 2
0
        public bool StartScan(string searchForDevice = null)
        {
            _searchingFor = searchForDevice;
            _publisher.Start();

            if (_publisher.Status != WiFiDirectAdvertisementPublisherStatus.Started)
            {
                LastErrorMessage = "Failed to start advertisement.";
                return(false);
            }

            _discoveredDevices.Clear();
            LastErrorMessage = "Finding Devices...";

            String deviceSelector = WiFiDirectDevice.GetDeviceSelector(WiFiDirectDeviceSelectorType.DeviceInterface);

            _deviceWatcher = DeviceInformation.CreateWatcher(deviceSelector, new string[] { "System.Devices.WiFiDirect.InformationElements" });

            _deviceWatcher.Added   += OnDeviceAdded;
            _deviceWatcher.Removed += OnDeviceRemoved;
            _deviceWatcher.Updated += OnDeviceUpdated;
            _deviceWatcher.EnumerationCompleted += OnEnumerationCompleted;
            _deviceWatcher.Stopped += OnStopped;

            _deviceWatcher.Start();
            _scanning = true;

            return(true);
        }
Ejemplo n.º 3
0
        public static async void GetDevices()
        {
            string deviceSelector = WiFiDirectDevice.GetDeviceSelector(WiFiDirectDeviceSelectorType.AssociationEndpoint);
            DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(deviceSelector);

            foreach (DeviceInformation device in devices)
            {
                Send.Devices.Add(new Device(device.Name, device.Id));
            }
            MessagingCenter.Send("P2PShare", "WFDSearchIsEnabled", true);
        }
        private void btnWatcher_Click(object sender, RoutedEventArgs e)
        {
            if (_fWatcherStarted == false)
            {
                _publisher.Start();

                if (_publisher.Status != WiFiDirectAdvertisementPublisherStatus.Started)
                {
                    rootPage.NotifyUser("Failed to start advertisement.", NotifyType.ErrorMessage);
                    return;
                }

                DiscoveredDevices.Clear();
                rootPage.NotifyUser("Finding Devices...", NotifyType.StatusMessage);

                String deviceSelector = WiFiDirectDevice.GetDeviceSelector(
                    Utils.GetSelectedItemTag <WiFiDirectDeviceSelectorType>(cmbDeviceSelector));

                _deviceWatcher = DeviceInformation.CreateWatcher(deviceSelector, new string[] { "System.Devices.WiFiDirect.InformationElements" });

                _deviceWatcher.Added   += OnDeviceAdded;
                _deviceWatcher.Removed += OnDeviceRemoved;
                _deviceWatcher.Updated += OnDeviceUpdated;
                _deviceWatcher.EnumerationCompleted += OnEnumerationCompleted;
                _deviceWatcher.Stopped += OnStopped;

                _deviceWatcher.Start();

                btnWatcher.Content = "Stop Watcher";
                _fWatcherStarted   = true;
            }
            else
            {
                _publisher.Stop();

                btnWatcher.Content = "Start Watcher";
                _fWatcherStarted   = false;

                _deviceWatcher.Added   -= OnDeviceAdded;
                _deviceWatcher.Removed -= OnDeviceRemoved;
                _deviceWatcher.Updated -= OnDeviceUpdated;
                _deviceWatcher.EnumerationCompleted -= OnEnumerationCompleted;
                _deviceWatcher.Stopped -= OnStopped;

                _deviceWatcher.Stop();

                rootPage.NotifyUser("Device watcher stopped.", NotifyType.StatusMessage);
            }
        }
        void discoverPeers(Dictionary <String, Object> jsonOutput)
        {
            try {
                advertise();
                mDiscovered.Clear();
                mDeviceWatcher = DeviceInformation.CreateWatcher(
                    WiFiDirectDevice.GetDeviceSelector(WiFiDirectDeviceSelectorType.AssociationEndpoint),
                    new string[] { "System.Devices.WiFiDirect.InformationElements" });

                mDeviceWatcher.Added   += OnDeviceAdded;
                mDeviceWatcher.Removed += OnDeviceRemoved;
                mDeviceWatcher.Updated += OnDeviceUpdated;
                mDeviceWatcher.Stopped += OnStopped;

                mDeviceWatcher.Start();
                jsonOutput[TAG_DATA] = TAG_TRUE;
            } catch (Exception ex) {
                setError(jsonOutput, ex.ToString(), ex.HResult);
            }
            native_.PostMessageToJS(new JavaScriptSerializer().Serialize(jsonOutput));
        }
        private void btnWatcher_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            if (_fWatcherStarted == false)
            {
                btnWatcher.Content = "Stop Watcher";
                _fWatcherStarted   = true;

                _discoveredDevices.Clear();
                rootPage.NotifyUser("Finding Devices...", NotifyType.StatusMessage);

                _deviceWatcher = null;
                String deviceSelector = WiFiDirectDevice.GetDeviceSelector((cmbDeviceSelector.ToString().Equals("Device Interface") == true) ?
                                                                           WiFiDirectDeviceSelectorType.DeviceInterface : WiFiDirectDeviceSelectorType.AssociationEndpoint);

                _deviceWatcher = DeviceInformation.CreateWatcher(deviceSelector, new string[] { "System.Devices.WiFiDirect.InformationElements" });

                _deviceWatcher.Added   += OnDeviceAdded;
                _deviceWatcher.Removed += OnDeviceRemoved;
                _deviceWatcher.Updated += OnDeviceUpdated;
                _deviceWatcher.EnumerationCompleted += OnEnumerationCompleted;
                _deviceWatcher.Stopped += OnStopped;

                _deviceWatcher.Start();
            }
            else
            {
                btnWatcher.Content = "Start Watcher";
                _fWatcherStarted   = false;

                _deviceWatcher.Added   -= OnDeviceAdded;
                _deviceWatcher.Removed -= OnDeviceRemoved;
                _deviceWatcher.Updated -= OnDeviceUpdated;
                _deviceWatcher.EnumerationCompleted -= OnEnumerationCompleted;
                _deviceWatcher.Stopped -= OnStopped;

                _deviceWatcher.Stop();
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 连接者开始搜索发出广播的设备
        /// </summary>
        public async void StartConnecting()
        {
            string deviceSelector = WiFiDirectDevice.GetDeviceSelector(WiFiDirectDeviceSelectorType.AssociationEndpoint);

            //获取所有检测到的Wi-Fi直连设备
            DeviceInformationCollection devInfoCollection = await DeviceInformation.FindAllAsync(deviceSelector);

            Invoke(() =>
            {
                if (devInfoCollection.Count == 0)
                {
                    TextBlock_ConnectedState.Text = "找不到设备";
                }
                else
                {
                    TextBlock_ConnectedState.Text = "";
                    foreach (var devInfo in devInfoCollection)
                    {
                        TextBlock_ConnectedState.Text += devInfo.Id + '\n';
                    }
                    TextBox_SelectedIP.Text = devInfoCollection[0].Id;
                }
            });
        }
        private async void SetupWifiDirect()
        {
            ListenForIncomingConnections();
            if (_fWatcherStarted == false)
            {
                _publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Normal;
                //make this device known
                _publisher.Start();

                if (_publisher.Status != WiFiDirectAdvertisementPublisherStatus.Started)
                {
                    NotifyUser("Failed to start advertisement.", NotifyType.ErrorMessage);
                    return;
                }

                //detect other devices
                DiscoveredDevices.Clear();
                NotifyUser("Finding Devices...", NotifyType.StatusMessage);

                String deviceSelector = WiFiDirectDevice.GetDeviceSelector(WiFiDirectDeviceSelectorType.AssociationEndpoint);//WiFiDirectDevice.GetDeviceSelector(
                //Utils.GetSelectedItemTag<WiFiDirectDeviceSelectorType>(cmbDeviceSelector));

                _deviceWatcher = DeviceInformation.CreateWatcher(deviceSelector, new string[] { "System.Devices.WiFiDirect.InformationElements" });

                _deviceWatcher.Added   += OnDeviceAdded;
                _deviceWatcher.Removed += OnDeviceRemoved;
                _deviceWatcher.Updated += OnDeviceUpdated;
                _deviceWatcher.EnumerationCompleted += OnEnumerationCompleted;
                _deviceWatcher.Stopped += OnStopped;

                _deviceWatcher.Start();

                //btnWatcher.Content = "Stop Watcher";
                _fWatcherStarted = true;
            }
            else
            {
                _publisher.Stop();

                //btnWatcher.Content = "Start Watcher";
                _fWatcherStarted = false;

                _deviceWatcher.Added   -= OnDeviceAdded;
                _deviceWatcher.Removed -= OnDeviceRemoved;
                _deviceWatcher.Updated -= OnDeviceUpdated;
                _deviceWatcher.EnumerationCompleted -= OnEnumerationCompleted;
                _deviceWatcher.Stopped -= OnStopped;

                _deviceWatcher.Stop();

                NotifyUser("Device watcher stopped.", NotifyType.StatusMessage);
            }


            string ServiceSelector = WiFiDirectService.GetSelector("WalkieTalkie");

            // Get all WiFiDirect services that are advertising and in range
            //DeviceInformationCollection devInfoCollection = await DeviceInformation.FindAllAsync(ServiceSelector);

            //// Get a Service Seeker object
            //WiFiDirectService Service = await WiFiDirectService.FromIdAsync(devInfoCollection[0].Id);

            //// Connect to the Advertiser
            //WiFiDirectServiceSession Session = await Service.ConnectAsync();

            //// Get the local and remote IP addresses
            //var EndpointPairs = Session.GetConnectionEndpointPairs();
        }