Skip to content

Commit bf90a31

Browse files
blackspherefollowerqdot
authored andcommitted
fix: GetGattServicesForUuidAsync() failed to find services
This was an issue discovered with the MagicMotion Vini which wasn't showing up due to not getting a service returned. The service was present and useable when GetServicesAsync() was called instead. Additioanlly: - more logging is now emitted from the UWP BLE manager when this sort of error occures - only one attempt to create a connection to a specific device happens at once now (previously multiple threads could be launched to create a connection for a single device).
1 parent 9bd1c53 commit bf90a31

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Buttplug.Server.Managers.UWPBluetoothManager/UWPBluetoothDeviceInterface.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,22 @@ public static async Task<IButtplugDeviceImpl> Create(IButtplugLogManager aLogMan
6060
protectedasyncTask<GattDeviceService>GetService(GuidaServiceGuid)
6161
{
6262
// GetGattServicesForUuidAsync is 15063+ only?
63-
varserviceResult=await_bleDevice.GetGattServicesForUuidAsync(aServiceGuid,BluetoothCacheMode.Cached);
63+
varservicedResult=await_bleDevice.GetGattServicesAsync();
6464

6565
// Don't log exceptions here, as we may not want to report them at some points.
66-
if(serviceResult.Status!=GattCommunicationStatus.Success)
66+
if(servicedResult.Status!=GattCommunicationStatus.Success)
6767
{
6868
thrownewButtplugDeviceException($"Cannot check for service {aServiceGuid} of {_bleDevice.Name}.");
6969
}
7070

71-
if(serviceResult.Services.Count==0)
71+
varservices=servicedResult.Services.Where(s =>s.Uuid==aServiceGuid).ToList();
72+
if(services.Count==0)
7273
{
7374
thrownewButtplugDeviceException($"Cannot find service {aServiceGuid} of {_bleDevice.Name}.");
7475
}
7576

7677
// TODO is there EVER a way we'd get more than one service back?
77-
returnserviceResult.Services[0];
78+
returnservices[0];
7879
}
7980

8081
protectedasyncTaskInitializeDevice(BluetoothLEProtocolConfigurationaConfig)
@@ -85,6 +86,7 @@ protected async Task InitializeDevice(BluetoothLEProtocolConfiguration aConfig)
8586
// characteristic detection.
8687
if(serviceInfo.Value==null||serviceInfo.Value.Count==0)
8788
{
89+
BpLogger.Debug($"Auto finding characteristics for {_bleDevice.Name}");
8890
awaitAddDefaultCharacteristics(serviceInfo.Key).ConfigureAwait(false);
8991
}
9092
else
@@ -97,11 +99,12 @@ protected async Task InitializeDevice(BluetoothLEProtocolConfiguration aConfig)
9799
{
98100
service=awaitGetService(serviceGuid).ConfigureAwait(false);
99101
}
100-
catch(ButtplugDeviceException)
102+
catch(ButtplugDeviceExceptionex)
101103
{
102104
// In this case, we may have a whole bunch of services that aren't valid for
103105
// a device and only one that is. We can ignore the exception here, and throw
104106
// later if we don't get anything from any service in the list.
107+
BpLogger.Error(ex.Message);
105108
continue;
106109
}
107110

Buttplug.Server.Managers.UWPBluetoothManager/UWPBluetoothManager.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher aObj,
123123
{
124124
return;
125125
}
126+
127+
_seenAddresses.Add(btAddr);
126128

127129
// todo Add advertGUIDs back in. Not sure that ever really gets used though.
128130
vardeviceCriteria=newBluetoothLEProtocolConfiguration(advertName);
@@ -134,10 +136,9 @@ private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher aObj,
134136
{
135137
BpLogger.Debug($"No usable device factory available for {advertName}.");
136138
// If we've got an actual name this time around, and we don't have any factories
137-
// available that match the info we have, add to our seen list so we won't keep
139+
// available that match the info we have, keep the device in our seen list so we won't keep
138140
// rechecking. If a device does have a factory, but doesn't connect, we still want to
139141
// try again.
140-
_seenAddresses.Add(btAddr);
141142
return;
142143
}
143144

@@ -148,6 +149,7 @@ private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher aObj,
148149
// remove it from seen devices, since the user may turn it back on during this scanning period.
149150
if(fromBluetoothAddressAsync==null)
150151
{
152+
_seenAddresses.Remove(btAddr);
151153
return;
152154
}
153155

@@ -176,8 +178,11 @@ private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher aObj,
176178
}
177179

178180
BpLogger.Error(
179-
$"Cannot connect to device {advertName}{btAddr}: {ex.Message}");
181+
$"Cannot connect to device {advertName}{btAddr}: {ex.Message}\n{ex.StackTrace}");
180182
}
183+
184+
// Remove the device from the seen list so that if we error'ed or disconnect, we can try again
185+
_seenAddresses.Remove(btAddr);
181186
}
182187

183188
privatevoidOnWatcherStopped(BluetoothLEAdvertisementWatcheraObj,

0 commit comments

Comments
 (0)
close