Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# DotNet-BlueZ
A quick and dirty library for BlueZ's D-Bus APIs. Primary focus is Bluetooth Low Energy.

[![DotNetBlueZ NuGet Badge](https://buildstats.info/nuget/HashtagChris.DotNetBlueZ?dWidth=70&includePreReleases=true)](https://www.nuget.org/packages/HashtagChris.DotNetBlueZ/)
[![DotNetBlueZ NuGet Badge](https://buildstats.info/nuget/DotnetBlueZ?dWidth=70&includePreReleases=true)](https://www.nuget.org/packages/DotnetBlueZ/)

Uses [Tmds.DBus](https://github.com/tmds/Tmds.DBus) to access D-Bus. Tmds.DBus.Tool was used to generate the D-Bus object interfaces. D-Bus is the preferred interface for Bluetooth in userspace. The [Doing Bluetooth Low Energy on Linux](https://elinux.org/images/3/32/Doing_Bluetooth_Low_Energy_on_Linux.pdf) presentation says "Use D-Bus API (documentation in [doc/]((https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc))) whenever possible".

Expand All @@ -13,7 +13,7 @@ Uses [Tmds.DBus](https://github.com/tmds/Tmds.DBus) to access D-Bus. Tmds.DBus.T
# Installation

```bash
dotnet add package HashtagChris.DotNetBlueZ
dotnet add package DotnetBlueZ
```

# Events
Expand All @@ -25,7 +25,7 @@ C# events are available for several properties. Events are useful for properly h
## Get a Bluetooth adapter

```C#
using HashtagChris.DotNetBlueZ;
using DotnetBlueZ;
...

IAdapter1 adapter = (await BlueZManager.GetAdaptersAsync()).FirstOrDefault();
Expand Down
4 changes: 2 additions & 2 deletions manualTests/DotNetBlueZTest1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HashtagChris.DotNetBlueZ;
using HashtagChris.DotNetBlueZ.Extensions;
using DotnetBlueZ;
using DotnetBlueZ.Extensions;

namespace DotNetBlueZTest1
{
Expand Down
6 changes: 3 additions & 3 deletions samples/printDeviceInfo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// This program is the equivalent of the sample code posted to https://stackoverflow.com/questions/53933345/utilizing-bluetooth-le-on-raspberry-pi-using-net-core/56623587#56623587
// This uses HashtagChris.DotNetBlueZ instead of Tmds.DBus directly.
// This uses DotnetBlueZ instead of Tmds.DBus directly.
//
// Use the `bluetoothctl` command-line tool or the Bluetooth Manager GUI to scan for devices and possibly pair.
// Then you can use this program to connect and print "Device Information" GATT service values.
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HashtagChris.DotNetBlueZ;
using HashtagChris.DotNetBlueZ.Extensions;
using DotnetBlueZ;
using DotnetBlueZ.Extensions;

class Program
{
Expand Down
4 changes: 2 additions & 2 deletions samples/scan/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using HashtagChris.DotNetBlueZ;
using HashtagChris.DotNetBlueZ.Extensions;
using DotnetBlueZ;
using DotnetBlueZ.Extensions;

namespace Scan
{
Expand Down
4 changes: 2 additions & 2 deletions samples/subscribeToCharacteristic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HashtagChris.DotNetBlueZ;
using HashtagChris.DotNetBlueZ.Extensions;
using DotnetBlueZ;
using DotnetBlueZ.Extensions;

// An event-driven example that subscribes to one GATT characteristic and prints the value on updates.
namespace subscribeToCharacteristic
Expand Down
2 changes: 1 addition & 1 deletion script/nuget-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SCRIPT_PATH=`dirname $0`

NUGET_PKG=$1
if [ -z "$NUGET_PKG" ]; then
NUGET_PKG=`find $SCRIPT_PATH/.. -name "HashtagChris.DotNetBlueZ.*.nupkg" | sort | tail -1`
NUGET_PKG=`find $SCRIPT_PATH/.. -name "DotnetBlueZ.*.nupkg" | sort | tail -1`
fi

if [ -z "$NUGET_PKG" ]; then
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using HashtagChris.DotNetBlueZ.Extensions;
using DotnetBlueZ.Extensions;
using Tmds.DBus;

namespace HashtagChris.DotNetBlueZ
namespace DotnetBlueZ
{
public delegate Task DeviceChangeEventHandlerAsync(Adapter sender, DeviceFoundEventArgs eventArgs);

Expand Down
2 changes: 1 addition & 1 deletion src/BlueZManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Threading.Tasks;
using Tmds.DBus;

namespace HashtagChris.DotNetBlueZ
namespace DotnetBlueZ
{
public static class BlueZManager
{
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace HashtagChris.DotNetBlueZ
namespace DotnetBlueZ
{
public static class BluezConstants
{
Expand Down
2 changes: 1 addition & 1 deletion src/DBusInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Tmds.DBus;

[assembly: InternalsVisibleTo(Tmds.DBus.Connection.DynamicAssemblyName)]
namespace HashtagChris.DotNetBlueZ
namespace DotnetBlueZ
{
[DBusInterface("org.freedesktop.DBus.ObjectManager")]
public interface IObjectManager : IDBusObject
Expand Down
2 changes: 1 addition & 1 deletion src/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Threading.Tasks;
using Tmds.DBus;

namespace HashtagChris.DotNetBlueZ
namespace DotnetBlueZ
{
public delegate Task DeviceEventHandlerAsync(Device sender, BlueZEventArgs eventArgs);

Expand Down
14 changes: 7 additions & 7 deletions src/DotNetBlueZ.csproj
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>HashtagChris.DotNetBlueZ</RootNamespace>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>DotnetBlueZ</RootNamespace>
</PropertyGroup>

<!-- NuGet -->
<PropertyGroup>
<PackageId>HashtagChris.DotNetBlueZ</PackageId>
<Version>1.3.1</Version>
<Authors>Chris Sidi</Authors>
<PackageId>DotNetBlueZ</PackageId>
<Version>1.0.1</Version>
<Authors>Onur Coskun</Authors>
<Title>Quick and dirty library for BlueZ's D-Bus APIs</Title>
<Description>A quick and dirty library for BlueZ's D-Bus APIs. Focus is on Bluetooth Low Energy APIs.</Description>
<PackageTags>BlueZ;net-core;netstandard;Bluetooth;BluetoothLowEnergy;BLE</PackageTags>
<RepositoryUrl>https://github.com/hashtagchris/DotNet-BlueZ</RepositoryUrl>
<RepositoryUrl>https://github.com/onurcoskun14/DotNet-BlueZ</RepositoryUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Tmds.DBus" Version="0.7.0" />
<PackageReference Include="Tmds.DBus" Version="0.16.0" />
<DotNetCliToolReference Include="Tmds.DBus.Tool" Version="0.7.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/EventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace HashtagChris.DotNetBlueZ
namespace DotnetBlueZ
{
public class BlueZEventArgs : EventArgs
{
Expand Down
60 changes: 35 additions & 25 deletions src/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Threading.Tasks;
using Tmds.DBus;

namespace HashtagChris.DotNetBlueZ.Extensions
namespace DotnetBlueZ.Extensions
{
public static class Extensions
{
Expand Down Expand Up @@ -144,34 +144,44 @@ public static async Task WaitForPropertyValueAsync<T>(this IDevice1 obj, string
await watchTask;
}

private static (Task, IDisposable) WaitForPropertyValueInternal<T>(IDevice1 obj, string propertyName, T value)
private static (Task<bool>, IDisposable) WaitForPropertyValueInternal<T>(IDevice1 obj, string propertyName, T value)
{
var taskSource = new TaskCompletionSource<bool>();

IDisposable watcher = null;
watcher = obj.WatchPropertiesAsync(propertyChanges => {
try
var taskSource = new TaskCompletionSource<bool>();
var isCompleted = false;

IDisposable watcher = null;
watcher = obj.WatchPropertiesAsync(propertyChanges =>
{
if (propertyChanges.Changed.Any(kvp => kvp.Key == propertyName))
{
var pair = propertyChanges.Changed.Single(kvp => kvp.Key == propertyName);
if (pair.Value.Equals(value))
try
{
// Console.WriteLine($"[CHG] {propertyName}: {pair.Value}.");
taskSource.SetResult(true);
watcher.Dispose();
if (isCompleted)
{
watcher.Dispose();
return;
}

if (propertyChanges.Changed.Any(kvp => kvp.Key == propertyName))
{
var pair = propertyChanges.Changed.Single(kvp => kvp.Key == propertyName);
if (pair.Value.Equals(value))
{
// Console.WriteLine($"[CHG] {propertyName}: {pair.Value}.");
isCompleted = true;
taskSource.SetResult(true);
watcher.Dispose();
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex}");
taskSource.SetException(ex);
watcher.Dispose();
}
});

return (taskSource.Task, watcher);
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex}");
isCompleted = true;
taskSource.SetException(ex);
watcher.Dispose();
}
});

return (taskSource.Task, watcher);
}
}
}
2 changes: 1 addition & 1 deletion src/GattCharacteristic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Threading.Tasks;
using Tmds.DBus;

namespace HashtagChris.DotNetBlueZ
namespace DotnetBlueZ
{
public delegate Task GattCharacteristicEventHandlerAsync(GattCharacteristic sender, GattCharacteristicValueEventArgs eventArgs);

Expand Down