Giter Club home page Giter Club logo

plugin.bluez's Introduction

Linux Bluetooth with Plugin.BlueZ

Announcement

The project is officially changing names to, Linux.Bluetooth!

This repository will remain and eventually marked as, read-only. Please migrate your project over to the new NuGet package to continue getting the latest features and builds.

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

Linux.Bluetooth NuGet Badge

Plugin.BlueZ NuGet Badge

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

Check out the SuessLabs article on using Plugin.BlueZ

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

Reference

plugin.bluez's People

Contributors

corentin-raoult avatar damiansuess avatar hashtagchris avatar yaakov-h avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

plugin.bluez's Issues

Upgrade Wishlist for 2.0

Refactoring:

  • Rename Device.GetAllAsync() to ..GetPropertiesAsync()
  • Rename Device.GetUUIDsAsync() to ..GetServiceUuidsAsync()
  • Include DeviceExtensions as part of Device class
  • Rename Device.GetConnectedAsync() to IsConnectedAsync()

Normalization:

  • Use custom Interfaces versus BlueZ dependent ones

Bump NuGet Version

Create NuGet package for the latest changes.

Prepare for package renaming

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.