private static async Task <ConnectionProfile> GetConectionProfile() { try { ConnectionProfileFilter filter = new ConnectionProfileFilter { IsWwanConnectionProfile = true, IsConnected = true }; var result = await NetworkInformation.FindConnectionProfilesAsync(filter); if (result != null && result.Count > 0) { return(result[result.Count - 1]); //通常连接的是最后一个,当然手机已连接上就只有一个(吧?)=。= } else { return(NetworkInformation.GetInternetConnectionProfile()); } } catch (Exception ex) { return(null); } }
public async Task <bool> IsWiFiEnabled() { //Get the Internet connection profile string ssid = string.Empty; ConnectionProfileFilter filter = new ConnectionProfileFilter(); filter.IsConnected = true; filter.IsWlanConnectionProfile = true; var result = await NetworkInformation.FindConnectionProfilesAsync(filter); if (result.Count > 0) { foreach (var profile in result) { if (profile.IsWlanConnectionProfile) { ssid += profile.WlanConnectionProfileDetails.GetConnectedSsid(); } } } if (string.IsNullOrEmpty(ssid)) { return(false); } else { return(true); } }
async static Task <List <HostName> > GetHostWithFilterAsync(ConnectionProfileFilter filter) { var list = new List <HostName>(); hostprofiles = new List <ConnectionProfile>(); var profiles = await NetworkInformation.FindConnectionProfilesAsync(filter); var hostnames = NetworkInformation.GetHostNames(); foreach (var host in hostnames) { if (host.IPInformation != null && host.IPInformation.NetworkAdapter != null) { Guid adapterID = host.IPInformation.NetworkAdapter.NetworkAdapterId; foreach (var profile in profiles) { if (adapterID == profile.NetworkAdapter.NetworkAdapterId) { list.Add(host); hostprofiles.Add(profile); break; } } } } return(list); }
/// <summary> /// Looks for profiles matching certain criteria and returns the first match. /// </summary> /// <remarks> /// Criteria are: /// <list type="number"> /// <item>Connected unrestricted WiFi</item> /// <item>Any connected unrestricted profile</item> /// <item>Any connected profile</item> /// <item>Any profile</item> /// </list> /// </remarks> /// <returns></returns> private static async Task <ConnectionProfile> SelectBestProfile() { var filters = new ConnectionProfileFilter[] { new ConnectionProfileFilter { IsConnected = true, IsWlanConnectionProfile = true, NetworkCostType = NetworkCostType.Unrestricted, }, new ConnectionProfileFilter { IsConnected = true, NetworkCostType = NetworkCostType.Unrestricted, }, new ConnectionProfileFilter { IsConnected = true, }, new ConnectionProfileFilter() }; foreach (var filter in filters) { var profiles = await NetworkInformation.FindConnectionProfilesAsync(filter); if (profiles.Any()) { return(profiles.First()); } } return(null); }
private async Task GetConnectedWiFiAsync() { lock (checkingNetworkLock) { if (checkingNetwork || verifiedPhoton) { return; } checkingNetwork = true; } ConnectionProfileFilter ProfileFilter = new ConnectionProfileFilter(); ProfileFilter.IsWlanConnectionProfile = true; ProfileFilter.IsConnected = true; ConnectionProfile photonConnectionProfile = null; var connectionProfilesAsync = NetworkInformation.FindConnectionProfilesAsync(ProfileFilter); var connectionProfiles = await connectionProfilesAsync.AsTask(); foreach (var connectionProfile in connectionProfiles) { if (connectionProfile.ProfileName.StartsWith("Photon-") && connectionProfile.ProfileName.Length == 11) { photonConnectionProfile = connectionProfile; break; } } if (photonConnectionProfile == null) { checkingNetwork = false; return; } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { verifiedPhoton = await VerifyPhotonConnectionAsync(photonConnectionProfile); if (verifiedPhoton) { checkingNetwork = false; NetworkInformation.NetworkStatusChanged -= NetworkInformation_NetworkStatusChanged; Frame.Navigate(typeof(SoftAPSelectWiFiPage)); } else { checkingNetwork = false; } }); }
private async Task checkConnection(CancellationTokenSource cancel) { var filter = new ConnectionProfileFilter { IsConnected = true, IsWwanConnectionProfile = false, IsWlanConnectionProfile = true, }; var profiles = await NetworkInformation.FindConnectionProfilesAsync(filter); foreach (var profile in profiles) { var ssid = profile.WlanConnectionProfileDetails.GetConnectedSsid(); if (IsCameraAccessPoint(ssid)) { var previous = PreviousSsid; PreviousSsid = ssid; // Connected to Access Point and it is a camera device. if (ssid == previous && devices.Count != 0) { // Keep searching even if CDS provider is discovered. DebugUtil.Log("Some devices discovered on the previous SSID. Finish auto discovery."); return; } if (ssid != previous) { DebugUtil.Log("New access point detected. Refresh."); RefreshDevices(); } else { DebugUtil.Log("No devices discovered yet. keep searching."); } SearchCamera(); SearchCds(); await Task.Delay(5000); if (!cancel.IsCancellationRequested) { await checkConnection(cancel); } return; } } DebugUtil.Log("Not connected to camera device."); Clear(); }
public async Task <bool> IsConnectedToCameraApDirectly() { var filter = new ConnectionProfileFilter { IsConnected = true, IsWwanConnectionProfile = false, IsWlanConnectionProfile = true, }; var profiles = await NetworkInformation.FindConnectionProfilesAsync(filter); var matched = profiles.Select(profile => profile.WlanConnectionProfileDetails.GetConnectedSsid()) .FirstOrDefault(ssid => { return(ssid != null && CameraApRegex.IsMatch(ssid)); }); return(matched != null); }
public async Task Initialize() { var filter = new ConnectionProfileFilter { IsConnected = true, IsWwanConnectionProfile = false, IsWlanConnectionProfile = true, }; var profiles = await NetworkInformation.FindConnectionProfilesAsync(filter); foreach (var profile in profiles) { var ssid = profile.WlanConnectionProfileDetails.GetConnectedSsid(); if (IsCameraAccessPoint(ssid)) { PreviousSsid = ssid; return; } } }
private async void NetworkInformation_NetworkStatusChanged(object sender) { hasInternetAccess = false; ConnectionProfileFilter ProfileFilter = new ConnectionProfileFilter(); ProfileFilter.IsConnected = true; var connectionProfilesAsync = NetworkInformation.FindConnectionProfilesAsync(ProfileFilter); var connectionProfiles = await connectionProfilesAsync.AsTask(); foreach (var connectionProfile in connectionProfiles) { if (!connectionProfile.ProfileName.StartsWith("Photon-")) { hasInternetAccess = connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess; if (hasInternetAccess) { break; } } } }
private MeasureBackgroundTask() { logModel.AppendLog(Log.CreateLog("Measure Service starting...", Log.LogType.System)); userSettings = new UserSettings(); var taskUser = Task.Run(async() => { userSettings = await userSettingsModel.GetUserSettingsAsync(); }); taskUser.Wait(); logModel.AppendLog(Log.CreateLog("UserSettings retreived", Log.LogType.System)); //Disable Diagnostic Mode on restart if (userSettings.isDiagnosticModeEnable) { userSettings.isDiagnosticModeEnable = false; var taskUserSync = Task.Run(async() => { await userSettingsModel.SyncUserSettings(userSettings); }); taskUserSync.Wait(); } var taskTethering = Task.Run(async() => { try { var connectedProfile = NetworkInformation.GetInternetConnectionProfile(); if (connectedProfile != null) { logModel.AppendLog(Log.CreateLog(String.Format("Connected Profile found - {0}", connectedProfile.ProfileName), Log.LogType.System)); } bool isWLANConnection = (connectedProfile == null) ? false : connectedProfile.IsWlanConnectionProfile; if (isWLANConnection == false) { logModel.AppendLog(Log.CreateLog("Device offline", Log.LogType.System)); ConnectionProfileFilter filter = new ConnectionProfileFilter(); filter.IsWlanConnectionProfile = true; var profile = await NetworkInformation.FindConnectionProfilesAsync(filter); var defaultProfile = profile.FirstOrDefault(); if (defaultProfile != null) { logModel.AppendLog(Log.CreateLog(String.Format("Default Profile found - {0}", defaultProfile.ProfileName), Log.LogType.System)); var networkOperatorTetheringManager = NetworkOperatorTetheringManager.CreateFromConnectionProfile(defaultProfile); if (networkOperatorTetheringManager.TetheringOperationalState != TetheringOperationalState.On) { var config = new NetworkOperatorTetheringAccessPointConfiguration(); config.Ssid = userSettings.SSID; config.Passphrase = userSettings.ACCESS_POINT_PWD; logModel.AppendLog(Log.CreateLog("Access Point creation init...", Log.LogType.System)); await networkOperatorTetheringManager.ConfigureAccessPointAsync(config); var rslt = await networkOperatorTetheringManager.StartTetheringAsync(); await Task.Delay(5000); logModel.AppendLog(Log.CreateLog("Access Point creation ending...", Log.LogType.System)); if (rslt.Status == TetheringOperationStatus.Success) { logModel.AppendLog(Log.CreateLog("Access Point created", Log.LogType.System)); } else { logModel.AppendLog(Log.CreateLog(String.Format("Access Point creation failed - {0}", rslt.AdditionalErrorMessage), Log.LogType.Warning)); } } else { logModel.AppendLog(Log.CreateLog(String.Format("Access Point already on - {0}", networkOperatorTetheringManager.TetheringOperationalState.ToString()), Log.LogType.System)); } } else { logModel.AppendLog(Log.CreateLog("No default profile found", Log.LogType.System)); } } else { logModel.AppendLog(Log.CreateLog("No connection profile found", Log.LogType.System)); } } catch (Exception ex) { logModel.AppendLog(Log.CreateErrorLog("Error on Access Point init", ex)); } }); taskTethering.Wait(); bw.WorkerSupportsCancellation = true; bw.WorkerReportsProgress = true; bw.DoWork += Bw_DoWork; bw.RunWorkerCompleted += Bw_RunWorkerCompleted; bw.ProgressChanged += Bw_ProgressChanged; }
/// <summary> /// This is the click handler for the 'FindConnectionProfilesButton' button. You would replace this with your own handler /// if you have a button or buttons on this page. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FindConnectionProfilesButton_Click(object sender, RoutedEventArgs e) { Guid serviceProviderGuid; // Add values to the filter. If we don't care about a particular field, we don't set it. ConnectionProfileFilter filter = new ConnectionProfileFilter(); switch (((ComboBoxItem)IsConnectedComboBox.SelectedItem).Content.ToString()) { case "Yes": filter.IsConnected = true; break; case "No": filter.IsConnected = false; break; } switch (((ComboBoxItem)IsWlanProfileComboBox.SelectedItem).Content.ToString()) { case "Yes": filter.IsWlanConnectionProfile = true; break; case "No": filter.IsWlanConnectionProfile = false; break; } switch (((ComboBoxItem)IsWwanProfileComboBox.SelectedItem).Content.ToString()) { case "Yes": filter.IsWwanConnectionProfile = true; break; case "No": filter.IsWwanConnectionProfile = false; break; } switch (((ComboBoxItem)NetworkCostTypeComboBox.SelectedItem).Content.ToString()) { case "Unrestricted": filter.NetworkCostType = NetworkCostType.Unrestricted; break; case "Fixed": filter.NetworkCostType = NetworkCostType.Fixed; break; case "Variable": filter.NetworkCostType = NetworkCostType.Variable; break; case "Unknown": filter.NetworkCostType = NetworkCostType.Unknown; break; } if (Guid.TryParse(ServiceProviderGuidTextBox.Text, out serviceProviderGuid)) { filter.ServiceProviderGuid = serviceProviderGuid; } NetworkInformation.FindConnectionProfilesAsync(filter).Completed = FindConnectionProfilesCompletedHandler; }