Darp.Ble.Mock 1.2.2

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

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

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)

Create new release

To create a release, simply push a new tag with pattern vX.Y.Z. This will trigger a workflow releasing the new version.

git tag vX.Y.Z
git push origin vX.Y.Z
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.2.5 62 11/18/2024
1.2.4 76 10/23/2024
1.2.3 124 8/18/2024
1.2.2 102 7/14/2024
1.2.1 151 5/10/2024
1.2.0 134 5/4/2024
0.1.1 222 4/18/2024
0.1.0 120 4/18/2024