Skip to content

[FEATURE] Add a net10.0-macos target to the Apple platform for headless macOS apps #115

Open

Description

@framinosona

Summary

Let the Apple platform project build for net10.0-macos, so a headless macOS app (a console tool, a test peripheral or central) can use the Core and Apple projects without MAUI. Bluetooth.Maui (the facade) stays MAUI-only.

Why

  • The library cannot be driven on a Mac without a MAUI app today: the Apple project targets net10.0-ios and net10.0-maccatalyst only, and a Mac Catalyst app is not a console tool.
  • A headless Mac makes scripted BLE checks possible without a phone UI, for example a console client that scans, explores and reads, or a test peripheral built on this library's own broadcaster. A peripheral that lists one service UUID twice (see [BUG] [Android] A peripheral that exposes the same service UUID more than once makes every read, write and notification on it time out #107) was reproduced with a throwaway CoreBluetooth script that this library's broadcaster can replace once it runs on macOS; listing the same UUID twice is a separate decision, because CreateServiceAsync rejects it today.
  • CoreBluetooth is shared between iOS and macOS, so most of the project is already neutral.

Spike result (verified by building, not run)

Added net10.0-macos to Bluetooth.Maui.Platforms.Apple (macOS workload 26.5) on a scratch branch:

Step Errors
Add the target 283, nearly all CS0246 ("type not found"): GlobalUsings.cs imports CoreBluetooth, CoreFoundation and Foundation only under #if IOS || MACCATALYST
Add MACOS to that guard 3
Guard the iOS-only members behind #if !MACOS (below) 4 more, hidden by the first three
Guard those too 0 errors, 0 warnings

iOS-only members that need a guard on macOS:

Member Where
CBCentralManagerDelegate.ConnectionEventDidOccur, DidUpdateAncsAuthorization CbCentralManagerWrapper
PeripheralConnectionOptions.NotifyOnConnection, NotifyOnNotification AppleBluetoothRemoteDevice.NativeConnectAsync
CBCentralInitOptions.RestoreIdentifier CbCentralManagerWrapper, ServiceCollectionExtensions
CBPeripheral.RSSI (obsolete on macOS, CA1422) AppleBluetoothRemoteDevice.RssiUpdated
BasePlatformPermission.RequiredInfoPlistKeys BaseApplePermissionHandler

Total: 6 files, 16 insertions (diff below).

What the compile does not prove

  • On net10.0-macos NuGet resolves Microsoft.Maui.Essentials and Plugin.ExceptionListeners.Maui to their plain net10.0 assets, which are reference-only for MAUI platform APIs. Code that calls them compiles but is expected to fail at runtime on macOS (not run): MainThread (4 call sites in MainThreadDispatcher), Permissions.CheckStatusAsync / RequestAsync (6 call sites in AppleBluetoothScanner and AppleBluetoothBroadcaster) and the BasePlatformPermission base class.
  • Nothing was run on a Mac: no scan, connection or advertisement through this target.
  • A console process takes the Bluetooth permission from the host terminal app; not verified.

Proposed scope

  1. Add net10.0-macos with the guards from the spike.
  2. On the macOS target, replace MAUI MainThread with the main dispatch queue and MAUI Permissions with CBManager.Authorization, and do not reference Plugin.ExceptionListeners.Maui (its other assets are MAUI and native hooks that a headless app does not use). Doing this for all Apple targets would remove the MAUI dependency from the platform project, but it changes shipping platforms, so it is a separate decision.
  3. A DI registration usable from a non-MAUI host. CoreBluetooth delivers callbacks on the main queue by default, so a console host has to run the main run loop.
  4. CI: build the new target on the macOS runner (workload macos).
  5. A console sample (scan, explore, read) as the first consumer.

Out of scope, not needed

  • The Bluetooth.Maui facade stays MAUI-only: MAUI has no macOS target.
  • The low-level plugins need no macOS target: Plugin.ByteArrays, Plugin.BaseTypeExtensions and Plugin.ExceptionListeners ship plain net9.0, which net10.0-macos consumes unchanged (restored and built in the spike).

