Skip to content

Commit

Permalink
Added capability to connect to wifi network
Browse files Browse the repository at this point in the history
Very basic
  • Loading branch information
graboskyc committed May 16, 2017
1 parent 6864dc4 commit d93aa74
Show file tree
Hide file tree
Showing 17 changed files with 322 additions and 29 deletions.
Binary file added IoTBrowser/IoTBrowser/Assets/open_1bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IoTBrowser/IoTBrowser/Assets/open_2bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IoTBrowser/IoTBrowser/Assets/open_3bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IoTBrowser/IoTBrowser/Assets/open_4bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IoTBrowser/IoTBrowser/Assets/open_5bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IoTBrowser/IoTBrowser/Assets/secure_1bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IoTBrowser/IoTBrowser/Assets/secure_2bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IoTBrowser/IoTBrowser/Assets/secure_3bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IoTBrowser/IoTBrowser/Assets/secure_4bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added IoTBrowser/IoTBrowser/Assets/secure_5bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions IoTBrowser/IoTBrowser/IoTBrowser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WifiPage.xaml.cs">
<DependentUpon>WifiPage.xaml</DependentUpon>
</Compile>
<Compile Include="WifiScanner.cs" />
</ItemGroup>
<ItemGroup>
<AppxManifest Include="Package.appxmanifest">
Expand All @@ -110,6 +114,16 @@
<None Include="IoTBrowser_TemporaryKey.pfx" />
</ItemGroup>
<ItemGroup>
<Content Include="Assets\open_1bar.png" />
<Content Include="Assets\open_2bar.png" />
<Content Include="Assets\open_3bar.png" />
<Content Include="Assets\open_4bar.png" />
<Content Include="Assets\open_5bar.png" />
<Content Include="Assets\secure_1bar.png" />
<Content Include="Assets\secure_2bar.png" />
<Content Include="Assets\secure_3bar.png" />
<Content Include="Assets\secure_4bar.png" />
<Content Include="Assets\secure_5bar.png" />
<Content Include="Properties\Default.rd.xml" />
<Content Include="Assets\LockScreenLogo.scale-200.png" />
<Content Include="Assets\SplashScreen.scale-200.png" />
Expand All @@ -128,6 +142,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="WifiPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
Expand Down
2 changes: 2 additions & 0 deletions IoTBrowser/IoTBrowser/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" x:Name="Web_Address" FontSize="24" TextWrapping="Wrap" Text="http://www.bing.com" VerticalAlignment="Center" VerticalContentAlignment="Center" KeyUp="Web_Address_KeyUp" HorizontalAlignment="Stretch"/>
<Button Grid.Column="2" x:Name="Go_Web" Content="Go!" HorizontalAlignment="Right" VerticalAlignment="Center" Height="40" Width="45" Click="Go_Web_Click"/>
<Button Grid.Column="3" x:Name="Go_Wifi" Content="Wifi" HorizontalAlignment="Right" VerticalAlignment="Center" Height="40" Width="45" Click="Go_Wifi_Click"/>
</Grid>
</ScrollViewer>
</Grid>
Expand Down
5 changes: 5 additions & 0 deletions IoTBrowser/IoTBrowser/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ private void Go_Web_Click(object sender, RoutedEventArgs e)
DoWebNavigate();
}

private void Go_Wifi_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(WifiPage));
}


private void Web_Address_KeyUp(object sender, KeyRoutedEventArgs e)
{
Expand Down
39 changes: 10 additions & 29 deletions IoTBrowser/IoTBrowser/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -1,49 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>

<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
IgnorableNamespaces="uap mp">

<Identity
Name="655322c3-787d-4e4a-947e-cd2a5db6577a"
Publisher="CN=chris"
Version="1.0.0.0" />

<mp:PhoneIdentity PhoneProductId="655322c3-787d-4e4a-947e-cd2a5db6577a" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp">
<Identity Name="655322c3-787d-4e4a-947e-cd2a5db6577a" Publisher="CN=chris" Version="1.1.1.0" />
<mp:PhoneIdentity PhoneProductId="655322c3-787d-4e4a-947e-cd2a5db6577a" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>IoTBrowser</DisplayName>
<PublisherDisplayName>chris</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>

<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
</Dependencies>

<Resources>
<Resource Language="x-generate"/>
<Resource Language="x-generate" />
</Resources>

<Applications>
<Application Id="App"
Executable="$targetnametoken$.exe"
EntryPoint="IoTBrowser.App">
<uap:VisualElements
DisplayName="IoTBrowser"
Square150x150Logo="Assets\Square150x150Logo.png"
Square44x44Logo="Assets\Square44x44Logo.png"
Description="IoTBrowser"
BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="IoTBrowser.App">
<uap:VisualElements DisplayName="IoTBrowser" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="IoTBrowser" BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png">
</uap:DefaultTile>
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>

<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="location" />
<DeviceCapability Name="wifiControl" />
</Capabilities>
</Package>
61 changes: 61 additions & 0 deletions IoTBrowser/IoTBrowser/WifiPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<Page
x:Class="IoTBrowser.WifiPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:IoTBrowser"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">


<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
<RowDefinition Height="*"/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>

<TextBlock FontSize="36" Grid.RowSpan="2" >WiFi Scanner</TextBlock>

<ScrollViewer Grid.Row="1" HorizontalAlignment="Stretch" HorizontalScrollBarVisibility ="Auto" VerticalScrollBarVisibility="Auto">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Name="btnScan" Click="btnScan_Click" Grid.Column="0">Scan For Networks</Button>
<Button Name="btnBrowser" Click="btnBrowser_Click" Grid.Column="1">Back To Browser</Button>
</Grid>
</ScrollViewer>


<ListView x:Name="report_lv" Grid.Row="2" ItemClick="report_lv_ItemClick" IsItemClickEnabled="True">
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="54"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="{Binding Icon}" Width="44" Height="44" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding SSID}" FontSize="36" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>



