Table of Contents

Core Concepts

Five ideas explain most of how this extension behaves. Read this once and the profile pages will make sense without re-explaining themselves.

Table of Contents

Clients and Services

Everything Interesting Arrives as a Notification

The Adapter Index Is the Key to Everything

Profile Services Are Per-Adapter Plugins

Two Stacks: BlueZ and Desktop

Clients and Services

The extension exposes functionality as client/service pairs. Your application only ever holds clients; the services run in their own process, started automatically.

There are two top-level services and a client per profile:

Client Talks to Covers
BluetoothServiceClient BluetoothService (5104) Adapters, discovery, pairing, paired devices
AudioSinkServiceClient AudioSink profile service Phone-side audio and media control
AudioSourceServiceClient AudioSource profile service Headset-side audio and media control
MessageAccessServiceClient MessageAccess profile service SMS/MMS
BLEServiceClient BLEService (6108) BLE peripheral mode

Profile clients are separate objects with their own lifetimes. You construct, register callbacks on, and Start() each one you use. A profile client does not give you adapter control — that always comes from BluetoothServiceClient.

Start() is optional: a client starts itself on its first call. Call it explicitly anyway, right after registering your notification callback — notifications only flow once the client is started, so starting it yourself avoids missing any.

Everything Interesting Arrives as a Notification

Requests are how you ask for something. Notifications are how you find out what happened. Almost nothing you care about is the return value of a call:

  • EnableAdapter returns void. The adapter coming up is AdapterEnabled.
  • SetAdapterDiscoverable returns void. The change taking effect is AdapterDiscoverableChanged.
  • StartDiscovery returns a BluetoothResult meaning "the request was accepted", not "here are the devices". Devices arrive as DiscoveredDeviceAdded, one at a time, until DiscoveryComplete.
  • RequestToPair returns acceptance. The outcome is PairingComplete or PairingCanceled, possibly after a PairingRequested round trip through your UI.

So the shape of a correct application is: register the callback, issue requests, and drive all state and UI from notifications. Do not poll, and do not treat a returned BluetoothResult as completion.

All notifications for one client come through a single callback, keyed by a strongly typed TransportId, with the payload in NotificationObject:

static void OnBluetoothNotification(
    object sender,
    AhsokaClientBase<BluetoothMessageTypes.Ids>.AhsokaNotificationArgs e)
{
    switch (e.TransportId)
    {
        case BluetoothMessageTypes.Ids.PairingComplete
            when e.NotificationObject is BluetoothDevicePairedInfo paired:
            break;
    }
}

Each service and profile has its own message-id enum — BluetoothMessageTypes.Ids, AudioSinkProfileMessageTypes.Ids, AudioSourceProfileMessageTypes.Ids, MessageAccessProfileMessageTypes.Ids, BLEMessageTypes.Ids, and BluetoothProfileMessageTypes.Ids for the connect/disconnect messages shared by every profile.

Important

Notifications are not replayed when you subscribe. A client that starts after a device is already connected never learns about that connection — there is no "query current state" call to fall back on. The supported way to avoid stale state is to disable your adapters during application shutdown, so connections are severed cleanly and there is nothing to reconcile on restart.

The Adapter Index Is the Key to Everything

A display can have more than one Bluetooth adapter, and the extension treats them as genuinely independent. Nearly every request carries an AdapterIndex, and nearly every notification payload reports one.

Concept Scope
Adapter power, discoverable, pairable Per adapter
Discovery session Per adapter
Paired device list Per adapter
Profile service instances Per adapter
Connected devices Per adapter

Two adapters can be discovering, pairing, and streaming at the same time without interfering. The practical consequence: always route notifications back to the adapter they came from. A design that stores "the connected phone" in a single field breaks silently the moment a second adapter is configured. Key your state by adapter index from the start, even if you ship with one adapter.

The index is a configuration concept, not a hardware one — it maps to a deviceInterfacePath such as /org/bluez/hci0. See Configuration Reference.

Profile Services Are Per-Adapter Plugins

Profile services are not always-on features. Each adapter declares which ones it wants in SupportedProfileServiceTypes, and the service instantiates exactly those, for that adapter, when the adapter is enabled.

flowchart TB
    svc["BluetoothService"]
    m0["Module — adapter 0"]
    m1["Module — adapter 1"]
    g0["GAP profile<br/>discovery · pairing · connection"]
    g1["GAP profile<br/>discovery · pairing · connection"]
    p0["AudioSink · AudioSource · MAP"]
    p1["AudioSink only"]

    svc --> m0
    svc --> m1
    m0 --> g0
    m0 --> p0
    m1 --> g1
    m1 --> p1

Three consequences:

  1. A profile client is useless if no adapter is configured for that profile. Constructing an AudioSourceServiceClient when no adapter declares AudioSourceProfileService gets you a client that never reports anything.
  2. Connections are per profile, not per device. A phone can be connected for AudioSink and not for MessageAccess. See Profile Connection Lifecycle.
  3. Adapters can have different capabilities. One adapter can serve phones and another serve headsets, which is exactly the arrangement MediaRelay exists to bridge.

Two Stacks: BlueZ and Desktop

Every platform-facing operation has two implementations, selected by the Stack value in configuration:

Stack Where What it is
StackBluez Linux on a display The real thing — BlueZ over D-Bus, obexd for OBEX profiles, oFono for HFP
StackDesktop Windows during development Stubs that simulate adapters, devices, pairing, connections, and media playback

The stubs are deliberately faithful at the message level: the same notifications fire, in the same order, with the same payload types. That makes them good enough to build and debug an entire application against.

They are not faithful at the hardware level, and the gap is where most surprises live — real devices refuse connections, report capabilities inconsistently before pairing, deliver AVRCP buttons through an unexpected path, and behave differently between iOS and Android. Everything in Troubleshooting is in that gap.

Read the stack at runtime rather than compiling against an assumption:

var config = bluetoothClient.GetBluetoothConfiguration();
bool isDesktop = config.Stack == SupportedBluetoothStack.StackDesktop;

Optional Helpers

Two classes in Ahsoka.Services.Bluetooth.AppHelper handle bookkeeping most applications end up writing anyway. Both are optional and neither is privileged — they use the same public clients you do.

  • DeviceManager — maintains discovered and paired device lists and tells subscribers when they change.
  • MediaRelay — bridges a phone to a headset.

Both are passive: your application receives notifications and feeds them in. They never subscribe to notifications themselves. That keeps the data flow visible in your code and avoids surprising threading behavior.