@@ -25,6 +25,7 @@ public BluetoothHeartrateProvider(BluetoothHeartrateModule module)
25
25
26
26
public override void Initialise ( )
27
27
{
28
+ module . LogDebug ( "Initializing BluetoothHeartrateProvider" ) ;
28
29
if ( module . GetDeviceMacSetting ( ) == string . Empty )
29
30
{
30
31
Log ( "Device MAC setting is not set, module will log discovered devices" ) ;
@@ -36,7 +37,9 @@ public override void Initialise()
36
37
return ;
37
38
}
38
39
40
+ module . LogDebug ( "Registering watcher received handler" ) ;
39
41
module . watcher . Received += Watcher_Received ;
42
+ module . LogDebug ( "Starting watcher" ) ;
40
43
module . StartWatcher ( ) ;
41
44
}
42
45
@@ -49,45 +52,54 @@ public override async Task Teardown()
49
52
50
53
private void Reset ( )
51
54
{
52
-
55
+ module . LogDebug ( "Resetting provider" ) ;
53
56
if ( module . watcher != null )
54
57
{
58
+ module . LogDebug ( "Unregistering watcher received handler" ) ;
55
59
module . watcher . Received -= Watcher_Received ;
56
60
}
61
+ module . LogDebug ( "Clearing device names" ) ;
57
62
deviceNames . Clear ( ) ;
63
+ module . LogDebug ( "Clearing missing characteristics" ) ;
58
64
missingCharacteristicDevices . Clear ( ) ;
59
65
ResetDevice ( ) ;
60
66
processingData = false ;
61
67
}
62
68
63
69
private async void ResetDevice ( )
64
70
{
71
+ module . LogDebug ( "Resetting device data" ) ;
65
72
if ( heartRateService != null )
66
73
{
74
+ module . LogDebug ( "Resetting heartRateService" ) ;
67
75
try
68
76
{
69
77
heartRateService . Dispose ( ) ;
70
78
}
71
79
catch ( ObjectDisposedException )
72
80
{
73
81
// Ignore if object is already disposed
82
+ module . LogDebug ( "heartRateService already disposed" ) ;
74
83
}
75
84
heartRateService = null ;
76
85
}
77
86
if ( heartRateCharacteristic != null )
78
87
{
88
+ module . LogDebug ( "Resetting heartRateCharacteristic" ) ;
79
89
try
80
90
{
81
91
heartRateCharacteristic . ValueChanged -= HeartRateCharacteristic_ValueChanged ;
82
92
}
83
93
catch ( ObjectDisposedException )
84
94
{
85
95
// Ignore if object is already disposed
96
+ module . LogDebug ( "heartRateCharacteristic already disposed" ) ;
86
97
}
87
98
heartRateCharacteristic = null ;
88
99
}
89
100
if ( currentDevice != null )
90
101
{
102
+ module . LogDebug ( "Resetting currentDevice" ) ;
91
103
try
92
104
{
93
105
currentDevice . ConnectionStatusChanged -= Device_ConnectionStatusChanged ;
@@ -96,6 +108,7 @@ private async void ResetDevice()
96
108
catch ( ObjectDisposedException )
97
109
{
98
110
// Ignore if object is already disposed
111
+ module . LogDebug ( "currentDevice already disposed" ) ;
99
112
}
100
113
currentDevice = null ;
101
114
}
@@ -104,64 +117,87 @@ private async void ResetDevice()
104
117
105
118
private async void Watcher_Received ( BluetoothLEAdvertisementWatcher sender , BluetoothLEAdvertisementReceivedEventArgs args )
106
119
{
107
-
120
+ var advertisementId = Guid . NewGuid ( ) . ToString ( ) ;
121
+ // We need a prefix to follow the logs as advertisements can come in pretty rapidly
122
+ var logPrefix = $ "[{ advertisementId } ]";
123
+ module . LogDebug ( $ "{ logPrefix } Watcher received advertisement") ;
108
124
var advertisementMac = Converter . FormatAsMac ( args . BluetoothAddress ) ;
125
+ module . LogDebug ( $ "{ logPrefix } advertisementMac = { advertisementMac } ") ;
109
126
var deviceMacSetting = module . GetDeviceMacSetting ( ) ;
127
+ module . LogDebug ( $ "{ logPrefix } deviceMacSetting = { deviceMacSetting } ") ;
110
128
var isConfiguredDevice = advertisementMac == deviceMacSetting ;
129
+ module . LogDebug ( $ "{ logPrefix } isConfiguredDevice = { isConfiguredDevice } ") ;
111
130
if ( deviceMacSetting == string . Empty || isConfiguredDevice )
112
131
{
113
132
var deviceNamesValue = deviceNames . GetValueOrDefault ( advertisementMac , null ) ;
133
+ module . LogDebug ( $ "{ logPrefix } Cached device name: { deviceNamesValue } ") ;
114
134
if ( deviceNamesValue == null )
115
135
{
136
+ module . LogDebug ( $ "{ logPrefix } Creating device name resolver") ;
116
137
var dnr = new DeviceNameResolver ( module ) ;
117
- var advertisementDeviceName = await dnr . GetDeviceNameAsync ( args . Advertisement , args . BluetoothAddress ) ;
118
- deviceNames [ advertisementMac ] = advertisementDeviceName ;
138
+ module . LogDebug ( $ "{ logPrefix } Resolving device name") ;
139
+ var resolvedDeviceName = await dnr . GetDeviceNameAsync ( args . Advertisement , args . BluetoothAddress ) ;
140
+ module . LogDebug ( $ "{ logPrefix } Caching device name") ;
141
+ deviceNames [ advertisementMac ] = resolvedDeviceName ;
119
142
if ( ! isConfiguredDevice )
120
143
{
121
- Log ( $ "Discovered device: { advertisementDeviceName } (MAC: { advertisementMac } )") ;
144
+ Log ( $ "Discovered device: { resolvedDeviceName } (MAC: { advertisementMac } )") ;
122
145
}
123
146
}
124
- if ( ! isConfiguredDevice )
125
- {
126
- return ;
127
- }
128
147
}
129
148
130
149
if ( ! isConfiguredDevice )
131
150
{
132
151
// Not the droid we're looking for
152
+ module . LogDebug ( $ "{ logPrefix } Not the configured device, stop further advertisement processing") ;
133
153
return ;
134
154
}
135
155
if ( heartRateCharacteristic != null )
136
156
{
137
157
// Characteristic already found
158
+ module . LogDebug ( $ "{ logPrefix } heartRateCharacteristic already found, stop further advertisement processing") ;
138
159
return ;
139
160
}
140
161
141
- if ( processingData ) return ;
162
+ if ( processingData )
163
+ {
164
+ module . LogDebug ( $ "{ logPrefix } Currently another advertisement is being processed, ignore this advertisement") ;
165
+ return ;
166
+ }
142
167
processingData = true ;
168
+ module . LogDebug ( $ "{ logPrefix } Begin processing advertisement data") ;
143
169
try
144
170
{
145
171
if ( currentDevice == null )
146
172
{
173
+ module . LogDebug ( $ "{ logPrefix } Setting currrent device") ;
147
174
currentDevice = await BluetoothLEDevice . FromBluetoothAddressAsync ( args . BluetoothAddress ) ;
148
175
var currentDeviceName = deviceNames [ advertisementMac ] ?? "Unknown" ;
149
176
Log ( $ "Found device named { currentDeviceName } for MAC { advertisementMac } ") ;
150
177
module . SetDeviceName ( currentDeviceName ) ;
178
+ module . LogDebug ( $ "{ logPrefix } Register connection status change handler") ;
151
179
currentDevice . ConnectionStatusChanged += Device_ConnectionStatusChanged ;
152
180
}
181
+ else
182
+ {
183
+ module . LogDebug ( $ "{ logPrefix } Current device already set") ;
184
+ }
153
185
154
186
var missungUnknown = ! missingCharacteristicDevices . Contains ( deviceMacSetting ) ;
187
+ module . LogDebug ( $ "{ logPrefix } missungUnknown = { missungUnknown } ") ;
188
+ module . LogDebug ( $ "{ logPrefix } Finding HeratRate service") ;
155
189
var services = await currentDevice . GetGattServicesForUuidAsync ( GattServiceUuids . HeartRate , BluetoothCacheMode . Uncached ) ;
156
190
if ( services . Services . Count > 0 )
157
191
{
192
+ module . LogDebug ( $ "{ logPrefix } Queueuing all found services for cleanup") ;
158
193
IEnumerable < GattDeviceService > cleanupServices = services . Services ;
159
194
var firstService = cleanupServices . First ( ) ;
160
195
if ( missungUnknown )
161
196
{
162
197
Log ( "Found heartrate service" ) ;
163
198
}
164
199
var characteristics = await firstService . GetCharacteristicsForUuidAsync ( GattCharacteristicUuids . HeartRateMeasurement , BluetoothCacheMode . Uncached ) ;
200
+ module . LogDebug ( $ "{ logPrefix } Finding HeartRateMeasurement characteristic") ;
165
201
if ( characteristics . Characteristics . Count > 0 )
166
202
{
167
203
if ( heartRateCharacteristic == null )
@@ -181,11 +217,15 @@ private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, Blue
181
217
// Remove receive handler
182
218
if ( module . watcher != null )
183
219
{
220
+ module . LogDebug ( $ "{ logPrefix } Unregistering watcher recived handler") ;
184
221
module . watcher . Received -= Watcher_Received ;
185
222
}
223
+ module . LogDebug ( $ "{ logPrefix } Invoking OnConnected action") ;
186
224
OnConnected ? . Invoke ( ) ;
187
225
Log ( "Connection successful" ) ;
226
+ module . LogDebug ( $ "{ logPrefix } Stopping watcher") ;
188
227
module . StopWatcher ( ) ;
228
+ module . LogDebug ( $ "{ logPrefix } Excluding first service from cleanup") ;
189
229
cleanupServices = services . Services . Skip ( 1 ) ;
190
230
}
191
231
else
@@ -194,16 +234,26 @@ private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, Blue
194
234
}
195
235
}
196
236
}
237
+ else
238
+ {
239
+ module . LogDebug ( $ "{ logPrefix } No characteristics found") ;
240
+ }
197
241
198
242
if ( cleanupServices . Any ( ) )
199
243
{
244
+ module . LogDebug ( $ "{ logPrefix } Cleaning up services queued for cleanup") ;
200
245
foreach ( var service in cleanupServices )
201
246
service . Dispose ( ) ;
202
247
}
203
248
}
249
+ else
250
+ {
251
+ module . LogDebug ( $ "{ logPrefix } No services found") ;
252
+ }
204
253
205
254
if ( heartRateCharacteristic == null && missungUnknown )
206
255
{
256
+ module . LogDebug ( $ "{ logPrefix } Adding device to missing characteristic list") ;
207
257
missingCharacteristicDevices . Add ( deviceMacSetting ) ;
208
258
throw new Exception ( "No heartrate characteristic found" ) ;
209
259
}
@@ -216,22 +266,30 @@ private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, Blue
216
266
finally
217
267
{
218
268
processingData = false ;
269
+ module . LogDebug ( $ "{ logPrefix } Stopped processing advertisement") ;
219
270
}
220
271
}
221
272
222
273
private void HeartRateCharacteristic_ValueChanged ( GattCharacteristic sender , GattValueChangedEventArgs args )
223
274
{
275
+ module . LogDebug ( "HeartRateCharacteristic_ValueChanged" ) ;
224
276
var data = new byte [ args . CharacteristicValue . Length ] ;
277
+ module . LogDebug ( "Reading new hartrate value into buffer" ) ;
225
278
DataReader . FromBuffer ( args . CharacteristicValue ) . ReadBytes ( data ) ;
226
279
227
- OnHeartrateUpdate ? . Invoke ( data [ 1 ] ) ;
280
+ var updateData = data [ 1 ] ;
281
+ module . LogDebug ( $ "Invoking OnHeartrateUpdate action with data { updateData } ") ;
282
+ OnHeartrateUpdate ? . Invoke ( updateData ) ;
228
283
}
229
284
230
285
private async void Device_ConnectionStatusChanged ( BluetoothLEDevice sender , object args )
231
286
{
287
+ module . LogDebug ( "Device_ConnectionStatusChanged" ) ;
288
+ module . LogDebug ( $ "sender.ConnectionStatus = { sender . ConnectionStatus } ") ;
232
289
if ( sender . ConnectionStatus == BluetoothConnectionStatus . Disconnected )
233
290
{
234
291
Log ( "Current device disconected" ) ;
292
+ module . LogDebug ( $ "Invoking OnDisconnected action") ;
235
293
OnDisconnected ? . Invoke ( ) ;
236
294
}
237
295
}
0 commit comments