Table of Contents

BLE Overview & Stack Lifecycle

BLEService (port 6108) puts the display on the air as a Bluetooth Low Energy peripheral: it advertises, serves a GATT database, and handles pairing when a characteristic requires encryption.

BLE is entirely independent of the Classic side. Different service, different port, different client, and no adapter-index concept — BLEServiceClient has no relationship to BluetoothServiceClient.

Table of Contents

Peripheral Mode Only

Client

Starting the Stack

Registering Services

Pairing

Stopping

Desktop Stack Behavior

Peripheral Mode Only

Important

The display is always the peripheral (GATT server). A phone or tool connects to it and reads and writes its characteristics.

The central role is not implemented. There is no scanning for BLE devices, no connecting outward to a BLE peripheral, and no GATT client. If your requirement is "the display reads a BLE sensor", this extension does not do that.

What is supported:

  • Advertising, including manufacturer-specific data
  • A GATT server with services, characteristics, and descriptors
  • Characteristic read, write, and notify
  • Encrypted characteristics, with a pairing confirmation flow through your UI

Client

using Ahsoka.Core;
using Ahsoka.Services.BLE;

var bleClient = new BLEServiceClient();

Dispatcher.Default.RegisterCallback<AhsokaClientBase<BLEMessageTypes.Ids>.AhsokaNotificationArgs>(
    OnBleNotification,
    bleClient);

bleClient.Start();
Method Purpose
StartStack(string localName) Bring the BLE stack up under the given advertised name
StopStack() Bring it down
IsStackRunning() Returns whether it is up
StartServiceModels(params BLEServiceViewModelBase[]) Register attribute-defined services and begin advertising
StopServiceModels(params BLEServiceViewModelBase[]) Unregister them
ConfirmPairingRequests(bool accepted) Answer a pairing request
CreateService, RemoveService Register a service from raw properties
CreateAdvertisement, RemoveAdvertisement Manage an advertisement directly
SetCharacteristicValue, QueueCharacteristicValue Set a value directly
SendCharacteristicNotification Notify subscribers directly

Most applications only need StartStack, StartServiceModels, and ConfirmPairingRequests — the declarative layer covers the rest. See Defining GATT Services.

Starting the Stack

StartStack takes the local name devices will see:

bleClient.StartStack("OpenViewDisplay");

It is asynchronous, and completion arrives as StackStarted carrying a BLEConnectionNotification. Check its State — this notification reports failure too:

case BLEMessageTypes.Ids.StackStarted
    when e.NotificationObject is BLEConnectionNotification notification:
    if (notification.State == BLEConnectionState.ClientReady)
        RegisterServices();
    else
        HandleStartupFailure();
    break;

Register your services only after ClientReady. Registering earlier fails, because there is no stack to register against.

On a display, wait for the wireless hardware to report ready before starting the stack. The BLE demo starts it from the device service's DeviceEnabled notification, once the device type is a wireless combo module:

if (e.TransportId == DeviceMessageTypes.Ids.DeviceEnabled
    && e.NotificationObject is DeviceInfo info)
{
    if (info.DeviceType == DeviceTypes.OnboardWirelessComboModule ||
        info.DeviceType == DeviceTypes.UsbWirelessComboModule)
    {
        bleClient.StartStack("OpenViewDisplay");
    }
}

Registering Services

Once the stack is ready, hand over your service objects. This registers the GATT database and starts advertising in one call:

bleClient.StartServiceModels(serviceExample, serviceExample2);

Both services and advertisements come from the attributes on those classes, which is the whole point of the declarative layer. See Defining GATT Services.

Note that both BluetoothService and BLEService ultimately drive the same radio. Starting the BLE stack and enabling a Classic adapter both bring up Ahsoka.Bluetooth.service; the two coexist.

Pairing

BLE pairing happens for a specific reason: a characteristic marked with an encrypted flag was accessed. If none of your characteristics require encryption, you will never see a pairing request.

When one is required, you get PairingRequested with a passkey to show, and you must answer:

case BLEMessageTypes.Ids.PairingRequested
    when e.NotificationObject is BLEPairingRequest pairingRequest:
    ShowPairingPrompt(pairingRequest.Passkey);
    break;

case BLEMessageTypes.Ids.PairingCanceled:
    HidePairingPrompt();
    break;
bleClient.ConfirmPairingRequests(true);   // accept
bleClient.ConfirmPairingRequests(false);  // reject

BLEPairingRequest carries only Passkey and DisplayOnly, and BLEPairingConfirmation only Confirmed — there is no device identity to match on, because the peripheral handles one pairing at a time. Hide your prompt on PairingCanceled, which fires when the central gives up or the request otherwise lapses.

Some centrals ask the user to type the passkey on the central instead. That request has DisplayOnly set: show the passkey with nothing to accept or reject, and do not call ConfirmPairingRequests. Hide it on PairingComplete, or on PairingCanceled if the pairing fails. If the phone was already bonded to the display, PairingComplete arrives about 35 seconds after the passkey is shown. Match the display-only case first:

case BLEMessageTypes.Ids.PairingRequested
    when e.NotificationObject is BLEPairingRequest { DisplayOnly: true } passkeyRequest:
    ShowPasskey(passkeyRequest.Passkey);
    break;

case BLEMessageTypes.Ids.PairingComplete:
    HidePasskey();
    break;

This is a different flow from Classic pairing. There is no discovery, no pairable mode, no timeout to configure, and no BluetoothDevicePairedInfo. Compare Adapters, Discovery & Pairing.

There is also no BLE call to list or remove bonds. Forgetting the display on the phone leaves the bond on the display. To remove it, run this on the display, using the address from the first command:

bluetoothctl devices Paired
bluetoothctl remove <address>

Stopping

bleClient.StopServiceModels(serviceExample, serviceExample2);
bleClient.StopStack();

StackStopped confirms the stack is down. Stop service models before the stack if you intend to restart with a different set.

Desktop Stack Behavior

The BLE desktop implementation is a stub only. It lets an application that uses BLEServiceClient start, run, and shut down on Windows without throwing, which is enough to develop UI and application flow.

It does not create a real GATT server and does not advertise. A phone will not find the display, and no central can connect. Unlike the Classic desktop stubs — which simulate devices and media playback faithfully enough to be genuinely useful — BLE work needs real hardware to verify.

Start the stack directly on Windows. The desktop device service never reports a wireless module, so an application that waits for DeviceEnabled never starts it there. The demo checks OperatingSystem.IsWindows() and calls StartStack at startup.

The stub does simulate pairing, so a pairing UI can be exercised on Windows. Once the stack starts it sends a confirmation request; rejecting it produces PairingCanceled and a retry. Accepting it is followed by a display-only request that ends in PairingCanceled, then one that ends in PairingComplete.

Plan accordingly: build structure on the desktop, verify behavior on a display.