Acceptance

  • The Apple project builds for net10.0-macos with 0 warnings on the macOS runner, and the other targets are unchanged.
  • A console sample scans, connects, explores and reads on a Mac (hardware run).
  • Bluetooth.Packages.slnx still builds everywhere it does today.
Spike diff (compile-only, 6 files)
diff --git a/Bluetooth.Maui.Platforms.Apple/Bluetooth.Maui.Platforms.Apple.csproj b/Bluetooth.Maui.Platforms.Apple/Bluetooth.Maui.Platforms.Apple.csproj
index 74a4998..3420dcc 100644
--- a/Bluetooth.Maui.Platforms.Apple/Bluetooth.Maui.Platforms.Apple.csproj
+++ b/Bluetooth.Maui.Platforms.Apple/Bluetooth.Maui.Platforms.Apple.csproj
@@ -29,6 +29,7 @@
     <PropertyGroup>
         <TargetFrameworks>$(TargetFrameworks);net10.0-maccatalyst</TargetFrameworks>
         <TargetFrameworks>$(TargetFrameworks);net10.0-ios</TargetFrameworks>
+        <TargetFrameworks>$(TargetFrameworks);net10.0-macos</TargetFrameworks>
     </PropertyGroup>
 
     <!-- ==================== PACKAGING ==================== -->
diff --git a/Bluetooth.Maui.Platforms.Apple/GlobalUsings.cs b/Bluetooth.Maui.Platforms.Apple/GlobalUsings.cs
index 3d02d49..e0f95d3 100644
--- a/Bluetooth.Maui.Platforms.Apple/GlobalUsings.cs
+++ b/Bluetooth.Maui.Platforms.Apple/GlobalUsings.cs
@@ -43,7 +43,7 @@
 global using Plugin.ByteArrays;
 global using Plugin.ExceptionListeners;
 
-#if IOS || MACCATALYST
+#if IOS || MACCATALYST || MACOS
 global using CoreBluetooth;
 
 global using CoreFoundation;
diff --git a/Bluetooth.Maui.Platforms.Apple/Permissions/BaseApplePermissionHandler.cs b/Bluetooth.Maui.Platforms.Apple/Permissions/BaseApplePermissionHandler.cs
index c1c3488..895475f 100644
--- a/Bluetooth.Maui.Platforms.Apple/Permissions/BaseApplePermissionHandler.cs
+++ b/Bluetooth.Maui.Platforms.Apple/Permissions/BaseApplePermissionHandler.cs
@@ -16,6 +16,7 @@ protected BaseApplePermissionHandler(string infoPlistKey)
         _infoPlistKey = infoPlistKey;
     }
 
+#if !MACOS
     /// <summary>
     ///     Gets the required Info.plist keys for this permission.
     /// </summary>
@@ -23,6 +24,7 @@ protected BaseApplePermissionHandler(string infoPlistKey)
     [
         _infoPlistKey
     ];
+#endif
 
     /// <summary>
     ///     Requests the permission if needed asynchronously.
