Skip to content

Commit

Permalink
feat(WindowsRt): store SDP record metadata for better device detection
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed May 17, 2024
1 parent 1c697ea commit feb981a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
12 changes: 9 additions & 3 deletions GalaxyBudsClient.Platform.WindowsRT/BluetoothDeviceRT.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
using Windows.Devices.Enumeration;
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Devices.Enumeration;
using GalaxyBudsClient.Platform.Interfaces;

namespace GalaxyBudsClient.Platform.WindowsRT
{
internal class BluetoothDeviceRt(DeviceInformation info) : BluetoothDevice(0)
internal class BluetoothDeviceRt(DeviceInformation info, IEnumerable<Guid> services) : BluetoothDevice(0)
{
public DeviceInformation DeviceInfo { get; } = info;
private IEnumerable<Guid>? _services = services;

public string Id => DeviceInfo.Id;
public override string Name => DeviceInfo.Name;
public override string Address => DeviceInfo.Properties["System.Devices.Aep.DeviceAddress"]?.ToString() ?? string.Empty;
public override bool IsPaired => (bool) (DeviceInfo.Properties["System.Devices.Aep.IsPaired"] ?? false);
public override bool IsConnected => (bool) (DeviceInfo.Properties["System.Devices.Aep.IsConnected"] ?? false);
public override Guid[]? ServiceUuids => _services?.ToArray();

public void Update(DeviceInformationUpdate? update)
public void Update(DeviceInformationUpdate? update, IEnumerable<Guid> services)
{
if (update == null)
{
return;
}

DeviceInfo.Update(update);
_services = services;
}
}
}
24 changes: 18 additions & 6 deletions GalaxyBudsClient.Platform.WindowsRT/BluetoothService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,25 @@ public BluetoothService()
requestedProperties,
DeviceInformationKind.AssociationEndpoint);

_deviceWatcher.Added += (watcher, deviceInfo) =>
_deviceWatcher.Added += async (watcher, deviceInfo) =>
{
Log.Debug("WindowsRT.BluetoothService: Device added: {DeviceInfoId}", deviceInfo.Id);
if (deviceInfo.Name != string.Empty)
{
_deviceCache?.Add(new BluetoothDeviceRt(deviceInfo));
var device = await global::Windows.Devices.Bluetooth.BluetoothDevice.FromIdAsync(deviceInfo.Id).AsTask();
var services = await device.GetRfcommServicesAsync(BluetoothCacheMode.Cached).AsTask();
var knownServices = services?.Services?.Select(x => x.ServiceId.Uuid);

_deviceCache.Add(new BluetoothDeviceRt(deviceInfo, knownServices));

Check warning on line 84 in GalaxyBudsClient.Platform.WindowsRT/BluetoothService.cs

View workflow job for this annotation

GitHub Actions / build-x64

Possible null reference argument for parameter 'services' in 'BluetoothDeviceRt.BluetoothDeviceRt(DeviceInformation info, IEnumerable<Guid> services)'.

Check warning on line 84 in GalaxyBudsClient.Platform.WindowsRT/BluetoothService.cs

View workflow job for this annotation

GitHub Actions / build-x86

Possible null reference argument for parameter 'services' in 'BluetoothDeviceRt.BluetoothDeviceRt(DeviceInformation info, IEnumerable<Guid> services)'.
}
};

