Profile Connection Lifecycle
Pairing bonds a device to the display. It does not connect anything. Before audio flows or messages can be read, a profile has to be connected — and profiles connect independently of each other.
This page covers the part that is identical for every profile. The profile pages cover what each one does once connected.
Table of Contents
Connection State Notifications
Check Capability Before Offering a Connection
Pairing Is Not Connecting
Three separate states, easy to conflate:
| State | Meaning | Survives power cycle |
|---|---|---|
| Paired (bonded) | Keys exchanged; the devices know each other | Yes |
| Connected (link) | A radio link exists | No |
| Profile connected | A specific profile is usable over that link | No |
A phone can be paired and absent. It can be present and connected for AudioSink but not for MessageAccess. Each profile is its own connection, tracked separately, on its own adapter.
That means "is the phone connected?" is not a well-formed question in this API. The answerable question is "is this device connected for this profile?"
Connecting and Disconnecting
Every profile client derives from BluetoothProfileClientBase<T> and inherits the same two methods:
audioSinkClient.RequestConnectProfile(deviceInfo);
audioSinkClient.RequestDisconnectProfile(deviceInfo);
deviceInfo is a BluetoothDevicePairedInfo — the object you got from PairingComplete or
RequestPairedDevices. Its AdapterIndex is what routes the request, so you do not pass an adapter
index separately.
Both return void. The outcome arrives as a notification.
Connection State Notifications
Connection messages are shared across all profiles, so they live in their own enum,
BluetoothProfileMessageTypes.Ids:
| Id | Direction | Payload |
|---|---|---|
ConnectProfile |
request | — |
DisconnectProfile |
request | — |
ProfileConnectionStateChanged |
notification | ProfileConnectionInfo |
ProfileConnectionInfo has exactly two properties:
| Property | Type | Meaning |
|---|---|---|
ConnectionState |
ProfileConnectionState |
StateDisconnected, StateConnecting, StateConnected, StateDisconnecting |
DeviceInfo |
BluetoothDevicePairedInfo |
Which device — and via its AdapterIndex, which adapter |
Because the id lives in a different enum from the profile's own messages, a profile callback handles
both. The transport id is typed to the profile's enum, so compare the two across enums with
EnumToInt():
void OnAudioSinkNotification(
object sender,
AhsokaClientBase<AudioSinkProfileMessageTypes.Ids>.AhsokaNotificationArgs e)
{
// Shared connection state, from a different enum
if (e.TransportId.EnumToInt() ==
BluetoothProfileMessageTypes.Ids.ProfileConnectionStateChanged.EnumToInt()
&& e.NotificationObject is ProfileConnectionInfo connectionInfo)
{
if (connectionInfo.DeviceInfo?.AdapterIndex != myAdapterIndex)
return;
switch (connectionInfo.ConnectionState)
{
case ProfileConnectionState.StateConnected:
break;
case ProfileConnectionState.StateDisconnected:
break;
}
return;
}
// This profile's own messages
switch (e.TransportId)
{
case AudioSinkProfileMessageTypes.Ids.AvrcpPlayerInfoChanged:
break;
}
}
Important
Filter by adapter index yourself. A profile service spans every adapter, so its notifications reach
every subscriber regardless of which adapter the event came from. If your application keeps per-adapter
objects — as the demo's panels do — each must ignore notifications whose
connectionInfo.DeviceInfo.AdapterIndex is not its own. Omitting that check is the classic
multi-adapter bug: adapter 0's panel reacting to adapter 1's headset.
Treat StateConnecting and StateDisconnecting as transitional — show a spinner, disable the connect
button — and only act on StateConnected and StateDisconnected. Repeated notifications for the same
state are possible; make your handling idempotent.
Check Capability Before Offering a Connection
A connect request to a device that does not implement the profile fails. Since a paired device reports its capabilities authoritatively, filter before offering the action:
// Offer AudioSink (receive audio from this device) only if it can source A2DP
bool canConnectSink = device.SupportedProfiles.HasFlag(BluetoothProfilesFlags.A2dpSource);
// Offer AudioSource (send audio to this device) only if it can sink A2DP
bool canConnectSource = device.SupportedProfiles.HasFlag(BluetoothProfilesFlags.A2dpSink);
Note the inversion again: to receive from a phone you need the phone to be an A2DP Source. The flags describe the remote device's roles, not the display's.
For a user-facing list, combine this with Class of Device so you separate phones from headsets rather than merely capable from incapable — see Class of Device for identity, SDP for capability.
SupportedProfiles is a [Flags] enum covering what the remote device advertises, which is a wider set
than the six profile services the extension implements:
Spp, HfpAg, Hfp, Opp, A2dpSink, A2dpSource, AvrcpSource, AvrcpTarget, Hid, HspAg,
Hsp, HdpSink, HdpSource, Pbap, Bgio, Ota, Map, Iap.
A flag being present means the device supports it. It does not mean the extension does — Iap is the
clearest example. See Supported & Not Supported.
Devices Connect Themselves
You are not the only party that initiates connections. A paired headset powering on will typically connect
on its own, a paired phone could reconnect when it comes into range, and the phone's user can connect from
the phone at any time. All of these produce ProfileConnectionStateChanged with StateConnected that your
application never asked for.
Handle it:
- Do not treat an unsolicited
StateConnectedas an error or ignore it — it is how many real connections begin. - Do not assume a connection you did not initiate belongs to the adapter you were last working with.
Read
DeviceInfo.AdapterIndex.
There Is No State Query
There is no "what is currently connected?" call.
Treat connection state as push-only and keep your own per-adapter view, seeded from an empty state:
- Register your profile callbacks and
Start()each client before enabling adapters. - Build state purely from
ProfileConnectionStateChanged. - Disable adapters on shutdown so the next run genuinely starts empty.
Step 3 is what makes step 2 correct. Skip it and a restart leaves the service holding connections your new process will never hear about. See Shutting Down Cleanly.