A quick and dirty library for BlueZ's D-Bus APIs. Focus is on Bluetooth Low Energy APIs.
Uses Tmds.DBus to access D-Bus. Tmds.DBus.Tool was used to generate the D-Bus object interfaces.
D-Bus is the preferred interface for Bluetooth in userspace. The Doing Bluetooth Low Energy on Linux presentation says "Use D-Bus API (documentation in doc/) whenever possible".
- Linux
- A recent release of BlueZ. This package was tested with BlueZ 5.50. You can check which version you're using with
bluetoothd -v
.
dotnet add package HashtagChris.DotNetBlueZ --version 1.0.5-alpha
using HashtagChris.DotNetBlueZ;
...
string adapterName = "hci0";
IAdapter1 adapter = BlueZManager.GetAdapter(adapterName);
await adapter.StartDiscoveryAsync();
...
await adapter.StopDiscoveryAsync();
You can optionally use the extension method IAdapter1.WatchDevicesAddedAsync
to monitor for new devices being found during the scan.
IReadOnlyList<IDevice1> devices = await adapter.GetDevicesAsync();
TimeSpan timeout = TimeSpan.FromSeconds(15);
await device.ConnectAsync();
await device.WaitForPropertyValueAsync("Connected", device.GetConnectedAsync, value: true, timeout);
Example using GATT Device Information Service UUIDs.
string serviceUUID = "0000180a-0000-1000-8000-00805f9b34fb";
string characteristicUUID = "00002a24-0000-1000-8000-00805f9b34fb";
TimeSpan timeout = TimeSpan.FromSeconds(15);
await device.WaitForPropertyValueAsync("ServicesResolved", device.GetServicesResolvedAsync, value: true, timeout);
IGattService1 service = await device.GetServiceAsync(serviceUUID);
IGattCharacteristic1 characteristic = await service.GetCharacteristicAsync(characteristicUUID);
byte[] value = await characteristic.ReadValueAsync(timeout);
string modelName = Encoding.UTF8.GetString(value);