diff --git a/BluetoothHeartrateModule/BluetoothHeartrateModule.csproj b/BluetoothHeartrateModule/BluetoothHeartrateModule.csproj index ff2d4cc..3374fa6 100644 --- a/BluetoothHeartrateModule/BluetoothHeartrateModule.csproj +++ b/BluetoothHeartrateModule/BluetoothHeartrateModule.csproj @@ -4,8 +4,8 @@ net6.0-windows10.0.22621.0 enable enable - 1.3.1 - 1.3.1 + 1.3.2 + 1.3.2 DJDavid98 Bluetooth Heartrate logo\logo.ico diff --git a/BluetoothHeartrateModule/BluetoothHeartrateProvider.cs b/BluetoothHeartrateModule/BluetoothHeartrateProvider.cs index 1e40734..24881d4 100644 --- a/BluetoothHeartrateModule/BluetoothHeartrateProvider.cs +++ b/BluetoothHeartrateModule/BluetoothHeartrateProvider.cs @@ -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) { diff --git a/BluetoothHeartrateModule/DeviceNameResolver.cs b/BluetoothHeartrateModule/DeviceNameResolver.cs index 7f044e1..c956e55 100644 --- a/BluetoothHeartrateModule/DeviceNameResolver.cs +++ b/BluetoothHeartrateModule/DeviceNameResolver.cs @@ -8,34 +8,52 @@ namespace BluetoothHeartrateModule { internal class DeviceNameResolver { - internal static async Task 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 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); }