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
19 changes: 19 additions & 0 deletions src/Linux.Bluetooth/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Device : IDevice1, IDisposable
{
private const string DeviceConnected = "Connected";
private const string DeviceServicesResolved = "ServicesResolved";
private const string DeviceRSSI = "RSSI";

private IDevice1 _proxy;
private IDisposable _propertyWatcher;
Expand Down Expand Up @@ -64,6 +65,16 @@ public event DeviceEventHandlerAsync Connected

public event DeviceEventHandlerAsync Disconnected;

/// <summary>Raised when the device's RSSI property changes, carrying the new value in dBm.</summary>
public event EventHandler<short>? RSSIChanged;

/// <summary>
/// Raised for every Device1 property change, carrying the raw <see cref="PropertyChanges"/>. Covers
/// properties without a dedicated typed event (e.g. ManufacturerData, TxPower). Note that changes also
/// covered by a typed event (Connected, ServicesResolved, RSSI) are delivered on both.
/// </summary>
public event EventHandler<PropertyChanges>? PropertyChanged;

public event DeviceEventHandlerAsync ServicesResolved
{
add
Expand Down Expand Up @@ -244,8 +255,16 @@ private void OnPropertyChanges(PropertyChanges changes)
OnResolved?.Invoke(this, new BlueZEventArgs());

break;

case DeviceRSSI:
if (pair.Value is short sRSSI)
RSSIChanged?.Invoke(this, sRSSI);

break;
}
}

PropertyChanged?.Invoke(this, changes);
}
}
}
25 changes: 14 additions & 11 deletions src/Linux.Bluetooth/Extensions/WatchableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class WatchableExtensions
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IAdapter1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>Wait for AdvertisingManager's Property and specified value to resolve.</summary>
/// <typeparam name="T">Type of value.</typeparam>
Expand All @@ -28,7 +28,7 @@ public static Task WaitForPropertyValueAsync<T>(this IAdapter1 obj, string prope
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this ILEAdvertisingManager1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>Wait for Device's Property and specified value to resolve.</summary>
/// <typeparam name="T">Type of value.</typeparam>
Expand All @@ -39,7 +39,7 @@ public static Task WaitForPropertyValueAsync<T>(this ILEAdvertisingManager1 obj,
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IDevice1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>Wait for Battery's Property and specified value to resolve.</summary>
/// <typeparam name="T">Type of value.</typeparam>
Expand All @@ -50,7 +50,7 @@ public static Task WaitForPropertyValueAsync<T>(this IDevice1 obj, string proper
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IBattery1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/*
/// <summary>Wait for GattService's Property and specified value to resolve.</summary>
Expand All @@ -62,7 +62,7 @@ public static Task WaitForPropertyValueAsync<T>(this IBattery1 obj, string prope
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IGattService1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>Wait for GattCharacteristic's Property and specified value to resolve.</summary>
/// <typeparam name="T">Type of value.</typeparam>
Expand All @@ -73,7 +73,7 @@ public static Task WaitForPropertyValueAsync<T>(this IGattService1 obj, string p
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IGattCharacteristic1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>Wait for GattDescriptor's Property and specified value to resolve.</summary>
/// <typeparam name="T">Type of value.</typeparam>
Expand All @@ -84,7 +84,7 @@ public static Task WaitForPropertyValueAsync<T>(this IGattCharacteristic1 obj, s
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IGattDescriptor1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
*/

/// <summary>Wait for MediaControl's Property and specified value to resolve.</summary>
Expand All @@ -96,7 +96,7 @@ public static Task WaitForPropertyValueAsync<T>(this IGattDescriptor1 obj, strin
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IMediaControl1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>
/// Wait for watchable objects property and specified value to resolve.
Expand All @@ -110,10 +110,13 @@ public static Task WaitForPropertyValueAsync<T>(this IMediaControl1 obj, string
/// <param name="timeout">TimeSpan to wait for.</param>
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
private static async Task WaitForPropertyValueInternalAsync<T>(
public static async Task WaitForPropertyValueAsync<T>(
Func<string, Task<T>> getAsync,
Func<Action<PropertyChanges>, Task<IDisposable>> watchPropertiesAsync,
string propertyName, T value, TimeSpan timeout)
Func<Action<PropertyChanges>,
Task<IDisposable>> watchPropertiesAsync,
string propertyName,
T value,
TimeSpan timeout)
{
// TODO: Change to Task<bool> versus throwing an error.
var (watchTask, watcher) = WaitForPropertyValueInternal(watchPropertiesAsync, propertyName, value);
Expand Down