Wifi Connector sample updates and fixes (#101)

* Do not enable cmdline if process running
* Wifi Connect sample fixes
    * Fix compilation warnings
    * Enable connecting to hidden networks
    * Remove unused Xaml elements, ConnectionKinds
This commit is contained in:
Mahmoud Saleh 2018-02-27 16:07:35 -08:00 коммит произвёл GitHub
Родитель d9e58093a6
Коммит a68fe9ef95
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 92 добавлений и 77 удалений

Просмотреть файл

@ -70,7 +70,7 @@ namespace IoTCoreDefaultApp
private IAsyncOperation<ProcessLauncherResult> processLauncherOperation;
private int currentCommandLine = -1;
private int totalOutputSize = 0;
private bool isProcessRunning = true;
private bool isProcessRunning = false;
private bool isProcessTimedOut = false;
private bool isProcessingAdminCommand = false;
private DateTime lastOutputTime = DateTime.Now;
@ -111,8 +111,11 @@ namespace IoTCoreDefaultApp
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
EnableCommandLineTextBox(true, CommandLine);
StdOutputScroller.ChangeView(null, StdOutputScroller.ScrollableHeight, null, true);
if (!isProcessRunning && !isProcessingAdminCommand)
{
EnableCommandLineTextBox(true, CommandLine);
StdOutputScroller.ChangeView(null, StdOutputScroller.ScrollableHeight, null, true);
}
}
private void RunProcess()

Просмотреть файл

