@@ -149,9 +149,9 @@ public final class Transmitter: BluetoothManagerDelegate {
149149
150150 peripheralManager. perform { ( peripheral) in
151151 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 " )
153153 do {
154- try peripheral. listenToControl ( )
154+ try peripheral. listenToCharacteristic ( . authentication )
155155 } catch let error {
156156 self . delegateQueue. async {
157157 self . delegate? . transmitter ( self , didError: error)
@@ -162,14 +162,14 @@ public final class Transmitter: BluetoothManagerDelegate {
162162 self . log. debug ( " Authenticating with transmitter " )
163163 let status = try peripheral. authenticate ( id: self . id)
164164
165- if status. bonded != 0x1 {
165+ if ! status. isBonded {
166166 self . log. debug ( " Requesting bond " )
167167 try peripheral. requestBond ( )
168168
169169 self . log. debug ( " Bonding request sent. Waiting user to respond. " )
170170 }
171171
172- try peripheral. enableNotify ( shouldWaitForBond: status. bonded != 0x1 )
172+ try peripheral. enableNotify ( shouldWaitForBond: ! status. isBonded )
173173 defer {
174174 self . log. debug ( " Initiating a disconnect " )
175175 peripheral. disconnect ( )
@@ -319,6 +319,28 @@ public final class Transmitter: BluetoothManagerDelegate {
319319
320320 self . backfillBuffer? . append ( response)
321321 }
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+ }
322344}
323345
324346
@@ -390,7 +412,7 @@ fileprivate extension PeripheralManager {
390412 throw TransmitterError . authenticationError ( " Unable to parse auth status: \( error) " )
391413 }
392414
393- guard challengeResponse. authenticated == 1 else {
415+ guard challengeResponse. isAuthenticated else {
394416 throw TransmitterError . authenticationError ( " Transmitter rejected auth challenge " )
395417 }
396418
@@ -478,12 +500,19 @@ fileprivate extension PeripheralManager {
478500 }
479501 }
480502
481- fileprivate func listenToControl ( ) throws {
503+ func listenToCharacteristic ( _ characteristic : CGMServiceCharacteristicUUID ) throws {
482504 do {
483- try setNotifyValue ( true , for: . control)
484- try setNotifyValue ( true , for: . backfill)
505+ try setNotifyValue ( true , for: characteristic)
485506 } 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) " )
487516 }
488517 }
489518}
0 commit comments