@@ -149,9 +149,9 @@ public final class Transmitter: BluetoothManagerDelegate {
149
149
150
150
peripheralManager. perform { ( peripheral) in
151
151
if self . passiveModeEnabled {
152
- self . log. debug ( " Listening for control commands in passive mode " )
152
+ self . log. debug ( " Listening for authentication responses in passive mode " )
153
153
do {
154
- try peripheral. listenToControl ( )
154
+ try peripheral. listenToCharacteristic ( . authentication )
155
155
} catch let error {
156
156
self . delegateQueue. async {
157
157
self . delegate? . transmitter ( self , didError: error)
@@ -162,14 +162,14 @@ public final class Transmitter: BluetoothManagerDelegate {
162
162
self . log. debug ( " Authenticating with transmitter " )
163
163
let status = try peripheral. authenticate ( id: self . id)
164
164
165
- if status. bonded != 0x1 {
165
+ if ! status. isBonded {
166
166
self . log. debug ( " Requesting bond " )
167
167
try peripheral. requestBond ( )
168
168
169
169
self . log. debug ( " Bonding request sent. Waiting user to respond. " )
170
170
}
171
171
172
- try peripheral. enableNotify ( shouldWaitForBond: status. bonded != 0x1 )
172
+ try peripheral. enableNotify ( shouldWaitForBond: ! status. isBonded )
173
173
defer {
174
174
self . log. debug ( " Initiating a disconnect " )
175
175
peripheral. disconnect ( )
@@ -319,6 +319,28 @@ public final class Transmitter: BluetoothManagerDelegate {
319
319
320
320
self . backfillBuffer? . append ( response)
321
321
}
322
+
323
+ func bluetoothManager( _ manager: BluetoothManager , peripheralManager: PeripheralManager , didReceiveAuthenticationResponse response: Data ) {
324
+
325
+ if let message = AuthChallengeRxMessage ( data: response) , message. isBonded, message. isAuthenticated {
326
+ self . log. debug ( " Observed authenticated session. enabling notifications for control characteristic. " )
327
+ peripheralManager. perform { ( peripheral) in
328
+ do {
329
+ try peripheral. listenToCharacteristic ( . control)
330
+ try peripheral. listenToCharacteristic ( . backfill)
331
+ } catch let error {
332
+ self . log. error ( " Error trying to enable notifications on control characteristic: %{public}@ " , String ( describing: error) )
333
+ }
334
+ do {
335
+ try peripheral. stopListeningToCharacteristic ( . authentication)
336
+ } catch let error {
337
+ self . log. error ( " Error trying to disable notifications on authentication characteristic: %{public}@ " , String ( describing: error) )
338
+ }
339
+ }
340
+ } else {
341
+ self . log. debug ( " Ignoring authentication response: %{public}@ " , response. hexadecimalString)
342
+ }
343
+ }
322
344
}
323
345
324
346
@@ -390,7 +412,7 @@ fileprivate extension PeripheralManager {
390
412
throw TransmitterError . authenticationError ( " Unable to parse auth status: \( error) " )
391
413
}
392
414
393
- guard challengeResponse. authenticated == 1 else {
415
+ guard challengeResponse. isAuthenticated else {
394
416
throw TransmitterError . authenticationError ( " Transmitter rejected auth challenge " )
395
417
}
396
418
@@ -478,12 +500,19 @@ fileprivate extension PeripheralManager {
478
500
}
479
501
}
480
502
481
- fileprivate func listenToControl ( ) throws {
503
+ func listenToCharacteristic ( _ characteristic : CGMServiceCharacteristicUUID ) throws {
482
504
do {
483
- try setNotifyValue ( true , for: . control)
484
- try setNotifyValue ( true , for: . backfill)
505
+ try setNotifyValue ( true , for: characteristic)
485
506
} catch let error {
486
- throw TransmitterError . controlError ( " Error enabling notification: \( error) " )
507
+ throw TransmitterError . controlError ( " Error enabling notification for \( characteristic) : \( error) " )
508
+ }
509
+ }
510
+
511
+ func stopListeningToCharacteristic( _ characteristic: CGMServiceCharacteristicUUID ) throws {
512
+ do {
513
+ try setNotifyValue ( false , for: characteristic)
514
+ } catch let error {
515
+ throw TransmitterError . controlError ( " Error disabling notification for \( characteristic) : \( error) " )
487
516
}
488
517
}
489
518
}
0 commit comments