Skip to content

A quick and dirty library for BlueZ's D-Bus APIs. Focus is on Bluetooth Low Energy APIs.

License

Notifications You must be signed in to change notification settings

Oscariremma/DotNet-BlueZ

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DotNet-BlueZ

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".

Requirements

  • 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.

Installation

dotnet add package HashtagChris.DotNetBlueZ --version 1.0.5-alpha

Usage

Get a Bluetooth adapter

using HashtagChris.DotNetBlueZ;
...

string adapterName = "hci0";
IAdapter1 adapter = BlueZManager.GetAdapter(adapterName);

Scan for Bluetooth devices

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.

Get Devices

IReadOnlyList<IDevice1> devices = await adapter.GetDevicesAsync();

Connecting to a Device

TimeSpan timeout = TimeSpan.FromSeconds(15);

await device.ConnectAsync();
await device.WaitForPropertyValueAsync("Connected", device.GetConnectedAsync, value: true, timeout);

Retrieve a GATT Service and Characteristic

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);

Read a GATT Characteristic value

byte[] value = await characteristic.ReadValueAsync(timeout);

string modelName = Encoding.UTF8.GetString(value);

Reference

About

A quick and dirty library for BlueZ's D-Bus APIs. Focus is on Bluetooth Low Energy APIs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 98.8%
  • Shell 1.2%