Darp.Ble.WinRT
1.2.6
dotnet add package Darp.Ble.WinRT --version 1.2.6
NuGet\Install-Package Darp.Ble.WinRT -Version 1.2.6
<PackageReference Include="Darp.Ble.WinRT" Version="1.2.6" />
paket add Darp.Ble.WinRT --version 1.2.6
#r "nuget: Darp.Ble.WinRT, 1.2.6"
// Install Darp.Ble.WinRT as a Cake Addin #addin nuget:?package=Darp.Ble.WinRT&version=1.2.6 // Install Darp.Ble.WinRT as a Cake Tool #tool nuget:?package=Darp.Ble.WinRT&version=1.2.6
Darp.Ble
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
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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0-windows10.0.19041 is compatible. |
-
net8.0-windows10.0.19041
- Darp.Ble (>= 1.2.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.