Interface IBluetoothBroadcaster
- Namespace
- Bluetooth.Abstractions.Broadcasting
- Assembly
- Bluetooth.Abstractions.Broadcasting.dll
Interface for managing Bluetooth broadcasting operations. Enables the device to act as a Bluetooth peripheral/server, advertising services and handling client connections.
public interface IBluetoothBroadcaster : IAsyncDisposable
- Inherited Members
Properties
Adapter
Gets the Bluetooth adapter associated with this broadcaster.
IBluetoothAdapter Adapter { get; }
Property Value
CurrentBroadcastingOptions
Gets the current broadcasting options being used by the Broadcaster.
BroadcastingOptions CurrentBroadcastingOptions { get; }
Property Value
IsRunning
Gets a value indicating whether the Bluetooth activity is actively running.
bool IsRunning { get; }
Property Value
IsStarting
Gets a value indicating whether the Bluetooth activity is starting.
bool IsStarting { get; }
Property Value
IsStopping
Gets a value indicating whether the Broadcaster is stopping.
bool IsStopping { get; }
Property Value
LoggerFactory
Gets the logger factory for creating loggers within the Bluetooth broadcaster/server implementation.
ILoggerFactory? LoggerFactory { get; }
Property Value
Methods
CreateServiceAsync(Guid, string?, bool, TimeSpan?, CancellationToken)
Adds a GATT service to be hosted by the broadcaster.
ValueTask<IBluetoothLocalService> CreateServiceAsync(Guid id, string? name = null, bool isPrimary = true, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
idGuidThe UUID of the service to add.
namestringAn optional name for the service. If not provided, a default name may be assigned based on the UUID or other heuristics.
isPrimaryboolIndicates whether the service is a primary service. Defaults to true.
timeoutTimeSpan?The timeout for this operation
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask<IBluetoothLocalService>
The added service.
GetClientDevice(Func<IBluetoothConnectedDevice, bool>)
Returns the first Bluetooth device that matches the specified filter.
IBluetoothConnectedDevice GetClientDevice(Func<IBluetoothConnectedDevice, bool> filter)
Parameters
filterFunc<IBluetoothConnectedDevice, bool>A function to filter devices. Should return true for matching devices.
Returns
- IBluetoothConnectedDevice
The matching IBluetoothConnectedDevice.
Exceptions
- ClientDeviceNotFoundException
Thrown when no device matches the specified filter.
- MultipleClientDevicesFoundException
Thrown when multiple devices match the specified filter.
GetClientDevice(string)
Returns a Bluetooth device with the specified ID.
IBluetoothConnectedDevice GetClientDevice(string id)
Parameters
idstringThe ID of the device to retrieve.
Returns
- IBluetoothConnectedDevice
The matching IBluetoothConnectedDevice.
Exceptions
- ClientDeviceNotFoundException
Thrown when no device with the specified ID is found.
- MultipleClientDevicesFoundException
Thrown when multiple devices with the specified ID are found.
GetClientDeviceOrDefault(Func<IBluetoothConnectedDevice, bool>)
Returns the first Bluetooth device that matches the specified filter.
IBluetoothConnectedDevice? GetClientDeviceOrDefault(Func<IBluetoothConnectedDevice, bool> filter)
Parameters
filterFunc<IBluetoothConnectedDevice, bool>A function to filter devices. Should return true for matching devices.
Returns
- IBluetoothConnectedDevice
The matching IBluetoothConnectedDevice, or null if none are found.
Exceptions
- MultipleClientDevicesFoundException
Thrown when multiple devices match the specified filter.
GetClientDeviceOrDefault(string)
Returns a Bluetooth device with the specified ID.
IBluetoothConnectedDevice? GetClientDeviceOrDefault(string id)
Parameters
idstringThe ID of the device to retrieve.
Returns
- IBluetoothConnectedDevice
The matching IBluetoothConnectedDevice, or null if none are found.
Exceptions
- MultipleClientDevicesFoundException
Thrown when multiple devices with the specified ID are found.
GetClientDevices(Func<IBluetoothConnectedDevice, bool>?)
Returns all Bluetooth devices that match the specified filter.
IReadOnlyList<IBluetoothConnectedDevice> GetClientDevices(Func<IBluetoothConnectedDevice, bool>? filter = null)
Parameters
filterFunc<IBluetoothConnectedDevice, bool>An optional function to filter devices. Defaults to null for all devices.
Returns
- IReadOnlyList<IBluetoothConnectedDevice>
A read-only snapshot of connected client devices at the time of the call. This collection is immutable and will not be modified if devices connect or disconnect after the call returns. To get updated results, call this method again or subscribe to ClientDeviceListChanged event.
GetService(Func<IBluetoothLocalService, bool>)
Gets a hosted GATT service that matches the specified filter.
IBluetoothLocalService GetService(Func<IBluetoothLocalService, bool> filter)
Parameters
filterFunc<IBluetoothLocalService, bool>A function to filter services. Should return true for the desired service.
Returns
- IBluetoothLocalService
The matching service.
Exceptions
- ServiceNotFoundException
Thrown if no service matches the specified filter.
- MultipleServicesFoundException
Thrown if multiple services match the specified filter.
GetService(Guid)
Gets a hosted GATT service by its UUID.
IBluetoothLocalService GetService(Guid id)
Parameters
idGuidThe UUID of the service to retrieve.
Returns
- IBluetoothLocalService
The matching service.
Exceptions
- ServiceNotFoundException
Thrown if no service matches the specified filter.
- MultipleServicesFoundException
Thrown if multiple services match the specified filter.
GetServiceOrDefault(Func<IBluetoothLocalService, bool>)
Gets a hosted GATT service that matches the specified filter.
IBluetoothLocalService? GetServiceOrDefault(Func<IBluetoothLocalService, bool> filter)
Parameters
filterFunc<IBluetoothLocalService, bool>A function to filter services. Should return true for the desired service.
Returns
- IBluetoothLocalService
The matching service, or null if not found.
Exceptions
- MultipleServicesFoundException
Thrown if multiple services match the specified filter.
GetServiceOrDefault(Guid)
Gets a hosted GATT service by its UUID.
IBluetoothLocalService? GetServiceOrDefault(Guid id)
Parameters
idGuidThe UUID of the service to retrieve.
Returns
- IBluetoothLocalService
The matching service, or null if not found.
Exceptions
- MultipleServicesFoundException
Thrown if multiple services match the specified filter.
GetServices(Func<IBluetoothLocalService, bool>?)
Gets all hosted GATT services.
IReadOnlyList<IBluetoothLocalService> GetServices(Func<IBluetoothLocalService, bool>? filter = null)
Parameters
filterFunc<IBluetoothLocalService, bool>An optional filter to apply to the services.
Returns
- IReadOnlyList<IBluetoothLocalService>
A read-only snapshot of services at the time of the call. This collection is immutable and will not be modified if services are added or removed after the call returns. To get updated results, call this method again or subscribe to ServiceListChanged event.
HasBroadcasterPermissionsAsync()
Checks if the application has the necessary broadcaster permissions.
ValueTask<bool> HasBroadcasterPermissionsAsync()
Returns
Remarks
This is a read-only check. It does not trigger any permission requests.
Platform-specific behavior:
- Android: Checks BLUETOOTH_ADVERTISE (API 31+) or location permissions (older)
- iOS/macOS: Checks Bluetooth Always + Peripheral permissions
- Windows: Checks adapter availability and peripheral role support
HasClientDevice(Func<IBluetoothConnectedDevice, bool>)
Determines if there is at least one connected client device that matches the specified filter.
bool HasClientDevice(Func<IBluetoothConnectedDevice, bool> filter)
Parameters
filterFunc<IBluetoothConnectedDevice, bool>A function to filter devices. Should return true for matching devices.
Returns
- bool
True if at least one matching device is found; otherwise, false.
HasClientDevice(string)
Determines if there is a connected client device with the specified ID.
bool HasClientDevice(string id)
Parameters
idstringThe ID of the device to check for.
Returns
- bool
True if a device with the specified ID is found; otherwise, false.
HasService(Func<IBluetoothLocalService, bool>)
Checks if a hosted GATT service that matches the specified filter exists.
bool HasService(Func<IBluetoothLocalService, bool> filter)
Parameters
filterFunc<IBluetoothLocalService, bool>A function to filter services. Should return true for the desired service.
Returns
- bool
True if a matching service exists, false otherwise.
HasService(Guid)
Checks if a hosted GATT service with the specified UUID exists.
bool HasService(Guid id)
Parameters
idGuidThe UUID of the service to check for.
Returns
- bool
True if a matching service exists, false otherwise.
RemoveAllServicesAsync(TimeSpan?, CancellationToken)
Removes all hosted services from the broadcaster.
ValueTask RemoveAllServicesAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
timeoutTimeSpan?The timeout for this operation
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous operation.
RemoveServiceAsync(IBluetoothLocalService, TimeSpan?, CancellationToken)
Removes a hosted GATT service from the broadcaster.
ValueTask RemoveServiceAsync(IBluetoothLocalService localService, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
localServiceIBluetoothLocalServiceThe service to remove.
timeoutTimeSpan?The timeout for this operation
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous operation.
RemoveServiceAsync(Guid, TimeSpan?, CancellationToken)
Removes a hosted GATT service from the broadcaster.
ValueTask RemoveServiceAsync(Guid id, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
idGuidThe UUID of the service to remove.
timeoutTimeSpan?The timeout for this operation
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous operation.
RequestBroadcasterPermissionsAsync(CancellationToken)
Requests the necessary broadcaster permissions from the user.
ValueTask RequestBroadcasterPermissionsAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token to cancel the permission request operation.
Returns
- ValueTask
Task that completes when permissions are requested.
Remarks
Platform-specific behavior:
- Android: Shows system permission dialog; can be requested multiple times
- iOS/macOS: Requests both Bluetooth Always and Peripheral permissions
- Windows: Checks adapter state and requests radio access if needed
Exceptions
- BluetoothPermissionException
Thrown when permission request fails or is denied. Check InnerException for platform-specific details.
StartBroadcastingAsync(BroadcastingOptions?, PermissionOptions?, TimeSpan?, CancellationToken)
Asynchronously starts the Bluetooth activity with an optional timeout.
ValueTask StartBroadcastingAsync(BroadcastingOptions? broadcastingOptions = null, PermissionOptions? permissionOptions = null, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
broadcastingOptionsBroadcastingOptionsThe broadcasting options to use when starting the broadcaster. If null, default options will be used.
permissionOptionsPermissionOptionsThe permission options to use when starting the broadcaster. If null, default options will be used.
timeoutTimeSpan?The timeout for this operation
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous start operation.
Remarks
Ensures that the Bluetooth activity is initialized and ready for use.
Exceptions
- InvalidOperationException
Thrown when broadcasting is already active.
- PlatformNotSupportedException
Thrown when the platform doesn't support peripheral mode.
- TimeoutException
Thrown when the operation times out.
- OperationCanceledException
Thrown when the operation is cancelled.
StartBroadcastingIfNeededAsync(BroadcastingOptions?, PermissionOptions?, TimeSpan?, CancellationToken)
Asynchronously starts the Bluetooth activity if it is not already running, with an optional timeout.
ValueTask StartBroadcastingIfNeededAsync(BroadcastingOptions? broadcastingOptions = null, PermissionOptions? permissionOptions = null, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
broadcastingOptionsBroadcastingOptionsThe broadcasting options to use when starting the broadcaster. If null, default options will be used.
permissionOptionsPermissionOptionsThe permission options to use when starting the broadcaster. If null, default options will be used.
timeoutTimeSpan?The timeout for this operation
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous start operation.
Remarks
Checks if the Bluetooth activity is already running before attempting to start it.
StopBroadcastingAsync(TimeSpan?, CancellationToken)
Asynchronously stops the Broadcaster with an optional timeout.
ValueTask StopBroadcastingAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
timeoutTimeSpan?The timeout for this operation
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous stop operation.
Remarks
Ensures that the Broadcaster and its resources are safely released.
Exceptions
- InvalidOperationException
Thrown when broadcasting is not active.
- TimeoutException
Thrown when the operation times out.
- OperationCanceledException
Thrown when the operation is cancelled.
StopBroadcastingIfNeededAsync(TimeSpan?, CancellationToken)
Asynchronously stops the Broadcaster if it is running, with an optional timeout.
ValueTask StopBroadcastingIfNeededAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
timeoutTimeSpan?The timeout for this operation
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous stop operation.
Remarks
Checks if the Broadcaster is running before attempting to stop it.
Events
ClientDeviceListChanged
Event triggered when the list of connected client devices changes.
event EventHandler<ClientDeviceListChangedEventArgs>? ClientDeviceListChanged
Event Type
ClientDevicesAdded
Event triggered when client devices connect.
event EventHandler<ClientDevicesAddedEventArgs>? ClientDevicesAdded
Event Type
ClientDevicesRemoved
Event triggered when client devices disconnect.
event EventHandler<ClientDevicesRemovedEventArgs>? ClientDevicesRemoved
Event Type
RunningStateChanged
Occurs when the running state of the Bluetooth activity changes.
event EventHandler? RunningStateChanged
Event Type
ServiceListChanged
Occurs when the service list changes.
event EventHandler<ServiceListChangedEventArgs>? ServiceListChanged
Event Type
ServicesAdded
Event triggered when services are added.
event EventHandler<ServicesAddedEventArgs>? ServicesAdded
Event Type
ServicesRemoved
Event triggered when services are removed.
event EventHandler<ServicesRemovedEventArgs>? ServicesRemoved
Event Type
Started
Occurs when the Bluetooth activity has started.
event EventHandler Started
Event Type
Starting
Occurs when the Bluetooth activity is starting.
event EventHandler Starting
Event Type
Stopped
Occurs when the Broadcaster has stopped.
event EventHandler Stopped
Event Type
Stopping
Occurs when the Broadcaster is stopping.
event EventHandler Stopping