@ -20,12 +20,19 @@ namespace WiFiConnect
this.adapter = adapter;
}
public async Task UpdateAsync()
public async void Update()
{
UpdateWiFiImage();
UpdateNetworkKeyVisibility();
await UpdateConnectivityLevel();
await UpdateWpsPushbuttonAvailable();
UpdateHiddenSsidTextBoxVisibility();
await UpdateConnectivityLevelAsync();
await UpdateWpsPushButtonAvailableAsync();
}
private void UpdateHiddenSsidTextBoxVisibility()
{
IsHiddenNetwork = string.IsNullOrEmpty(AvailableNetwork.Ssid);
OnPropertyChanged("IsHiddenNetwork");
}
private void UpdateNetworkKeyVisibility()
@ -59,7 +66,7 @@ namespace WiFiConnect
}
public async Task UpdateConnectivityLevel()
public async Task UpdateConnectivityLevelAsync()
{
string connectivityLevel = "Not Connected";
string connectedSsid = null;
@ -74,7 +81,8 @@ namespace WiFiConnect
if (!string.IsNullOrEmpty(connectedSsid))
{
if (connectedSsid.Equals(AvailableNetwork.Ssid))
if (connectedSsid.Equals(AvailableNetwork.Ssid) ||
connectedSsid.Equals(HiddenSsid))
{
connectivityLevel = connectedProfile.GetNetworkConnectivityLevel().ToString();
}
@ -84,8 +92,8 @@ namespace WiFiConnect
OnPropertyChanged("ConnectivityLevel");
}
public async Task UpdateWpsPushbuttonAvailable()
{
public async Task UpdateWpsPushButtonAvailableAsync()
{
IsWpsPushButtonAvailable = await IsWpsPushButtonAvailableAsync();
OnPropertyChanged("IsWpsPushButtonAvailable");
}
@ -99,6 +107,8 @@ namespace WiFiConnect
public bool NetworkKeyInfoVisibility { get; set; }
public bool IsHiddenNetwork { get; set; }
private bool usePassword = false;
public bool UsePassword
{
@ -131,7 +141,7 @@ namespace WiFiConnect
{
get
{
return availableNetwork.Ssid;
return string.IsNullOrEmpty(AvailableNetwork.Ssid) ? "Hidden Network" : AvailableNetwork.Ssid;
}
}
@ -139,7 +149,7 @@ namespace WiFiConnect
{
get
{
return availableNetwork.Bssid;
return AvailableNetwork.Bssid;
}
}
@ -148,7 +158,7 @@ namespace WiFiConnect
{
get
{
return string.Format("{0}kHz", availableNetwork.ChannelCenterFrequencyInKilohertz);
return string.Format("{0}kHz", AvailableNetwork.ChannelCenterFrequencyInKilohertz);
}
}
@ -156,7 +166,7 @@ namespace WiFiConnect
{
get
{
return string.Format("{0}dBm", availableNetwork.NetworkRssiInDecibelMilliwatts);
return string.Format("{0}dBm", AvailableNetwork.NetworkRssiInDecibelMilliwatts);
}
}
@ -164,9 +174,10 @@ namespace WiFiConnect
{
get
{
return string.Format("Authentication: {0}; Encryption: {1}", availableNetwork.SecuritySettings.NetworkAuthenticationType, availableNetwork.SecuritySettings.NetworkEncryptionType);
return string.Format("Authentication: {0}; Encryption: {1}", AvailableNetwork.SecuritySettings.NetworkAuthenticationType, AvailableNetwork.SecuritySettings.NetworkEncryptionType);
}
}
public String ConnectivityLevel
{
get;
@ -200,12 +211,19 @@ namespace WiFiConnect
set { domain = value; OnPropertyChanged("Domain"); }
}
private string hiddenSsid;
public string HiddenSsid
{
get { return hiddenSsid; }
set { hiddenSsid = value; OnPropertyChanged("HiddenSsid"); }
}
public bool IsEapAvailable
{
get
{
return ((availableNetwork.SecuritySettings.NetworkAuthenticationType == NetworkAuthenticationType.Rsna) ||
(availableNetwork.SecuritySettings.NetworkAuthenticationType == NetworkAuthenticationType.Wpa));
return ((AvailableNetwork.SecuritySettings.NetworkAuthenticationType == NetworkAuthenticationType.Rsna) ||
(AvailableNetwork.SecuritySettings.NetworkAuthenticationType == NetworkAuthenticationType.Wpa));
}
}
@ -213,36 +231,19 @@ namespace WiFiConnect
{
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5, 0))
{
var result = await adapter.GetWpsConfigurationAsync(availableNetwork);
var result = await adapter.GetWpsConfigurationAsync(AvailableNetwork);
if (result.SupportedWpsKinds.Contains(WiFiWpsKind.PushButton))
return true;
}
return false;
}
private WiFiAvailableNetwork availableNetwork;
public WiFiAvailableNetwork AvailableNetwork
{
get
{
return availableNetwork;
}
private set
{
availableNetwork = value;
}
}
public WiFiAvailableNetwork AvailableNetwork { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}

Просмотреть файл

@ -14,7 +14,6 @@
x:Class="WiFiConnect.WiFiConnect_Scenario"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WiFiConnect"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
@ -26,7 +25,6 @@
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="Ssid:" Margin="0,0,5,0" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Path=Ssid}" FontWeight="Bold" TextWrapping="Wrap" VerticalAlignment="Center" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Path=ConnectionKinds}" TextWrapping="Wrap" VerticalAlignment="Center" Margin="0,0,5,0" Visibility="{Binding Path=ConnectionKindsVisibility}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
@ -38,7 +36,6 @@
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="Ssid:" VerticalAlignment="Center" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Path=Ssid}" FontWeight="Bold" TextWrapping="Wrap" Margin="0,0,5,0" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Path=ConnectionKinds}" TextWrapping="Wrap" Margin="0,0,5,0" VerticalAlignment="Center" Visibility="{Binding Path=ConnectionKindsVisibility}"/>
</StackPanel>
</StackPanel>
@ -65,6 +62,10 @@
<StackPanel Orientation="Vertical" Margin="0,5">
<CheckBox IsChecked="{Binding Path=ConnectAutomatically, Mode=TwoWay}">Connect automatically</CheckBox>
<StackPanel x:Name="HiddenSsidPanel" Visibility="{Binding Path=IsHiddenNetwork}">
<TextBlock Text="Enter the name (SSID) for the network:" Margin="5"/>
<TextBox HorizontalAlignment="Stretch" Margin="5" Text="{Binding Path=HiddenSsid, Mode=TwoWay}"/>
</StackPanel>
<CheckBox IsChecked="{Binding Path=UsePassword, Mode=TwoWay}" Visibility="{Binding Path=IsEapAvailable}">Use password</CheckBox>
<Grid x:Name="EapInfo" Visibility="{Binding Path=UsePassword}">
<Grid.RowDefinitions>
@ -76,10 +77,10 @@
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="User:" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5"/>
<TextBox Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" Margin="5" Text="{Binding Path=UserName, Mode=TwoWay}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Domain:" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5"/>
<TextBox Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" Margin="5" Text="{Binding Path=Domain, Mode=TwoWay}"/>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Domain:" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5"/>
<TextBox Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" Margin="5" Text="{Binding Path=Domain, Mode=TwoWay}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="User:" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5"/>
<TextBox Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" Margin="5" Text="{Binding Path=UserName, Mode=TwoWay}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Password:" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5"/>
<PasswordBox Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch" Margin="5" Password="{Binding Path=Password, Mode=TwoWay}"/>
</Grid>
@ -142,7 +143,6 @@
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="Ssid:" VerticalAlignment="Center" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Path=Ssid}" FontWeight="Bold" TextWrapping="Wrap" Margin="0,0,5,0" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Path=ConnectionKinds}" TextWrapping="Wrap" Margin="0,0,5,0" VerticalAlignment="Center" Visibility="{Binding Path=ConnectionKindsVisibility}"/>
</StackPanel>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="5">

Просмотреть файл

