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
11 changes: 10 additions & 1 deletion src/Linux.Bluetooth/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public void Dispose()
GC.SuppressFinalize(this);
}

/// <summary>
/// Raised for each device found. Subscribing replays the devices already known to BlueZ.
/// The subscriber owns every <see cref="Device"/> it receives and must dispose it; each one
/// holds a D-Bus match rule until disposed.
/// </summary>
public event DeviceChangeEventHandlerAsync DeviceFound
{
add
Expand Down Expand Up @@ -332,10 +337,11 @@ private async void TrackDeviceForConnection(ObjectPath objectPath)
_connTrackedDevices[objectPath] = null!;
}

Device? device = null;
try
{
var proxy = Connection.System.CreateProxy<IDevice1>(BluezConstants.DbusService, objectPath);
var device = await Device.CreateAsync(proxy);
device = await Device.CreateAsync(proxy);

// Device.Connected can fire twice (add-accessor replay + live change), so relay only
// on an actual transition.
Expand Down Expand Up @@ -372,6 +378,9 @@ private async void TrackDeviceForConnection(ObjectPath objectPath)
_connTrackedDevices.Remove(objectPath);
}

// The reserved slot is gone, so nothing else will ever dispose this device.
device?.Dispose();

Console.WriteLine($"Error tracking device '{objectPath}' for connection: {ex.Message}");
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Linux.Bluetooth/Extensions/AdapterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace Linux.Bluetooth.Extensions
public static class AdapterExtensions
{
/// <summary>Get available devices.</summary>
/// <remarks>
/// The caller owns the returned devices and must dispose each one. Every <see cref="Device"/>
/// holds a D-Bus match rule; an undisposed device keeps its rule until the connection closes,
/// and a connection is limited to <c>max_match_rules_per_connection</c> (2048 by default).
/// </remarks>
/// <param name="adapter">Adapter object.</param>
/// <returns>Collection of <seealso cref="Device"/>s.</returns>
public static async Task<IReadOnlyList<Device>> GetDevicesAsync(this IAdapter1 adapter)
Expand All @@ -19,6 +24,7 @@ public static async Task<IReadOnlyList<Device>> GetDevicesAsync(this IAdapter1 a
}

/// <summary>Get <seealso cref="Device"/> object from the specifed BLE address.</summary>
/// <remarks>The caller owns the returned device and must dispose it. See <see cref="GetDevicesAsync"/>.</remarks>
/// <param name="adapter">Adapter object.</param>
/// <param name="deviceAddress">BLE Device Address.</param>
/// <returns><seealso cref="Device"/> object or NULL if not found.</returns>
Expand Down Expand Up @@ -52,6 +58,7 @@ public static async Task<IReadOnlyList<Device>> GetDevicesAsync(this IAdapter1 a
}

/// <summary>Disposable object which waits for discovered devices.</summary>
/// <remarks>The handler owns each device it receives and must dispose it. See <see cref="GetDevicesAsync"/>.</remarks>
/// <param name="adapter">Adapter object.</param>
/// <param name="handler">Action delegate with <seealso cref="Device"/> as a parameter.</param>
/// <returns>Disposable object.</returns>
Expand Down