Bluetooth Pairing
The Bluetooth Pairing module lets you manage Bluetooth connections between devices. Since all Bluetooth headsets are connected to a computer using a dongle, such as the Jabra Link 380, the module is centered around the IBluetoothDongle interface.
Creating Bluetooth Devices
The BluetoothModule class is the entry to the Bluetooth Pairing domain. It lets you create objects that you can use to work with Bluetooth devices.
If a connected device is a Bluetooth dongle, you can create a corresponding IBluetoothDongle interface 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 interface represents devices connected through the dongle. They can also be created using the following 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 that exchange information letting them connect to each other. If you are familiar with Bluetooth pairing on your smartphone, the process for dongles and headsets is similar, and as follows: For two Bluetooth devices to pair, they should first be put in pairing mode. This lets them find other devices in pairing mode.
The following example shows 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);
}
});
}
Moreover, it may be useful to un-pair devices as follows:
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 are paired, they can also be connected to each other, and often connecting happens automatically. However, there may be cases where you want to control the connection. For example, the dongle is paired with multiple devices, and you would like to connect to a specific device.
The following example shows how you can use the entries in the pairing list to select which 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);
}
}
}