Skip to content
Snippets Groups Projects
Commit a564f3f6 authored by Paul-Winpenny's avatar Paul-Winpenny
Browse files

Get SSID working, bindings are wrong.

parent e0ff20bd
No related branches found
No related tags found
1 merge request!1App now has a basic structure and BLE support
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
......@@ -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.");
}
}
}
}
......@@ -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}" />
......
......@@ -7,4 +7,8 @@ public partial class ConnectionPage : ContentPage
InitializeComponent();
}
private void DeviceListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment