Plugin.BlueZ 5.64.0

Suggested Alternatives

Linux.Bluetooth

Additional Details

The Plugin.BlueZ package is no longer maintained and has moved to, Linux.Bluetooth. Still brought to you by Suess Labs and Xeno Innovations!

dotnet add package Plugin.BlueZ --version 5.64.0                
NuGet\Install-Package Plugin.BlueZ -Version 5.64.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Plugin.BlueZ" Version="5.64.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Plugin.BlueZ --version 5.64.0                
#r "nuget: Plugin.BlueZ, 5.64.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Plugin.BlueZ as a Cake Addin
#addin nuget:?package=Plugin.BlueZ&version=5.64.0

// Install Plugin.BlueZ as a Cake Tool
#tool nuget:?package=Plugin.BlueZ&version=5.64.0                

Linux Bluetooth with Plugin.BlueZ

A .NET Bluetooth LE library for Linux using BlueZ's D-Bus APIs.

Plugin.BlueZ NuGet Badge

The library uses, Tmds.DBus to access Linux's D-Bus, the preferred interface for Bluetooth in userspace.

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.

Supported Distributions

Plugin.BlueZ aims to support Linux Distributions where both .NET and BlueZ is supported. Officially, this NuGet package has only been tested on Ubuntu 20.04.

List of BlueZ supported distros:

  • Ubuntu Linux
  • Raspbian (Raspberry PI)
  • Debian GNU/Linux
  • Fedora Core / Red Hat Linux
  • OpenSuSE / SuSE Linux
  • Mandrake Linux
  • Gentoo Linux
  • Chrome OS

Installation

dotnet add package Plugin.BlueZ

Usage

C# events are available for several properties. Events are useful for properly handling disconnects and reconnects.

Get a Bluetooth adapter

using Plugin.BlueZ;
...
IAdapter1 adapter = (await BlueZManager.GetAdaptersAsync()).FirstOrDefault();

or get a particular adapter:

IAdapter1 adapter = await BlueZManager.GetAdapterAsync(adapterName: "hci0");

Scan for Bluetooth devices

adapter.DeviceFound += adapter_DeviceFoundAsync;

await adapter.StartDiscoveryAsync();
...
await adapter.StopDiscoveryAsync();

Get Devices

adapter.DeviceFound (above) will be called immediately for existing devices, and as new devices show up during scanning; eventArgs.IsStateChange can be used to distinguish between existing and new devices. Alternatively you can can use GetDevicesAsync:

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

Connect to a Device

device.Connected += device_ConnectedAsync;
device.Disconnected += device_DisconnectedAsync;
device.ServicesResolved += device_ServicesResolvedAsync;

await device.ConnectAsync();

Alternatively, you can wait for "Connected" and "ServicesResolved" to equal true:

TimeSpan timeout = TimeSpan.FromSeconds(15);

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

Retrieve a GATT Service and Characteristic

Prerequisite: You must be connected to a device and services must be resolved. You may need to pair with the device in order to use some services.

Example using GATT Device Information Service UUIDs.

string serviceUUID = "0000180a-0000-1000-8000-00805f9b34fb";
string characteristicUUID = "00002a24-0000-1000-8000-00805f9b34fb";

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

Subscribe to GATT Characteristic Notifications

characteristic.Value += characteristic_Value;
...

private static async Task characteristic_Value(GattCharacteristic characteristic, GattCharacteristicValueEventArgs e)
{
  try
  {
    Console.WriteLine($"Characteristic value (hex): {BitConverter.ToString(e.Value)}");

    Console.WriteLine($"Characteristic value (UTF-8): \"{Encoding.UTF8.GetString(e.Value)}\"");
  }
  catch (Exception ex)
  {
    Console.Error.WriteLine(ex);
  }
}

Tips

It may be necessary to pair with a device for a GATT service to be visible or for reading GATT characteristics to work. To pair, one option is to run bluetoothctl (or sudo bluetoothctl) and then run default agent and agent on within bluetoothctl. Watch bluetoothctl for pairing requests.

See Ubuntu's Introduction to Pairing.

BluetoothCtl Helper

From the command line, use bluetoothctl or Bluetooth Manager to scan and retrieve device UUIDs and Services to assist with debugging.."

$ bluetoothctl

; Scan for devices
scan on

; Stop Scanning
scan off

; List known devices
devices

Generating

Tmds.DBus.Tool is used to generate the D-Bus object interfaces.

Contributing

See Contributing.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.64.0 2,116 7/26/2022 5.64.0 is deprecated because it is no longer maintained.
5.53.1 522 4/27/2022
1.5.0 1,993 3/15/2022
1.4.1 453 3/10/2022