Skip to content

Commit 81d0d96

Browse files
blackspherefollowerqdot
authored andcommitted
Attempted fix for disappearing BLE advertisment
Fixes #223
1 parent 6d8b828 commit 81d0d96

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

Buttplug.Server.Managers.UWPBluetoothManager/UWPBluetoothDeviceFactory.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using Buttplug.Core;
@@ -8,7 +9,6 @@
89
using Windows.Devices.Bluetooth;
910
using Windows.Devices.Bluetooth.Advertisement;
1011
using Windows.Devices.Bluetooth.GenericAttributeProfile;
11-
using System.Collections.Generic;
1212

1313
namespace Buttplug.Server.Managers.UWPBluetoothManager
1414
{
@@ -31,12 +31,8 @@ public UWPBluetoothDeviceFactory([NotNull] IButtplugLogManager aLogManager, [Not
3131
_deviceInfo = aInfo;
3232
}
3333

34-
public bool MayBeDevice(BluetoothLEAdvertisement aAdvertisement)
34+
public bool MayBeDevice(string advertName, List<Guid> advertGUIDs)
3535
{
36-
var advertName = aAdvertisement.LocalName;
37-
var advertGUIDs = new List<Guid>();
38-
advertGUIDs.AddRange(aAdvertisement.ServiceUuids);
39-
4036
if (_deviceInfo.Names.Any() && !_deviceInfo.Names.Contains(advertName))
4137
{
4238
return false;

Buttplug.Server.Managers.UWPBluetoothManager/UWPBluetoothManager.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,21 @@ public UWPBluetoothManager(IButtplugLogManager aLogManager)
5858
private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher aObj,
5959
BluetoothLEAdvertisementReceivedEventArgs aEvent)
6060
{
61+
var advertName = aEvent.Advertisement.LocalName;
62+
var advertGUIDs = new List<Guid>();
63+
advertGUIDs.AddRange(aEvent.Advertisement.ServiceUuids);
64+
var btAddr = aEvent.BluetoothAddress;
65+
6166
// BpLogger.Trace($"Got BLE Advertisement for device: {aEvent.Advertisement.LocalName} / {aEvent.BluetoothAddress}");
62-
if (_currentlyConnecting.Contains(aEvent.BluetoothAddress))
67+
if (_currentlyConnecting.Contains(btAddr))
6368
{
6469
// BpLogger.Trace($"Ignoring advertisement for already connecting device: {aEvent.Advertisement.LocalName} / {aEvent.BluetoothAddress}");
6570
return;
6671
}
6772

68-
BpLogger.Trace("BLE device found: " + aEvent.Advertisement.LocalName);
73+
BpLogger.Trace("BLE device found: " + advertName);
6974
var factories = from x in _deviceFactories
70-
where x.MayBeDevice(aEvent.Advertisement)
75+
where x.MayBeDevice(advertName, advertGUIDs)
7176
select x;
7277

7378
// We should always have either 0 or 1 factories.
@@ -76,7 +81,7 @@ where x.MayBeDevice(aEvent.Advertisement)
7681
{
7782
if (buttplugBluetoothDeviceFactories.Any())
7883
{
79-
BpLogger.Warn($"Found multiple BLE factories for {aEvent.Advertisement.LocalName} {aEvent.BluetoothAddress}:");
84+
BpLogger.Warn($"Found multiple BLE factories for {advertName} {btAddr}:");
8085
buttplugBluetoothDeviceFactories.ToList().ForEach(x => BpLogger.Warn(x.GetType().Name));
8186
}
8287
else
@@ -87,12 +92,12 @@ where x.MayBeDevice(aEvent.Advertisement)
8792
return;
8893
}
8994

90-
_currentlyConnecting.Add(aEvent.BluetoothAddress);
95+
_currentlyConnecting.Add(btAddr);
9196
var factory = buttplugBluetoothDeviceFactories.First();
9297
BpLogger.Debug($"Found BLE factory: {factory.GetType().Name}");
9398

9499
// If we actually have a factory for this device, go ahead and create the device
95-
var fromBluetoothAddressAsync = BluetoothLEDevice.FromBluetoothAddressAsync(aEvent.BluetoothAddress);
100+
var fromBluetoothAddressAsync = BluetoothLEDevice.FromBluetoothAddressAsync(btAddr);
96101
if (fromBluetoothAddressAsync != null)
97102
{
98103
var dev = await fromBluetoothAddressAsync;
@@ -108,13 +113,13 @@ where x.MayBeDevice(aEvent.Advertisement)
108113
catch (Exception ex)
109114
{
110115
BpLogger.Error(
111-
$"Cannot connect to device {aEvent.Advertisement.LocalName} {aEvent.BluetoothAddress}: {ex.Message}");
112-
_currentlyConnecting.Remove(aEvent.BluetoothAddress);
116+
$"Cannot connect to device {advertName} {btAddr}: {ex.Message}");
117+
_currentlyConnecting.Remove(btAddr);
113118
return;
114119
}
115120
}
116121

117-
_currentlyConnecting.Remove(aEvent.BluetoothAddress);
122+
_currentlyConnecting.Remove(btAddr);
118123
}
119124

120125
private void OnWatcherStopped(BluetoothLEAdvertisementWatcher aObj,

0 commit comments

Comments
 (0)