Hello,
There are a few possible reasons why this happens:
- You're referencing the portable/shared version of
Plugin.BLE
. - The platform-specific implementations (Android, iOS, etc.) are not being loaded properly.
- .NET MAUI uses updated dependency injection and platform-specific loading mechanisms compared to Xamarin.Forms.
Recommended Solution
For the smoothest migration experience, switch to the official Plugin.BLE NuGet package. The NuGet package handles all platform-specific loading automatically.
<PackageReference Include="Plugin.BLE" Version="3.1.0" />
There is also a beta version:
<PackageReference Include="Plugin.BLE" Version="3.2.0-beta.1" />
Alternative: Using Source Code Directly
If you must use the source code version, follow these updated steps for .NET 9:
- Project References: Ensure you reference the platform-specific Plugin.BLE projects in your MAUI platform folders (
Platforms/Android
,Platforms/iOS
, etc.). - Update Target Frameworks: Your Plugin.BLE projects should target:
-
net9.0-android
for Android -
net9.0-ios
for iOS -
net9.0
for shared code
-
- Dependency Injection in
MauiProgram.cs
:public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); }); // Register BLE services builder.Services.AddSingleton<Plugin.BLE.Abstractions.Contracts.IBluetoothLE>( sp => Plugin.BLE.CrossBluetoothLE.Current); return builder.Build(); } }
- Platform-Specific Permissions:
- Android (
Platforms/Android/AndroidManifest.xml
):<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- iOS (
Platforms/iOS/Info.plist
):<key>NSBluetoothAlwaysUsageDescription</key> <string>This app uses Bluetooth to connect to BLE devices.</string> <key>NSBluetoothPeripheralUsageDescription</key> <string>This app uses Bluetooth to connect to BLE devices.</string>
- Android (
- Runtime Permissions: For Android 12+ (API 31+), ensure you handle runtime permission requests in your application code.
- Conditional Compilation: Use conditional compilation to include platform-specific logic where necessary:
#if ANDROID // Android-specific BLE initialization #elif IOS // iOS-specific BLE initialization #endif
- Assembly Loading: Ensure the platform-specific assemblies are included in your build output.
I hope this helps you resolve the issue.
References: