Table of Contents

MediaRelay

MediaRelay bridges the two audio profiles so a phone and a headset work together through the display: the phone's now-playing state is pushed out to the headset, and the headset's buttons are turned into commands for the phone.

Namespace Ahsoka.Services.Bluetooth.AppHelper. It is optional and unprivileged — it calls the same public AudioSinkServiceClient and AudioSourceServiceClient you would.

Table of Contents

What It Does

It Is Passive — You Feed It

Constructing It

Feeding It

Routing Is Per Adapter

Carrying the Audio

Inspecting What It Did

What It Does Not Do

Threading

What It Does

Two directions:

Direction Trigger Result
Phone → headset PlayerInfo from AudioSink SetNowPlaying on AudioSource, so the headset displays the track
Headset → phone MediaButtonInfo from AudioSource SendMusicCommand on AudioSink, so the phone acts on the press

It also carries the audio. See Carrying the Audio.

It Is Passive — You Feed It

This is the single most important thing about the class. MediaRelay does not subscribe to notifications. It has no Start(), no Stop(), and no subscriber interface.

Your application already receives the notifications it needs. You forward them in; the relay decides what to do and makes the outbound client calls.

flowchart TB
    sink["AudioSink profile service"]
    source["AudioSource profile service"]
    app["Your application<br/>notification callbacks"]
    relay["MediaRelay"]

    sink -- "ProfileConnectionStateChanged<br/>AvrcpPlayerInfoChanged" --> app
    source -- "ProfileConnectionStateChanged<br/>AvrcpMediaButtonPressed" --> app

    app -- "HandlePhoneConnectionChanged<br/>HandlePlayerInfo" --> relay
    app -- "HandleHeadsetConnectionChanged<br/>HandleHeadsetButton" --> relay

    relay -- "SendMusicCommand" --> sink
    relay -- "SetNowPlaying" --> source

A notification you do not forward is invisible to the relay. If nothing relays, check your forwarding calls before suspecting the class.

Constructing It

Give it a client for each side:

using Ahsoka.Services.Bluetooth.AppHelper;
using Ahsoka.Services.Bluetooth.Profiles.AudioSink;
using Ahsoka.Services.Bluetooth.Profiles.AudioSource;

var relaySinkClient = new AudioSinkServiceClient();
var relaySourceClient = new AudioSourceServiceClient();

var relay = new MediaRelay(relaySinkClient, relaySourceClient);

These clients are used only for outbound calls, so they need no notification callback and no explicit Start() — the first request starts them.

A relay only makes sense when the configuration has both sides. Check before constructing:

var config = bluetoothClient.GetBluetoothConfiguration();

bool hasPhoneSide = config.AdapterConfigurations.Any(adapter =>
    adapter.SupportedProfileServiceTypes.Contains(BluetoothProfileServiceType.AudioSinkProfileService));
bool hasHeadsetSide = config.AdapterConfigurations.Any(adapter =>
    adapter.SupportedProfileServiceTypes.Contains(BluetoothProfileServiceType.AudioSourceProfileService));

if (hasPhoneSide && hasHeadsetSide)
    relay = new MediaRelay(relaySinkClient, relaySourceClient);

Create it before the adapters come up, so the first connection notifications are not missed.

Feeding It

Four methods, each fed from a notification you already handle:

Method Feed it from
HandlePhoneConnectionChanged(ProfileConnectionInfo) Every AudioSink ProfileConnectionStateChanged
HandleHeadsetConnectionChanged(ProfileConnectionInfo) Every AudioSource ProfileConnectionStateChanged
HandlePlayerInfo(PlayerInfo) Every AudioSink AvrcpPlayerInfoChanged
HandleHeadsetButton(MediaButtonInfo) Every AudioSource AvrcpMediaButtonPressed

In the AudioSink callback:

if (e.TransportId.EnumToInt() ==
        BluetoothProfileMessageTypes.Ids.ProfileConnectionStateChanged.EnumToInt()
    && e.NotificationObject is ProfileConnectionInfo connectionInfo)
{
    relay?.HandlePhoneConnectionChanged(connectionInfo);
}

if (e.TransportId == AudioSinkProfileMessageTypes.Ids.AvrcpPlayerInfoChanged
    && e.NotificationObject is PlayerInfo playerInfo)
{
    relay?.HandlePlayerInfo(playerInfo);
}

In the AudioSource callback:

if (e.TransportId.EnumToInt() ==
        BluetoothProfileMessageTypes.Ids.ProfileConnectionStateChanged.EnumToInt()
    && e.NotificationObject is ProfileConnectionInfo connectionInfo)
{
    relay?.HandleHeadsetConnectionChanged(connectionInfo);
}

if (e.TransportId == AudioSourceProfileMessageTypes.Ids.AvrcpMediaButtonPressed
    && e.NotificationObject is MediaButtonInfo buttonInfo)
{
    relay?.HandleHeadsetButton(buttonInfo);
}

Feed it everything of those four kinds — including adapters you are not currently displaying. Repeated notifications are harmless, so you do not need to deduplicate.

Routing Is Per Adapter

A phone and a headset are each bonded to the display, never to each other. The relay's association between them is a route — a purely local pairing-up of two adapters, with no Bluetooth relationship involved.

By default a headset drives the phone on its own adapter. Every adapter relays independently and simultaneously, with no configuration: adapter 0's phone and headset are routed together, adapter 1's do the same, and they do not interfere.

To point a headset at a phone on a different adapter:

// headset on adapter 1 drives the phone on adapter 0
relay.RouteHeadsetToPhone(headsetAdapterIndex: 1, phoneAdapterIndex: 0);

// restore the default by pointing it back at its own adapter
relay.RouteHeadsetToPhone(headsetAdapterIndex: 1, phoneAdapterIndex: 1);

Current state:

Member Returns
ActiveRoutes Every route with a connected device at both ends, by ascending headset adapter
ConnectedPhones Phones connected on the AudioSink side, by ascending adapter
ConnectedHeadsets Headsets connected on the AudioSource side, by ascending adapter
GetHeadsetRoute(headsetAdapterIndex) That headset's route, or null if it has no phone to drive
GetPhoneRoute(phoneAdapterIndex) That phone's route, or null if no headset points at it

MediaRelayRoute carries PhoneAdapterIndex and HeadsetAdapterIndex, plus PhoneAddress and HeadsetAddress for finding the devices themselves. Equality is the adapter pair, so a route stays equal to itself when a different phone connects on the same adapter. It prints as phone hci0 -> headset hci1.

The Classic demo's Headset panel showing an active route printed as phone hci0 to headset hci0, with the now-playing state that was pushed to the headset

A phone and a headset pair one to one. Point a second headset at a phone that already has one and the newcomer takes it — an explicit RouteHeadsetToPhone beats a headset that only landed there by the same-adapter default, and the lowest adapter settles a tie between two explicit routes. The headset that loses is left with no route, so its buttons stop reaching that phone.

Carrying the Audio

Every playing phone's audio goes somewhere, and its route decides where:

The phone Its audio plays
Routed to a headset In that headset, and nowhere else
With no headset routed to it In the cabin

Nothing to call. Audio follows the same routes the buttons and now-playing state do, so a headset always hears the phone it is controlling, and a phone is never in two places at once. Two adapters carry different audio at the same time: adapter 0's phone can be in its headset while adapter 1's plays in the cabin.

This works on the display only. Off Linux there is no audio server and the relay carries control alone.

The cabin is the platform's own default output. Connecting a headset moves that default to the headset, so what the relay uses is the most recent default that was not one — for as long as the audio server still has it. A phone with no headset routed to it is therefore never sent to someone else's, and a headset connecting on one adapter does not move or silence a phone playing in the cabin from another.

Member Returns
IsAudioPending Whether a phone's audio is still waiting on the audio server
StopRoutingAudio() Stops carrying audio for every phone

Audio follows playback. A paused phone has no audio to carry, and it picks back up on its own when play resumes — you do not need to do anything. IsAudioPending is true only for the moment a playing phone spends waiting on its headset, and it clears on its own. A paused phone is not pending.

Call StopRoutingAudio() before your application exits, so the audio server is not left carrying phones nothing owns:

relay?.StopRoutingAudio();
Note

The relay does this itself for now. Audio routing belongs to Ahsoka.Extensions.AudioManager, and this moves behind its API once that extension exposes routing.

Inspecting What It Did

The relay records its last action per adapter, which is what the demo polls from its draw loop rather than subscribing to anything:

RelayedButton lastButton = relay.GetLastRelayedButton(phoneAdapterIndex);
if (lastButton != null && !lastButton.Relayed)
{
    // the relay was given this button but declined or the phone refused it
}

NowPlayingInfo lastPushed = relay.GetLastPushedNowPlaying(headsetAdapterIndex);

RelayedButton has Button and Relayed. Relayed being false means the relay was handed the press but it did not reach the phone — either the relay declined it (volume buttons are declined by design) or there was no route, or the phone did not accept it.

What It Does Not Do

It does not relay headset volume to the phone. Volume buttons are declined; RelayedButton.Relayed is false for them.

It does not choose an output device. Audio goes to the routed headset; picking between a headset and the display's speakers is not an API call here.

It does not connect anything. Connecting profiles stays your application's decision.

It does not query connection state on startup. It learns about connections only from what you forward, so construct it before adapters come up. See There Is No State Query.

Threading

Feed the relay from one thread — normally the notification thread your application dispatches on. It guards its own state, but it makes its outbound client calls on the calling thread and does not order concurrent callers.

If you register your callbacks through Dispatcher.Default as shown in Getting Started, this is already true and there is nothing extra to do.