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

Bluetooth connection (potentially) scanning, windows only

parent 654aa726
Branches
No related tags found
1 merge request!1App now has a basic structure and BLE support
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\App.sln",
"PreviewInSolutionExplorer": false
}
\ No newline at end of file
No preview for this file type
using Microsoft.Extensions.Logging;

using Shiny;
namespace RobobinApp;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
public static MauiApp CreateMauiApp()
{
var builder = MauiApp
.CreateBuilder()
.UseMauiApp<App>()
.UseShiny()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
#if DEBUG
builder.Logging.AddDebug();
#endif
builder.Services.AddBluetoothLE();
return builder.Build();
}
}
return builder.Build();
}
}
\ No newline at end of file
......@@ -58,9 +58,13 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.91" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.91" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
<PackageReference Include="Plugin.BLE" Version="3.1.0" />
<PackageReference Include="Shiny" Version="2.7.3" />
<PackageReference Include="Shiny.BluetoothLE" Version="3.3.3" />
<PackageReference Include="Shiny.Hosting.Maui" Version="3.3.3" />
</ItemGroup>
<ItemGroup>
......
......@@ -4,6 +4,11 @@
<IsFirstTimeProjectOpen>False</IsFirstTimeProjectOpen>
<ActiveDebugFramework>net8.0-windows10.0.19041.0</ActiveDebugFramework>
<ActiveDebugProfile>Windows Machine</ActiveDebugProfile>
<SelectedPlatformGroup>Emulator</SelectedPlatformGroup>
<DefaultDevice>pixel_5_-_api_34</DefaultDevice>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<ItemGroup>
<MauiXaml Update="Views\ConnectionPage.xaml">
......
......@@ -2,6 +2,9 @@
using System.Windows.Input;
using Microsoft.Maui.Controls;
using RobobinApp.Views;
using Plugin.BLE;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions.Exceptions;
namespace RobobinApp.ViewModels
{
......@@ -12,33 +15,74 @@ namespace RobobinApp.ViewModels
public ObservableCollection<BluetoothDevice> BluetoothDevices { get; }
public ICommand ConnectCommand { get; }
// BLE plugin fields
private readonly IAdapter _adapter;
private readonly IBluetoothLE _bluetoothLE;
public ConnectionPageViewModel()
{
BluetoothDevices = new ObservableCollection<BluetoothDevice>();
ConnectCommand = new Command(OnConnect);
LoadBluetoothDevices(); // Load available devices
GoHomeCommand = new Command(async () => await OnGoHome());
// Initialize BLE manager and adapter
_bluetoothLE = CrossBluetoothLE.Current;
_adapter = _bluetoothLE.Adapter;
ScanDevices(); // Start scanning for devices
}
public async Task OnGoHome()
{
await Application.Current.MainPage.Navigation.PushAsync(new MainPage());
}
public BluetoothDevice SelectedDevice
{
get => _selectedDevice;
set
{
_selectedDevice = value;
OnPropertyChanged(); // Notify UI that SelectedDevice has changed
OnPropertyChanged();
}
}
private async void LoadBluetoothDevices()
public async Task OnGoHome()
{
// Logic to load available Bluetooth devices
// This is a placeholder for your Bluetooth device discovery logic
BluetoothDevices.Add(new BluetoothDevice { Name = "Device 1" });
BluetoothDevices.Add(new BluetoothDevice { Name = "Device 2" });
await Application.Current.MainPage.Navigation.PushAsync(new MainPage());
}
private async void ScanDevices()
{
// Clear the current list of devices before scanning
BluetoothDevices.Clear();
try
{
if (!_bluetoothLE.IsAvailable)
{
await Application.Current.MainPage.DisplayAlert("Bluetooth", "Bluetooth is not available.", "OK");
return;
}
if (!_bluetoothLE.IsOn)
{
await Application.Current.MainPage.DisplayAlert("Bluetooth", "Please turn on Bluetooth.", "OK");
return;
}
_adapter.DeviceDiscovered += (s, a) =>
{
// Add discovered devices to the list
BluetoothDevices.Add(new BluetoothDevice
{
Name = a.Device.Name ?? "Unknown Device",
MacAddress = a.Device.Id.ToString()
});
};
// Start scanning for devices
await _adapter.StartScanningForDevicesAsync();
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Error", $"Failed to scan for devices: {ex.Message}", "OK");
}
}
private async void OnConnect()
......@@ -47,12 +91,29 @@ namespace RobobinApp.ViewModels
// Logic to connect to the selected Bluetooth device
await Application.Current.MainPage.DisplayAlert("Connection", $"Connecting to {SelectedDevice.Name}...", "OK");
// Implement actual connection logic here
try
{
// Stop scanning before attempting connection
await _adapter.StopScanningForDevicesAsync();
// Find the device using the MAC address
var device = await _adapter.ConnectToKnownDeviceAsync(Guid.Parse(SelectedDevice.MacAddress));
// Display successful connection
await Application.Current.MainPage.DisplayAlert("Connected", $"Connected to {SelectedDevice.Name}.", "OK");
}
catch (DeviceConnectionException ex)
{
// Handle connection failure
await Application.Current.MainPage.DisplayAlert("Connection Error", $"Failed to connect: {ex.Message}", "OK");
}
}
}
public class BluetoothDevice // Define this class to represent a Bluetooth device
public class BluetoothDevice
{
public string Name { get; set; }
public string MacAddress { get; set; }
}
}
......@@ -3,6 +3,7 @@ using System.Runtime.CompilerServices;
using System.Windows.Input;
using Microsoft.Maui.Controls;
using RobobinApp.Views;
using Shiny.BluetoothLE;
namespace RobobinApp.ViewModels
......@@ -11,11 +12,12 @@ namespace RobobinApp.ViewModels
{
private bool _isBusy;
private string _statusMessage;
public ICommand ConnectToRobobinCommand { get; }
public MainPageViewModel()
{
ConnectToRobobinCommand = new Command(async () => await OnConnectToRobobin());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment