Table of Contents

Bluetooth Pairing

The Bluetooth Pairing module allows you to manage Bluetooth connections between devices. Since all Bluetooth headsets are connected to the computer using a dongle, such as the Jabra Link 380, the module is centered around the IBluetoothDongle.

Creating Bluetooth Devices

The BluetoothModule is the entry to the Bluetooth Pairing domain. It allows you to create objects you can use to work with Bluetooth devices.

If a connected device is a Bluetooth dongle, you can create a corresponding IBluetoothDongle using the factory:

var factory = new BluetoothModule(api);
if (await factory.CreateBluetoothDongle(device) is IBluetoothDongle dongle)
{
    // The IDevice is a dongle...
}

In a similar way the IBluetoothChildDevice represents devices connected through the dongle. They can also be created using the factory:

var factory = new BluetoothModule(api);
if (
    await factory.CreateBluetoothChildDevice(device)
    is IBluetoothChildDevice childDevice
)
{
    // The IDevice is a bluetooth device connected through a dongle...
}

Bluetooth Operations

There are two primary operations in Bluetooth connectivity: Pairing and Connecting.

Pairing

Pairing is a handshake between two devices. It is a one time operation between two Bluetooth devices. They exchange information that will allow them to later connect with each other. You are probably familiar with the Bluetooth pairing from your smartphone. The situation for dongles and headsets are not different from this: For two devices to pair, they should first be put in pairing mode. This will allow them to find other devices in pairing mode.

The following example show how to start scanning for new Bluetooth devices, and pair with the first device it finds with a given name:

public void PairWithDevice(IBluetoothDongle dongle, string deviceNameToPairWith)
{
    dongle.ScanForDevicesInPairingMode(TimeSpan.FromSeconds(30))
        .Subscribe(async entry =>
        {
            if (entry.BluetoothName == deviceNameToPairWith)
            {
                await dongle.PairAndConnectTo(entry.BluetoothAddress);
            }
        });
}

It may sometimes be useful to un-pair devices. That can be done in a similar fashion:

public async Task UnpairDevice(IBluetoothDongle dongle, string deviceNameToUnpair)
{
    var pairingList = await dongle.GetPairingList();
    foreach (var entry in pairingList)
    {
        if (entry.BluetoothName == deviceNameToUnpair)
        {
            await dongle.Unpair(entry);
        }
    }
}

Connecting and Disconnecting

When two devices have been paired, they can be connected with each other. Often connecting happens automatically, but there may be cases, where you want to control it. It could for example be that the dongle is paired with multiple devices, and you want to switch the connected device.

The following example shows how you can use the entries in the pairing list to select what device to connect to:

public async Task ConnectToDevice(IBluetoothDongle dongle, string deviceNameToConnectWith)
{
    var pairingList = await dongle.GetPairingList();
    foreach (var entry in pairingList)
    {
        if (entry.BluetoothName == deviceNameToConnectWith)
        {
            await dongle.ConnectTo(entry);
        }
    }
}