#if Windows
_deviceWatcher.Updated += (watcher, deviceInfoUpdate) =>
_deviceWatcher.Updated += async (watcher, deviceInfoUpdate) =>
{
Log.Debug("WindowsRT.BluetoothService: Device updated: {Id}", deviceInfoUpdate?.Id);

_deviceCache?.Where(x => x?.Id == deviceInfoUpdate?.Id).ToList().ForEach(async x =>
_deviceCache.Where(x => x?.Id == deviceInfoUpdate?.Id).ToList().ForEach(async x =>
{
if (string.Equals(x.Address, _bluetoothDevice?.BluetoothAddressAsString(),
StringComparison.CurrentCultureIgnoreCase) &&
Expand All @@ -106,9 +110,18 @@ await ConnectAsync(x.Address,
}
}
});

IEnumerable<Guid>? knownServices = null;
if (deviceInfoUpdate != null)
{
var device = await global::Windows.Devices.Bluetooth.BluetoothDevice.FromIdAsync(deviceInfoUpdate.Id).AsTask();
var services = await device.GetRfcommServicesAsync(BluetoothCacheMode.Cached).AsTask();
knownServices = services?.Services?.Select(x => x.ServiceId.Uuid);
}

_deviceCache?.Where(x => deviceInfoUpdate?.Id == x?.Id)
.ToList()
.ForEach(x => x?.Update(deviceInfoUpdate ?? null));
.ForEach(x => x.Update(deviceInfoUpdate ?? null, knownServices));

Check warning on line 124 in GalaxyBudsClient.Platform.WindowsRT/BluetoothService.cs

View workflow job for this annotation

GitHub Actions / build-x64

Possible null reference argument for parameter 'services' in 'void BluetoothDeviceRt.Update(DeviceInformationUpdate? update, IEnumerable<Guid> services)'.

Check warning on line 124 in GalaxyBudsClient.Platform.WindowsRT/BluetoothService.cs

View workflow job for this annotation

GitHub Actions / build-x86

Possible null reference argument for parameter 'services' in 'void BluetoothDeviceRt.Update(DeviceInformationUpdate? update, IEnumerable<Guid> services)'.
};

_deviceWatcher.Removed += (watcher, deviceInfoUpdate) =>
Expand Down Expand Up @@ -380,7 +393,6 @@ public async Task SendAsync(byte[] data)
public async Task<BluetoothDevice[]> GetDevicesAsync()
{
await Task.Delay(100);
// TODO Add UUIDs to the device cache
return _deviceCache.Cast<BluetoothDevice>().ToArray();
}

Expand Down
2 changes: 1 addition & 1 deletion GalaxyBudsClient.Platform/Interfaces/IBluetoothService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override string ToString()
public virtual bool IsConnected { get; } = isConnected;
public virtual bool IsPaired { get; } = isPaired;
public BluetoothCoD Class { get; } = cod;
public Guid[]? ServiceUuids { get; } = serviceUuids;
public virtual Guid[]? ServiceUuids { get; } = serviceUuids;

public static IEnumerable<BluetoothDevice> DummyDevices()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class BatteryHistoryPageViewModel : SubPageViewModelBase
[Reactive] public BatteryHistoryTimeSpans SelectedTimeSpan { set; get; } = BatteryHistoryTimeSpans.Last12Hours;
[Reactive] public BatteryHistoryTools SelectedTool { set; get; } = BatteryHistoryTools.PanAndZoom;

// TODO show warning if not enough data is available
public BatteryHistoryPageViewModel()
{
PropertyChanged += OnPropertyChanged;
Expand Down
2 changes: 1 addition & 1 deletion GalaxyBudsClient/i18n/en.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ This program is designed to provide much more functionality over the official Sa
<sys:String x:Key="devsel_nodevices_title">No paired devices found</sys:String>
<sys:String x:Key="devsel_invalid_selection">Please select a valid device</sys:String>
<sys:String x:Key="devsel_winrt_title">Use alternative Bluetooth backend</sys:String>
<sys:String x:Key="devsel_winrt">Much better stability, but may not work on all PCs with some Bluetooth adapters</sys:String>
<sys:String x:Key="devsel_winrt">Much better stability, but may not work on all PCs with some Bluetooth adapters (A restart may be required!)</sys:String>

<!--Device page-->
<sys:String x:Key="devices_header">Paired devices</sys:String>
Expand Down

0 comments on commit feb981a

Please sign in to comment.