Table of Contents

DeviceManager

DeviceManager keeps the discovered and paired device lists for you and tells subscribers when either changes. It is the bookkeeping every Bluetooth application ends up writing: add on discovery, remove on loss, deduplicate by address, refresh after pairing, and redraw when something moves.

Namespace Ahsoka.Services.Bluetooth.AppHelper. Optional, and it holds no client — it is a container plus a change-notification mechanism.

Table of Contents

Creating One

Subscribing

Feeding It

Reading the Lists

One Per Adapter

Creating One

using Ahsoka.Services.Bluetooth.AppHelper;

var deviceManager = new DeviceManager();          // unbounded
var capped = new DeviceManager(maxDevices: 20);   // cap the discovered list

maxDevices bounds the discovered list, which is useful for a fixed-height UI in a busy radio environment. 0 — the default — means no limit.

Subscribing

Implement IDeviceSubscriber and register:

public class DeviceListPanel : IDeviceSubscriber
{
    public void OnDiscoveredDevicesChanged() { /* redraw the scan list */ }
    public void OnPairedDevicesChanged()     { /* redraw the paired list */ }
    public void OnDiscoveryComplete()        { /* stop the spinner */ }
}
deviceManager.SubscribeToDeviceChanges(panel);
deviceManager.UnsubscribeFromDeviceChanges(panel);

The callbacks say that something changed, not what. Re-read the list and rebuild. Unsubscribe when your UI object goes away.

Feeding It

Like MediaRelay, DeviceManager is passive — it does not subscribe to notifications. Your application receives them and feeds them in:

Notification Call
DiscoveredDeviceAdded AddDiscoveredDevice(BluetoothDeviceDiscoveryInfo)
DiscoveredDeviceRemoved RemoveDiscoveredDevice(BluetoothDeviceDiscoveryInfo)
DiscoveryComplete NotifyDiscoveryComplete()
PairingComplete AddPairedDevice(BluetoothDevicePairedInfo)
UnpairingComplete RemovePairedDevice(BluetoothDevicePairedInfo)
switch (e.TransportId)
{
    case BluetoothMessageTypes.Ids.DiscoveredDeviceAdded
        when e.NotificationObject is BluetoothDeviceDiscoveryInfo discovered:
        deviceManager.AddDiscoveredDevice(discovered);
        break;

    case BluetoothMessageTypes.Ids.DiscoveryComplete:
        deviceManager.NotifyDiscoveryComplete();
        break;

    case BluetoothMessageTypes.Ids.PairingComplete
        when e.NotificationObject is BluetoothDevicePairedInfo paired:
        deviceManager.AddPairedDevice(paired);
        break;
}

Seed the paired list at startup from the service, since pairings survive restarts:

var paired = bluetoothClient.RequestPairedDevices(adapterIndex);
deviceManager.RefreshPairedDevices(paired.PairedDevices);

RefreshPairedDevices replaces the whole list in one operation and raises a single OnPairedDevicesChanged, which is what you want after a bulk re-read.

The add and remove methods handle deduplication and only raise a change callback when something actually changed, so feeding the same notification twice is harmless.

Reading the Lists

Member Returns
DiscoveredDevices IReadOnlyList<BluetoothDeviceDiscoveryInfo>
PairedDevices IReadOnlyList<BluetoothDevicePairedInfo>
FindDiscoveredDevice(string deviceMacAddress) The entry, or null
FindPairedDevice(string deviceMacAddress) The entry, or null

MAC address is the stable identity — names change, RSSI changes, and objects are replaced as fresher information arrives. Key your own state on the address, and use the Find methods to get back to the current object when you need to pass one to a connect or unpair call.

When you build a filtered view, classify with Class of Device and check capability with profile flags:

var headsets = deviceManager.PairedDevices
    .Where(device =>
        device.SupportedProfiles.HasFlag(BluetoothProfilesFlags.A2dpSink) &&
        IsDeviceClassOrUnknown(device, BluetoothMajorDeviceClass.DeviceClassAudioVideo));

For a discovered list, filter on Class of Device only — profile flags are not authoritative until a device is paired. See Class of Device for identity, SDP for capability.

One Per Adapter

DeviceManager does not partition by adapter. Discovery sessions and paired lists are per adapter, so use one DeviceManager per adapter and route each notification to the manager for the adapter it came from:

var managers = new Dictionary<int, DeviceManager>();

case BluetoothMessageTypes.Ids.DiscoveredDeviceAdded
    when e.NotificationObject is BluetoothDeviceDiscoveryInfo discovered:
    if (managers.TryGetValue(discovered.AdapterIndex, out var manager))
        manager.AddDiscoveredDevice(discovered);
    break;

Sharing one instance across adapters merges two independent device lists into one and produces a UI that offers devices on the wrong adapter. The Classic demo gives each adapter its own context object holding its own DeviceManager.