I am trying to use Shiny.NET in my maui project for bluetooth, for that I have installed Shiny.BluetoothLE and Shiny.Hosting.Maui dependencies as mentioned on the docs page.
However I am getting null BleManager even after using dependency injection to use it.
MauiProgram.cs
using Microsoft.Extensions.Logging;
using Shiny;
using Shiny.BluetoothLE;
namespace shinyTest
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseShiny()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.Services.AddSingleton<IBleManager, BleManager>();
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
}
}
MainPage.xaml.cs
using Shiny.BluetoothLE;
namespace shinyTest
{
public partial class MainPage : ContentPage
{
int count = 0;
private IBleManager bleManager;
public MainPage(IBleManager bleManager)
{
InitializeComponent();
this.bleManager = bleManager;
}
public MainPage()
{
InitializeComponent();
}
private async void OnCounterClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new BluePage(bleManager));
}
}
}
BluePage.xaml.cs
using Shiny;
using Shiny.BluetoothLE;
using System.Collections.Generic;
using System.Reactive.Linq;
namespace shinyTest;
public partial class BluePage : ContentPage
{
private IBleManager _bleManager;
public BluePage(IBleManager bleManager)
{
InitializeComponent();
_bleManager = bleManager;
}
private async void scan_btn_Clicked(object sender, EventArgs e)
{
if(_bleManager==null)
{
await DisplayAlert("Null bleManager", "BLE is NULL", "OK");
return;
}
IEnumerable<IPeripheral> connected_devices;
try
{
connected_devices = _bleManager.GetConnectedPeripherals();
await DisplayAlert("Connected Devices", $"{connected_devices}", "Ok");
}
catch (Exception ex)
{
await DisplayAlert("Exception", $"{ex.Message}", "ok");
}
}
}
on clicking the button the alert of Null BleManager is shown. And on removing this ‘if’ check it is providing the exception of “Object reference not set to an instance of an object”.
Help appreciated!