Darp.Ble.HciHost 1.2.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package Darp.Ble.HciHost --version 1.2.5                
NuGet\Install-Package Darp.Ble.HciHost -Version 1.2.5                
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="Darp.Ble.HciHost" Version="1.2.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Darp.Ble.HciHost --version 1.2.5                
#r "nuget: Darp.Ble.HciHost, 1.2.5"                
#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 Darp.Ble.HciHost as a Cake Addin
#addin nuget:?package=Darp.Ble.HciHost&version=1.2.5

// Install Darp.Ble.HciHost as a Cake Tool
#tool nuget:?package=Darp.Ble.HciHost&version=1.2.5                

Darp.Ble

.NET NuGet Downloads License

Darp.Ble is an abstraction layer which aims to provide a simple, reactive way of using BLE with .Net while maintaining granular configuration options.

Disclaimer

This package is under heavy development. Huge changes to the API are to be expected!

Implementations

Implementation Observer Central Broadcaster Peripheral Nuget
Darp.Ble.WinRT X X X X Darp.Ble.WinRT
Darp.Ble.Android X X Darp.Ble.Android
Darp.Ble.HciHost X X Darp.Ble.HciHost
Darp.Ble.iOS planned
Darp.Ble.Mac planned
Darp.Ble.BlueZ planned
Darp.Ble.Mock X X Darp.Ble.Mock

Features

Initialize BLE device

To get a BleDevice, you will need a BleManager. When creating it implementation specific factories are registered. EnumerateDevices then looks through all defined implementations, yields them to the user and allows for device selection. Enumerating the devices does not connect to it yet.

This is done when calling InitializeAsync. After that, the device will be usable. To release resources call DisposeAsync.

var bleManager = new BleManagerBuilder()
    .Add<WinBleFactory>()
    .Add(new HciHostBleFactory("COM7"))
    .CreateManager();

var bleDevice = bleManager.EnumerateDevices().First();
await bleDevice.InitializeAsync();

...

await bleDevice.DisposeAsync();

Observer

If the device supports observer mode, use the Observer property. The observer works as a connectable observable. Subscribe to it to receive events and connect to actually start the scan. To stop the scan, dispose of the return of the connection or use StopScan.

var observer = bleDevice.Observer;
observer.Configure(new BleScanParameters
{
    ScanType = ScanType.Active,
    ScanInterval = ScanTiming.Ms100,
    ScanWindow = ScanTiming.Ms100,
});
observer.Subscribe(advertisement =>
{
    Console.WriteLine($"Received advertisement from {advertisement.Address}");
});
var disposable = observer.Connect();

Central

If the device supports central mode, use the Central property. .ConnectToPeripheral(), publishes a peer peripheral once connected.

After that, service and characteristic discovery it is possible to read/write/subscribe.

  • Notify: .OnNotify() gives back a connectable observable. You can register subscribers before actually subscribing using .Connect().
  • Write: .WriteAsync() allows you to write an array of bytes to the peerDevice
var central = bleDevice.Central;

var peerDevice = await central
    .ConnectToPeripheral(new BleAddress(BleAddressType.Public, (UInt48)0xAABBCCDDEEFF))
    .FirstAsync();

var service = await peerDevice.DiscoverServiceAsync(new BleUuid(0x1234));
var writeChar = await service.DiscoverCharacteristicAsync<Properties.Write>(new BleUuid(0x5678));
var notifyChar = await service.DiscoverCharacteristicAsync<Properties.Notify>(new BleUuid(0xABCD));
var notifyTask = notifyChar.OnNotify().RefCount().FirstAsync().ToTask();
await writeChar.WriteAsync([0x00, 0x02, 0x03, 0x04]);
var resultBytes = await notifyTask;

Further Documentation

Additional documentation exists in form of the unit tests.

Why use this library?

When deciding to write this library, we were unable to find a library meeting all of our requirements:

  • C# language support
  • Cross-platform including the ability to communicate with BLE dongles
  • Granular configuration options if the platform supports it
  • (Reactive interface)

Setup Android development

You can install JDK and AndroidSDK using the InstallAndroidDependencies target. Optionally, you can set the environment variables to avoid having to set the directories in each build.

Note: The path C:/work/... is just an example

After that, restore the dotnet workloads to install the required android workload.

dotnet build -t:InstallAndroidDependencies -f net8.0-android -p:AndroidSdkDirectory=c:\work\android-sdk -p:JavaSdkDirectory=c:\work\jdk -p:AcceptAndroidSdkLicenses=True
setx JAVA_HOME C:\work\jdk\
setx ANDROID_HOME C:\work\android-sdk\

# Restore android workload
dotnet workload restore
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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
1.3.2 79 12/16/2024
1.3.1 93 12/13/2024
1.3.0 76 12/13/2024
1.2.6 82 11/23/2024
1.2.5 87 11/18/2024
1.2.4 95 10/23/2024
1.2.3 128 8/18/2024
1.2.2 109 7/14/2024
1.2.1 157 5/10/2024
1.2.0 140 5/4/2024
0.1.1 114 4/18/2024
0.1.0 106 4/18/2024