diff --git a/Bluetooth.Maui.Platforms.Apple/Scanning/AppleBluetoothRemoteDevice.cs b/Bluetooth.Maui.Platforms.Apple/Scanning/AppleBluetoothRemoteDevice.cs
index 9a245f5..31a6b1c 100644
--- a/Bluetooth.Maui.Platforms.Apple/Scanning/AppleBluetoothRemoteDevice.cs
+++ b/Bluetooth.Maui.Platforms.Apple/Scanning/AppleBluetoothRemoteDevice.cs
@@ -241,9 +241,13 @@ protected override ValueTask NativeConnectAsync(ConnectionOptions connectionOpti
         var appleOptions = new PeripheralConnectionOptions
         {
             // Read Apple-specific sub-options (or use defaults if not provided)
+#if !MACOS
             NotifyOnConnection = connectionOptions.Apple?.NotifyOnConnection ?? true,
+#endif
             NotifyOnDisconnection = connectionOptions.Apple?.NotifyOnDisconnection ?? true,
+#if !MACOS
             NotifyOnNotification = connectionOptions.Apple?.NotifyOnNotification ?? true
+#endif
         };
         scanner.CbCentralManagerWrapper.CbCentralManager.ConnectPeripheral(CbPeripheralWrapper.CbPeripheral, appleOptions);
         return ValueTask.CompletedTask;
@@ -373,11 +377,13 @@ public void RssiRead(NSError? error, NSNumber rssi)
     /// <inheritdoc />
     public void RssiUpdated(NSError? error)
     {
+#if !MACOS
         // CbPeripheralWrapper.CbPeripheral.RSSI is Obsolete in iOS 8
         if (!OperatingSystem.IsIOSVersionAtLeast(8) && CbPeripheralWrapper.CbPeripheral.RSSI != null)
         {
             RssiRead(error, CbPeripheralWrapper.CbPeripheral.RSSI);
         }
+#endif
     }
 
     #endregion
diff --git a/Bluetooth.Maui.Platforms.Apple/Scanning/NativeObjects/CbCentralManagerWrapper.cs b/Bluetooth.Maui.Platforms.Apple/Scanning/NativeObjects/CbCentralManagerWrapper.cs
index 3c7a8ed..5c06ec3 100644
--- a/Bluetooth.Maui.Platforms.Apple/Scanning/NativeObjects/CbCentralManagerWrapper.cs
+++ b/Bluetooth.Maui.Platforms.Apple/Scanning/NativeObjects/CbCentralManagerWrapper.cs
@@ -52,11 +52,13 @@ public CBCentralManager CbCentralManager
             {
                 lock (_lock)
                 {
+#if !MACOS
                     // Validate Info.plist configuration if state restoration is enabled
                     if (!string.IsNullOrEmpty(_options.RestoreIdentifier))
                     {
                         PlistExtensions.EnsureHasBackgroundMode("bluetooth-central");
                     }
+#endif
 
                     _cbCentralManager = new CBCentralManager(this, _dispatchQueueProvider.GetCbCentralManagerDispatchQueue(), _options)
                     {
@@ -266,6 +268,7 @@ public override void ConnectedPeripheral(CBCentralManager central, CBPeripheral
         }
     }
 
+#if !MACOS
     /// <inheritdoc cref="CBCentralManagerDelegate.ConnectionEventDidOccur" />
     public override void ConnectionEventDidOccur(CBCentralManager central, CBConnectionEvent connectionEvent, CBPeripheral peripheral)
     {
@@ -303,6 +306,7 @@ public override void DidUpdateAncsAuthorization(CBCentralManager central, CBPeri
             BluetoothUnhandledExceptionListener.OnBluetoothUnhandledException(this, e);
         }
     }
+#endif
 
     #endregion
 
diff --git a/Bluetooth.Maui.Platforms.Apple/Scanning/ServiceCollectionExtensions.cs b/Bluetooth.Maui.Platforms.Apple/Scanning/ServiceCollectionExtensions.cs
index cd843e3..e74abeb 100644
--- a/Bluetooth.Maui.Platforms.Apple/Scanning/ServiceCollectionExtensions.cs
+++ b/Bluetooth.Maui.Platforms.Apple/Scanning/ServiceCollectionExtensions.cs
@@ -27,7 +27,9 @@ public static void AddBluetoothMauiAppleScanningServices(this IServiceCollection
         // Configure CBCentralManager options
         services.Configure<CBCentralInitOptions>(options => {
             options.ShowPowerAlert = true;
+#if !MACOS
             options.RestoreIdentifier = "com.bluetooth.maui.centralmanager.restore";
+#endif
         });
 
         // Note: CbCentralManagerWrapper is NOT registered in DI - it's created by AppleBluetoothScanner

Activity

  1. added
    enhancement
    on Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions