diff --git a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs index f5f6280a21bdb5df4447cd4099adaee26c77a6a8..d13f6dddd174db3adcb7e8d5cb50b8f990fc0f52 100644 --- a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs +++ b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs @@ -21,7 +21,7 @@ namespace RobobinApp.ViewModels public class ConnectionPageViewModel : BaseViewModel { private BluetoothDevice _selectedDevice; - private IDevice _connectedDevice; // Track the connected device + private IDevice _connectedDevice; public ICommand GoHomeCommand { get; } public ObservableCollection<BluetoothDevice> BluetoothDevices { get; } public ObservableCollection<WifiNetwork> WifiNetworks { get; } @@ -69,6 +69,8 @@ namespace RobobinApp.ViewModels private readonly IBluetoothLE _bluetoothLE; public string ConnectButtonText => IsConnected ? "Disconnect" : "Connect"; private bool _isWifiNetworkSelectionVisible; + private WifiNetwork _selectedWifiNetwork; + public bool IsWifiNetworkSelectionVisible { get => _isWifiNetworkSelectionVisible; @@ -85,7 +87,7 @@ namespace RobobinApp.ViewModels BluetoothDevices = new ObservableCollection<BluetoothDevice>(); WifiNetworks = new ObservableCollection<WifiNetwork>(); ConnectCommand = new Command(OnConnect); - DisconnectCommand = new Command(OnDisconnect); // Initialize disconnect command + DisconnectCommand = new Command(OnDisconnect); SendWifiInfoCommand = new Command(OnSendWifiInfo); GoHomeCommand = new Command(async () => await OnGoHome()); ConnectCommand = new Command(OnToggleConnection); @@ -114,28 +116,39 @@ namespace RobobinApp.ViewModels Debug.Write("Scanning for networks"); var networks = wifiAdapter.NetworkReport.AvailableNetworks; + var addedSsids = new HashSet<string>(); // To keep track of added SSIDs + foreach (var network in networks) { - Debug.Write("This network exists" + network.Ssid); - var wifiNetwork = new WifiNetwork + Debug.Write("This network exists: " + network.Ssid); + + if (addedSsids.Add(network.Ssid)) { - SSID = network.Ssid, - SignalStrength = network.NetworkRssiInDecibelMilliwatts - }; + 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"); + 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 (SelectedWifiNetwork != null) + { + Debug.WriteLine("Wifi been selected"); + } if (!_isConnected || _connectedDevice == null) { Debug.WriteLine("Cannot send WiFi information: No device is connected."); @@ -147,7 +160,7 @@ namespace RobobinApp.ViewModels { Debug.WriteLine($"Sending WiFi information to {_connectedDevice.Name}..."); - // Retrieve the UART service + var services = await _connectedDevice.GetServicesAsync(); var uartService = services.FirstOrDefault(s => s.Id == Guid.Parse("6e400001-b5a3-f393-e0a9-e50e24dcca9e")); @@ -158,7 +171,6 @@ namespace RobobinApp.ViewModels return; } - // Get the RX characteristic for writing data var characteristics = await uartService.GetCharacteristicsAsync(); var rxCharacteristic = characteristics.FirstOrDefault(c => c.Id == Guid.Parse("6e400002-b5a3-f393-e0a9-e50e24dcca9e")); @@ -169,11 +181,9 @@ namespace RobobinApp.ViewModels return; } - // Prepare the data to send - string wifiInfo = $"{SSID},{Password}"; // Format as SSID,Password + string wifiInfo = $"{SSID},{Password}"; byte[] data = System.Text.Encoding.UTF8.GetBytes(wifiInfo); - // Write the data to the characteristic await rxCharacteristic.WriteAsync(data); Debug.WriteLine("WiFi information sent successfully."); @@ -196,15 +206,24 @@ namespace RobobinApp.ViewModels OnPropertyChanged(); } } + public WifiNetwork SelectedWifiNetwork + { + get => _selectedWifiNetwork; + set + { + _selectedWifiNetwork = value; + OnPropertyChanged(); + } + } private async void OnToggleConnection() { if (IsConnected) { - OnDisconnect(); // Call the existing disconnect method + OnDisconnect(); } else { - OnConnect(); // Call the existing connect method + OnConnect(); } } private async void CheckAndRequestBluetoothPermissions() @@ -290,8 +309,8 @@ namespace RobobinApp.ViewModels try { Debug.WriteLine($"Attempt {attempt}: Connecting to device {deviceId}"); - var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30)); // Create a cancellation token with a timeout - device = await _adapter.ConnectToKnownDeviceAsync(deviceId, new ConnectParameters(false, true), cancellationTokenSource.Token); // Pass the cancellation token + var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30)); + device = await _adapter.ConnectToKnownDeviceAsync(deviceId, new ConnectParameters(false, true), cancellationTokenSource.Token); return device; } catch (Exception ex) @@ -299,7 +318,7 @@ namespace RobobinApp.ViewModels Debug.WriteLine($"Attempt {attempt} failed: {ex.Message}"); if (attempt == retryCount) { - throw; // Rethrow the exception if we've exhausted the retries + throw; } } } @@ -319,10 +338,10 @@ namespace RobobinApp.ViewModels try { - await _adapter.StopScanningForDevicesAsync(); // Ensure scanning is stopped. + await _adapter.StopScanningForDevicesAsync(); Debug.WriteLine("Stopped scanning for devices."); IsConnected = true; - _connectedDevice = await TryConnectAsync(Guid.Parse(SelectedDevice.MacAddress)); // Use retry logic for connection + _connectedDevice = await TryConnectAsync(Guid.Parse(SelectedDevice.MacAddress)); Debug.WriteLine($"Successfully connected to {SelectedDevice.Name}."); var services = await _connectedDevice.GetServicesAsync(); @@ -407,6 +426,8 @@ namespace RobobinApp.ViewModels Debug.WriteLine("No device to disconnect."); } } + } + } diff --git a/App/RobobinApp/Views/ConnectionPage.xaml b/App/RobobinApp/Views/ConnectionPage.xaml index 61bdfc0e854233dc5bf7b1ea7d08228c2f8075ed..a7df6ffadd4c4fb42093405a20254422f15cf401 100644 --- a/App/RobobinApp/Views/ConnectionPage.xaml +++ b/App/RobobinApp/Views/ConnectionPage.xaml @@ -49,7 +49,9 @@ <ListView x:Name="DeviceListView" ItemsSource="{Binding BluetoothDevices}" SelectedItem="{Binding SelectedDevice}" - VerticalOptions="FillAndExpand"> + VerticalOptions="FillAndExpand" + ItemTapped="DeviceListView_ItemTapped"> + <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding Name}" /> diff --git a/App/RobobinApp/Views/ConnectionPage.xaml.cs b/App/RobobinApp/Views/ConnectionPage.xaml.cs index 27c0fce5902a1796e1ef0d37fee26d545d4fbfc0..0d4674816737d8d2594ea102472bc83ea16ee250 100644 --- a/App/RobobinApp/Views/ConnectionPage.xaml.cs +++ b/App/RobobinApp/Views/ConnectionPage.xaml.cs @@ -7,4 +7,8 @@ public partial class ConnectionPage : ContentPage InitializeComponent(); } + private void DeviceListView_ItemTapped(object sender, ItemTappedEventArgs e) + { + + } } \ No newline at end of file