@ -83,7 +83,8 @@ namespace WiFiConnect
rootPage.NotifyUser(String.Format("Error scanning WiFi adapter: 0x{0:X}: {1}", err.HResult, err.Message), NotifyType.ErrorMessage);
return;
}
await DisplayNetworkReportAsync(firstAdapter.NetworkReport);
DisplayNetworkReport(firstAdapter.NetworkReport);
}
public string GetCurrentWifiNetwork()
@ -126,7 +127,7 @@ namespace WiFiConnect
return false;
}
private async Task DisplayNetworkReportAsync(WiFiNetworkReport report)
private void DisplayNetworkReport(WiFiNetworkReport report)
{
rootPage.NotifyUser(string.Format("Network Report Timestamp: {0}", report.Timestamp), NotifyType.StatusMessage);
@ -135,17 +136,22 @@ namespace WiFiConnect
foreach (var network in report.AvailableNetworks)
{
var item = new WiFiNetworkDisplay(network, firstAdapter);
if (!String.IsNullOrEmpty(network.Ssid))
{
var item = new WiFiNetworkDisplay(network, firstAdapter);
dictionary.TryAdd(network.Ssid, item);
}
else
{
string bssid = network.Bssid.Substring(0, network.Bssid.LastIndexOf(":"));
dictionary.TryAdd(bssid, item);
}
}
var values = dictionary.Values;
foreach (var item in values)
{
/*await*/ item.UpdateAsync();
item.Update();
if (IsConnected(item.AvailableNetwork))
{
ResultCollection.Insert(0, item);
@ -228,6 +234,22 @@ namespace WiFiConnect
rootPage.NotifyUser("Network not selected", NotifyType.ErrorMessage);
return;
}
var ssid = selectedNetwork.AvailableNetwork.Ssid;
if (string.IsNullOrEmpty(ssid))
{
if (string.IsNullOrEmpty(selectedNetwork.HiddenSsid))
{
rootPage.NotifyUser("Ssid required for connection to hidden network.", NotifyType.ErrorMessage);
return;
}
else
{
ssid = selectedNetwork.HiddenSsid;
}
}
WiFiReconnectionKind reconnectionKind = WiFiReconnectionKind.Manual;
if(selectedNetwork.ConnectAutomatically)
{
@ -240,46 +262,35 @@ namespace WiFiConnect
{
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5, 0))
{
didConnect = firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind, null, String.Empty, WiFiConnectionMethod.WpsPushButton).AsTask<WiFiConnectionResult>();
didConnect = firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind, null, string.Empty, WiFiConnectionMethod.WpsPushButton).AsTask();
}
}
else if (selectedNetwork.IsEapAvailable)
else
{
if (selectedNetwork.UsePassword)
PasswordCredential credential = new PasswordCredential();
if (selectedNetwork.IsEapAvailable && selectedNetwork.UsePassword)
{
var credential = new PasswordCredential();
if (!String.IsNullOrEmpty(selectedNetwork.Domain))
{
credential.Resource = selectedNetwork.Domain;
}
credential.UserName = selectedNetwork.UserName ?? "";
credential.Password = selectedNetwork.Password ?? "";
}
else if (!String.IsNullOrEmpty(selectedNetwork.Password))
{
credential.Password = selectedNetwork.Password;
}
didConnect = firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind, credential).AsTask<WiFiConnectionResult>();
if (selectedNetwork.IsHiddenNetwork)
{
// Hidden networks require the SSID to be supplied
didConnect = firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind, credential, ssid).AsTask();
}
else
{
didConnect = firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind).AsTask<WiFiConnectionResult>();
}
}
else if (selectedNetwork.AvailableNetwork.SecuritySettings.NetworkAuthenticationType == Windows.Networking.Connectivity.NetworkAuthenticationType.Open80211 &&
selectedNetwork.AvailableNetwork.SecuritySettings.NetworkEncryptionType == NetworkEncryptionType.None)
{
didConnect = firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind).AsTask<WiFiConnectionResult>();
}
else
{
// Only the password potion of the credential need to be supplied
if (String.IsNullOrEmpty(selectedNetwork.Password))
{
didConnect = firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind).AsTask<WiFiConnectionResult>();
}
else
{
var credential = new PasswordCredential();
credential.Password = selectedNetwork.Password ?? "";
didConnect = firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind, credential).AsTask<WiFiConnectionResult>();
didConnect = firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind, credential).AsTask();
}
}
@ -308,14 +319,14 @@ namespace WiFiConnect
}
else
{
rootPage.NotifyUser(string.Format("Could not connect to {0}. Error: {1}", selectedNetwork.Ssid, result.ConnectionStatus), NotifyType.ErrorMessage);
rootPage.NotifyUser(string.Format("Could not connect to {0}. Error: {1}", selectedNetwork.Ssid, (result != null ? result.ConnectionStatus : WiFiConnectionStatus.UnspecifiedFailure )), NotifyType.ErrorMessage);
SwitchToItemState(selectedNetwork, WifiConnectState, false);
}
// Since a connection attempt was made, update the connectivity level displayed for each
foreach (var network in ResultCollection)
{
network.UpdateConnectivityLevel();
var task = network.UpdateConnectivityLevelAsync();
}
}