<ScrollViewer Grid.Row="3" HorizontalAlignment="Stretch" HorizontalScrollBarVisibility ="Auto" VerticalScrollBarVisibility="Auto">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Name="txtPassword" HorizontalAlignment="Stretch" FontSize="24" VerticalAlignment="Center" VerticalContentAlignment="Center" Text="asd"></TextBox>
<Button Grid.Column="1" Name="btnConnect" Click="btnConnect_Click" HorizontalAlignment="Right" VerticalAlignment="Center" Height="40" Width="95">Connect</Button>
</Grid>
</ScrollViewer>
</Grid>
</Page>
164 changes: 164 additions & 0 deletions IoTBrowser/IoTBrowser/WifiPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.WiFi;
using System.Collections.ObjectModel;
using Windows.UI.Popups;
using System.Threading.Tasks;
using System.Text;
using Windows.Networking.Connectivity;
using Windows.Security.Credentials;
using Windows.Devices.Enumeration;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace IoTBrowser
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class WifiPage : Page
{
private WiFiScanner _wifiScanner;
private bool _scannerInitialized = false;
private string _selectedSSID = "";
WiFiNetworkReport _report;

public WifiPage()
{
this.InitializeComponent();

this._wifiScanner = new WiFiScanner();
}

private async void Page_Loaded(object sender, RoutedEventArgs e)
{
await InitializeScanner();
}

private async Task InitializeScanner()
{
if (!_scannerInitialized)
{
await this._wifiScanner.InitializeScanner();
_scannerInitialized = true;
}
}

private async void btnScan_Click(object sender, RoutedEventArgs e)
{
await InitializeScanner();
this.btnScan.IsEnabled = false;

try
{
await RunWifiScan();
}
catch (Exception ex)
{
MessageDialog md = new MessageDialog(ex.Message);
await md.ShowAsync();
}

this.btnScan.IsEnabled = true;
}


private async Task RunWifiScan()
{
await this._wifiScanner.ScanForNetworks();

_report = this._wifiScanner.WiFiAdapter.NetworkReport;

ObservableCollection<WifiList> ocwl = new ObservableCollection<WifiList>();

foreach (var availableNetwork in _report.AvailableNetworks)
{

if (availableNetwork.Ssid.Length > 1)
{
string icon = "";
if(availableNetwork.SecuritySettings.NetworkEncryptionType.ToString() == "None") { icon = "open_"; }
else { icon = "secure_"; }
icon = icon + availableNetwork.SignalBars + "bar.png";

WifiList wl = new WifiList { Icon = "Assets/"+icon, SSID = availableNetwork.Ssid };

ocwl.Add(wl);
}
}

report_lv.ItemsSource = ocwl;


}


private async Task ShowMessage(string message)
{
var dialog = new MessageDialog(message);
await dialog.ShowAsync();
}

private void btnBrowser_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(MainPage));
}

private async void btnConnect_Click(object sender, RoutedEventArgs e)
{

var access = await WiFiAdapter.RequestAccessAsync();
var wifiAdapterResults = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector());
WiFiAdapter wa = await WiFiAdapter.FromIdAsync(wifiAdapterResults[0].Id);

WiFiReconnectionKind reconnectionKind = WiFiReconnectionKind.Automatic;
WiFiConnectionResult result;

foreach (var availableNetwork in _report.AvailableNetworks)
{
if (availableNetwork.Ssid == _selectedSSID)
{
if (txtPassword.Text.Length > 1)
{
var credential = new PasswordCredential();
credential.Password = txtPassword.Text;
result = await wa.ConnectAsync(availableNetwork, reconnectionKind, credential);
}
else
{
result = await wa.ConnectAsync(availableNetwork, reconnectionKind);
}
}
}



}

private void report_lv_ItemClick(object sender, ItemClickEventArgs e)
{

WifiList item = (WifiList)e.ClickedItem;
//txtPassword.Text = item.SSID;
_selectedSSID = item.SSID;
}
}
}

public class WifiList
{
public string SSID { get; set; }
public string Icon { get; set; }
}
Loading

0 comments on commit d93aa74

Please sign in to comment.