From e0ff20bde8cdfd6552246f4f707cf4012de1a347 Mon Sep 17 00:00:00 2001
From: Paul-Winpenny <92634321+Paul-Winpenny@users.noreply.github.com>
Date: Fri, 18 Oct 2024 16:29:25 +0100
Subject: [PATCH] Wifi List shows up, need to bind the selected device to SSID
 in viewmodel

---
 App/RobobinApp/Models/WifiNetwork.cs          |  2 +-
 App/RobobinApp/RobobinApp.csproj              |  2 +-
 .../ViewModels/ConnectionPageViewModel.cs     | 43 ++++++++++++++++++-
 App/RobobinApp/Views/ConnectionPage.xaml      |  3 +-
 4 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/App/RobobinApp/Models/WifiNetwork.cs b/App/RobobinApp/Models/WifiNetwork.cs
index 0e0e8303..0a688fbe 100644
--- a/App/RobobinApp/Models/WifiNetwork.cs
+++ b/App/RobobinApp/Models/WifiNetwork.cs
@@ -9,6 +9,6 @@ namespace RobobinApp.Models
     public class WifiNetwork
     {
         public string SSID { get; set; }
-        public string SignalStrength { get; set; }
+        public double SignalStrength { get; set; }
     }
 }
diff --git a/App/RobobinApp/RobobinApp.csproj b/App/RobobinApp/RobobinApp.csproj
index 47ae3c5d..7d3108fc 100644
--- a/App/RobobinApp/RobobinApp.csproj
+++ b/App/RobobinApp/RobobinApp.csproj
@@ -1,6 +1,6 @@
 <Project Sdk="Microsoft.NET.Sdk">
     <PropertyGroup>
-        <TargetFrameworks>net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
+		<!-->TargetFrameworks>net8.0-ios;net8.0-maccatalyst</TargetFrameworks-->
         <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
         <OutputType>Exe</OutputType>
 		<RootNamespace>Robobin</RootNamespace>
diff --git a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
index 8a728f5b..f5f6280a 100644
--- a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
+++ b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
@@ -13,6 +13,9 @@ using RobobinApp.Models;
 using System.Net.NetworkInformation; 
 using System.Collections.Generic;
 
+using Windows.Networking.Connectivity;
+using Windows.Devices.WiFi;
+
 namespace RobobinApp.ViewModels
 {
     public class ConnectionPageViewModel : BaseViewModel
@@ -80,6 +83,7 @@ namespace RobobinApp.ViewModels
         public ConnectionPageViewModel()
         {
             BluetoothDevices = new ObservableCollection<BluetoothDevice>();
+            WifiNetworks = new ObservableCollection<WifiNetwork>();
             ConnectCommand = new Command(OnConnect);
             DisconnectCommand = new Command(OnDisconnect);  // Initialize disconnect command
             SendWifiInfoCommand = new Command(OnSendWifiInfo);
@@ -92,8 +96,45 @@ namespace RobobinApp.ViewModels
 
             Debug.WriteLine("Checking and requesting Bluetooth permissions.");
             CheckAndRequestBluetoothPermissions();
+            ScanForWifiNetworks();
+        }
+        private async void ScanForWifiNetworks()
+        {
+            WifiNetworks.Clear();
+            Debug.WriteLine("Scanning for Wi-Fi networks...");
+
+            var wifiAdapter = await GetWifiAdapterAsync();
+            if (wifiAdapter == null)
+            {
+                Debug.WriteLine("Wi-Fi adapter not found.");
+                return;
+            }
+
+            await wifiAdapter.ScanAsync();
+            Debug.Write("Scanning for networks");
+            var networks = wifiAdapter.NetworkReport.AvailableNetworks;
+
+            foreach (var network in networks)
+            {
+                Debug.Write("This network exists" +  network.Ssid);
+                var wifiNetwork = new WifiNetwork
+                {
+                    SSID = network.Ssid,
+                    SignalStrength = network.NetworkRssiInDecibelMilliwatts
+                };
+
+                Device.BeginInvokeOnMainThread(() => WifiNetworks.Add(wifiNetwork));
+                Debug.WriteLine($"Found Wi-Fi network: {wifiNetwork.SSID}, Signal Strength: {wifiNetwork.SignalStrength} dBm");
+            }
+        }
+
+        private async Task<WiFiAdapter> GetWifiAdapterAsync()
+        {
+            var result = await WiFiAdapter.FindAllAdaptersAsync();
+            return result.FirstOrDefault(); // Get the first available Wi-Fi adapter
         }
-        private async void OnSendWifiInfo(object obj)
+    
+    private async void OnSendWifiInfo(object obj)
         {
             if (!_isConnected || _connectedDevice == null)
             {
diff --git a/App/RobobinApp/Views/ConnectionPage.xaml b/App/RobobinApp/Views/ConnectionPage.xaml
index d7592573..61bdfc0e 100644
--- a/App/RobobinApp/Views/ConnectionPage.xaml
+++ b/App/RobobinApp/Views/ConnectionPage.xaml
@@ -90,7 +90,7 @@
                    VerticalOptions="Start" />
 
             <ListView x:Name="WifiListView"
-                      ItemsSource="{Binding AvailableWifiNetworks}"
+                      ItemsSource="{Binding WifiNetworks}"
                       SelectedItem="{Binding SelectedWifiNetwork}"
                       VerticalOptions="FillAndExpand">
                 <ListView.ItemTemplate>
@@ -100,7 +100,6 @@
                 </ListView.ItemTemplate>
             </ListView>
 
-            <!-- Section for Password Input -->
             <StackLayout Padding="10" 
                          BackgroundColor="LightGray"
                          HorizontalOptions="FillAndExpand" 
-- 
GitLab