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