Skip to content
Shiny.NET
Commercial SupportGuaranteed response times on Shiny bug fixes, straight from the maintainers. Plans from US$1,500 a year.See the plans

Background Operations

Shiny BLE supports background operations through delegates. These fire when Bluetooth events occur even when your app is in the background.

The IBleDelegate interface receives adapter state and peripheral connection events in the background.

public class MyBleDelegate : BleDelegate
{
readonly ILogger<MyBleDelegate> logger;
public MyBleDelegate(ILogger<MyBleDelegate> logger)
{
this.logger = logger;
}
public override Task OnAdapterStateChanged(AccessState state)
{
this.logger.LogInformation("BLE Adapter State: {State}", state);
return Task.CompletedTask;
}
public override Task OnPeripheralStateChanged(IPeripheral peripheral)
{
this.logger.LogInformation(
"Peripheral {Name} ({Uuid}) - {Status}",
peripheral.Name,
peripheral.Uuid,
peripheral.Status
);
return Task.CompletedTask;
}
}

Register your delegate during startup:

services.AddBluetoothLE<MyBleDelegate>();

iOS system alerts for background connections

Section titled “iOS system alerts for background connections”

On iOS and Mac Catalyst, CoreBluetooth can put its own alert in front of the user — “Accessory would like to open App” — when a peripheral connects, disconnects or sends a notification while your app is not in the foreground. Apple intends these for apps that do not declare the bluetooth-central background mode and so have no other way to tell the user something happened.

Shiny asks for all three by default. If your app connects from the background on purpose — for example one kept running by background location updates, connecting when it detects something and disconnecting afterwards — the alert appears on every connect, and for a device that streams notifications, once per packet. Turn them off (5.8.1+):

services.AddBluetoothLE(new AppleBleConfiguration
{
NotifyOnConnection = false,
NotifyOnDisconnection = false,
NotifyOnNotification = false
});

The options apply to every connect the app issues. They have no effect on other platforms.

Scenario Use
Respond to events while app is backgrounded Delegate
Monitor connection state while app is in foreground WhenStatusChanged() observable
React to adapter on/off changes globally Delegate
UI-bound connection monitoring Observable