@@ -8,34 +8,52 @@ namespace BluetoothHeartrateModule
8
8
{
9
9
internal class DeviceNameResolver
10
10
{
11
- internal static async Task < string > GetDeviceNameAsync ( BluetoothLEAdvertisement advertisement , ulong bluetoothAddress )
11
+ BluetoothHeartrateModule module ;
12
+
13
+ public DeviceNameResolver ( BluetoothHeartrateModule module )
12
14
{
13
- var advertisementDeviceName = advertisement . LocalName ;
14
- if ( advertisementDeviceName != string . Empty )
15
- {
16
- return advertisementDeviceName ;
17
- }
15
+ this . module = module ;
16
+ }
18
17
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
24
22
{
25
- var services = await device . GetGattServicesForUuidAsync ( GattServiceUuids . GenericAccess ) ;
26
- if ( services . Services . Count > 0 )
23
+ var advertisementDeviceName = advertisement . LocalName ;
24
+ if ( advertisementDeviceName != string . Empty )
27
25
{
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 )
31
37
{
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
+ }
35
49
}
50
+ foreach ( var service in services . Services )
51
+ service . Dispose ( ) ;
36
52
}
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 } ") ;
39
57
}
40
58
return GetDeviceNameOrFallback ( deviceName ) ;
41
59
}
0 commit comments