Skip to content

Commit

Permalink
Attempt to fix crash when device name cannot be read
Browse files Browse the repository at this point in the history
  • Loading branch information
DJDavid98 committed Aug 13, 2023
1 parent ac8ecdd commit b15b3ee
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
4 changes: 2 additions & 2 deletions BluetoothHeartrateModule/BluetoothHeartrateModule.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFramework>net6.0-windows10.0.22621.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>1.3.1</AssemblyVersion>
<FileVersion>1.3.1</FileVersion>
<AssemblyVersion>1.3.2</AssemblyVersion>
<FileVersion>1.3.2</FileVersion>
<Authors>DJDavid98</Authors>
<Product>Bluetooth Heartrate</Product>
<ApplicationIcon>logo\logo.ico</ApplicationIcon>
Expand Down
3 changes: 2 additions & 1 deletion BluetoothHeartrateModule/BluetoothHeartrateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, Blue
var deviceNamesValue = deviceNames.GetValueOrDefault(advertisementMac, null);
if (deviceNamesValue == null)
{
var advertisementDeviceName = await DeviceNameResolver.GetDeviceNameAsync(args.Advertisement, args.BluetoothAddress);
var dnr = new DeviceNameResolver(module);
var advertisementDeviceName = await dnr.GetDeviceNameAsync(args.Advertisement, args.BluetoothAddress);
deviceNames[advertisementMac] = advertisementDeviceName;
if (!isConfiguredDevice)
{
Expand Down
60 changes: 39 additions & 21 deletions BluetoothHeartrateModule/DeviceNameResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,52 @@ namespace BluetoothHeartrateModule
{
internal class DeviceNameResolver
{
internal static async Task<string> GetDeviceNameAsync(BluetoothLEAdvertisement advertisement, ulong bluetoothAddress)
BluetoothHeartrateModule module;

public DeviceNameResolver(BluetoothHeartrateModule module)
{
var advertisementDeviceName = advertisement.LocalName;
if (advertisementDeviceName != string.Empty)
{
return advertisementDeviceName;
}
this.module = module;
}

using var device = await BluetoothLEDevice.FromBluetoothAddressAsync(bluetoothAddress);
// Get the device name using the DeviceInformation class
DeviceInformation deviceInfo = await DeviceInformation.CreateFromIdAsync(device.DeviceId);
var deviceName = deviceInfo.Name;
if (deviceName == string.Empty)
internal async Task<string> GetDeviceNameAsync(BluetoothLEAdvertisement advertisement, ulong bluetoothAddress)
{
var deviceName = string.Empty;
try
{
var services = await device.GetGattServicesForUuidAsync(GattServiceUuids.GenericAccess);
if (services.Services.Count > 0)
var advertisementDeviceName = advertisement.LocalName;
if (advertisementDeviceName != string.Empty)
{
var service = services.Services[0];
var characteristics = await service.GetCharacteristicsForUuidAsync(GattCharacteristicUuids.GapDeviceName);
if (characteristics.Characteristics.Count > 0)
return advertisementDeviceName;
}

using var device = await BluetoothLEDevice.FromBluetoothAddressAsync(bluetoothAddress);
// Get the device name using the DeviceInformation class
DeviceInformation deviceInfo = await DeviceInformation.CreateFromIdAsync(device.DeviceId);
deviceName = deviceInfo.Name;
if (deviceName == string.Empty)
{
var services = await device.GetGattServicesForUuidAsync(GattServiceUuids.GenericAccess);
if (services.Services.Count > 0)
{
var characteristic = characteristics.Characteristics[0];
var value = await characteristic.ReadValueAsync(BluetoothCacheMode.Uncached);
deviceName = DataReader.FromBuffer(value.Value).ReadString(value.Value.Length);
var service = services.Services[0];
var characteristics = await service.GetCharacteristicsForUuidAsync(GattCharacteristicUuids.GapDeviceName);
if (characteristics.Characteristics.Count > 0)
{
var characteristic = characteristics.Characteristics[0];
var value = await characteristic.ReadValueAsync(BluetoothCacheMode.Uncached);
if (value != null)
{
deviceName = DataReader.FromBuffer(value.Value).ReadString(value.Value.Length);
}
}
}
foreach (var service in services.Services)
service.Dispose();
}
foreach (var service in services.Services)
service.Dispose();
}
catch (Exception ex)
{
module.Log($"Could not get device name for address {Converter.FormatAsMac(bluetoothAddress)}: {ex.Message}\n{ex.StackTrace}");
}
return GetDeviceNameOrFallback(deviceName);
}
Expand Down

0 comments on commit b15b3ee

Please sign in to comment.