Skip to content

Commit b15b3ee

Browse files
committed
Attempt to fix crash when device name cannot be read
1 parent ac8ecdd commit b15b3ee

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

BluetoothHeartrateModule/BluetoothHeartrateModule.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<TargetFramework>net6.0-windows10.0.22621.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<AssemblyVersion>1.3.1</AssemblyVersion>
8-
<FileVersion>1.3.1</FileVersion>
7+
<AssemblyVersion>1.3.2</AssemblyVersion>
8+
<FileVersion>1.3.2</FileVersion>
99
<Authors>DJDavid98</Authors>
1010
<Product>Bluetooth Heartrate</Product>
1111
<ApplicationIcon>logo\logo.ico</ApplicationIcon>

BluetoothHeartrateModule/BluetoothHeartrateProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, Blue
9393
var deviceNamesValue = deviceNames.GetValueOrDefault(advertisementMac, null);
9494
if (deviceNamesValue == null)
9595
{
96-
var advertisementDeviceName = await DeviceNameResolver.GetDeviceNameAsync(args.Advertisement, args.BluetoothAddress);
96+
var dnr = new DeviceNameResolver(module);
97+
var advertisementDeviceName = await dnr.GetDeviceNameAsync(args.Advertisement, args.BluetoothAddress);
9798
deviceNames[advertisementMac] = advertisementDeviceName;
9899
if (!isConfiguredDevice)
99100
{

BluetoothHeartrateModule/DeviceNameResolver.cs

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,52 @@ namespace BluetoothHeartrateModule
88
{
99
internal class DeviceNameResolver
1010
{
11-
internal static async Task<string> GetDeviceNameAsync(BluetoothLEAdvertisement advertisement, ulong bluetoothAddress)
11+
BluetoothHeartrateModule module;
12+
13+
public DeviceNameResolver(BluetoothHeartrateModule module)
1214
{
13-
var advertisementDeviceName = advertisement.LocalName;
14-
if (advertisementDeviceName != string.Empty)
15-
{
16-
return advertisementDeviceName;
17-
}
15+
this.module = module;
16+
}
1817

19-
using var device = await BluetoothLEDevice.FromBluetoothAddressAsync(bluetoothAddress);
20-
// Get the device name using the DeviceInformation class
21-
DeviceInformation deviceInfo = await DeviceInformation.CreateFromIdAsync(device.DeviceId);
22-
var deviceName = deviceInfo.Name;
23-
if (deviceName == string.Empty)
18+
internal async Task<string> GetDeviceNameAsync(BluetoothLEAdvertisement advertisement, ulong bluetoothAddress)
19+
{
20+
var deviceName = string.Empty;
21+
try
2422
{
25-
var services = await device.GetGattServicesForUuidAsync(GattServiceUuids.GenericAccess);
26-
if (services.Services.Count > 0)
23+
var advertisementDeviceName = advertisement.LocalName;
24+
if (advertisementDeviceName != string.Empty)
2725
{
28-
var service = services.Services[0];
29-
var characteristics = await service.GetCharacteristicsForUuidAsync(GattCharacteristicUuids.GapDeviceName);
30-
if (characteristics.Characteristics.Count > 0)
26+
return advertisementDeviceName;
27+
}
28+
29+
using var device = await BluetoothLEDevice.FromBluetoothAddressAsync(bluetoothAddress);
30+
// Get the device name using the DeviceInformation class
31+
DeviceInformation deviceInfo = await DeviceInformation.CreateFromIdAsync(device.DeviceId);
32+
deviceName = deviceInfo.Name;
33+
if (deviceName == string.Empty)
34+
{
35+
var services = await device.GetGattServicesForUuidAsync(GattServiceUuids.GenericAccess);
36+
if (services.Services.Count > 0)
3137
{
32-
var characteristic = characteristics.Characteristics[0];
33-
var value = await characteristic.ReadValueAsync(BluetoothCacheMode.Uncached);
34-
deviceName = DataReader.FromBuffer(value.Value).ReadString(value.Value.Length);
38+
var service = services.Services[0];
39+
var characteristics = await service.GetCharacteristicsForUuidAsync(GattCharacteristicUuids.GapDeviceName);
40+
if (characteristics.Characteristics.Count > 0)
41+
{
42+
var characteristic = characteristics.Characteristics[0];
43+
var value = await characteristic.ReadValueAsync(BluetoothCacheMode.Uncached);
44+
if (value != null)
45+
{
46+
deviceName = DataReader.FromBuffer(value.Value).ReadString(value.Value.Length);
47+
}
48+
}
3549
}
50+
foreach (var service in services.Services)
51+
service.Dispose();
3652
}
37-
foreach (var service in services.Services)
38-
service.Dispose();
53+
}
54+
catch (Exception ex)
55+
{
56+
module.Log($"Could not get device name for address {Converter.FormatAsMac(bluetoothAddress)}: {ex.Message}\n{ex.StackTrace}");
3957
}
4058
return GetDeviceNameOrFallback(deviceName);
4159
}

0 commit comments

Comments
 (0)