@@ -101,9 +101,6 @@ class G7BluetoothManager: NSObject {
101101 return activePeripheralManager? . peripheral
102102 }
103103 }
104-
105- /// Isolated to `managerQueue`
106- private var eventRegistrationActive : Bool = false
107104
108105 /// Isolated to `managerQueue`
109106 private var managedPeripherals : [ UUID : G7PeripheralManager ] = [ : ]
@@ -134,7 +131,7 @@ class G7BluetoothManager: NSObject {
134131 self . centralManager = CBCentralManager ( delegate: self , queue: managerQueue, options: [ CBCentralManagerOptionRestoreIdentifierKey: " com.loudnate.CGMBLEKit " ] )
135132 }
136133 }
137-
134+
138135 // MARK: - Actions
139136
140137 func scanForPeripheral( ) {
@@ -159,7 +156,7 @@ class G7BluetoothManager: NSObject {
159156
160157 private func managerQueue_stopScanning( ) {
161158 if centralManager. isScanning {
162- log. debug ( " Stopping scan " )
159+ log. default ( " Stopping scan " )
163160 centralManager. stopScan ( )
164161 delegate? . bluetoothManagerScanningStatusDidChange ( self )
165162 }
@@ -170,7 +167,7 @@ class G7BluetoothManager: NSObject {
170167
171168 managerQueue. sync {
172169 if centralManager. isScanning {
173- log. debug ( " Stopping scan on disconnect " )
170+ log. default ( " Stopping scan on disconnect " )
174171 centralManager. stopScan ( )
175172 delegate? . bluetoothManagerScanningStatusDidChange ( self )
176173 }
@@ -180,24 +177,17 @@ class G7BluetoothManager: NSObject {
180177 }
181178 }
182179 }
183-
180+
184181 func centralManager( _ central: CBCentralManager , connectionEventDidOccur event: CBConnectionEvent , for peripheral: CBPeripheral ) {
185-
186182 managerQueue. async {
187- guard self . eventRegistrationActive else {
188- self . centralManager. registerForConnectionEvents ( options: nil )
189- return
190- }
191-
192- self . managerQueue_establishActivePeripheral ( )
193-
194- if !self . eventRegistrationActive {
195- self . centralManager. registerForConnectionEvents ( options: nil )
183+ if self . activePeripheralIdentifier == nil {
184+ self . log. default ( " Discovered peripheral from connectionEventDidOccur %{public}@ " , peripheral. identifier. uuidString)
185+ self . handleDiscoveredPeripheral ( peripheral)
196186 }
197187 }
198188 }
199-
200- private func managerQueue_establishActivePeripheral ( ) {
189+
190+ private func managerQueue_scanForPeripheral ( ) {
201191 dispatchPrecondition ( condition: . onQueue( managerQueue) )
202192
203193 guard centralManager. state == . poweredOn else {
@@ -206,48 +196,36 @@ class G7BluetoothManager: NSObject {
206196
207197 let currentState = activePeripheral? . state ?? . disconnected
208198 guard currentState != . connected else {
209- eventRegistrationActive = false
210199 return
211200 }
212201
213202 if let peripheralID = activePeripheralIdentifier, let peripheral = centralManager. retrievePeripherals ( withIdentifiers: [ peripheralID] ) . first {
214- log. debug ( " Retrieved peripheral %{public}@ " , peripheral. identifier. uuidString)
203+ log. default ( " Retrieved peripheral %{public}@ " , peripheral. identifier. uuidString)
215204 handleDiscoveredPeripheral ( peripheral)
216205 } else {
217206 for peripheral in centralManager. retrieveConnectedPeripherals ( withServices: [
218207 SensorServiceUUID . advertisement. cbUUID,
219208 SensorServiceUUID . cgmService. cbUUID
220209 ] ) {
210+ log. default ( " Found system-connected peripheral: %{public}@ " , peripheral. identifier. uuidString)
221211 handleDiscoveredPeripheral ( peripheral)
222212 }
223213 }
224-
225- if activePeripheral != nil {
226- eventRegistrationActive = false
227- }
228- }
229-
230- private func managerQueue_scanForPeripheral( ) {
231- dispatchPrecondition ( condition: . onQueue( managerQueue) )
232-
233- managerQueue_establishActivePeripheral ( )
234214
235215 if activePeripheral == nil {
236- log. debug ( " Scanning for peripherals " )
216+ log. default ( " Scanning for peripherals and listening for connection events " )
217+
218+ centralManager. registerForConnectionEvents ( options: [ CBConnectionEventMatchingOption . serviceUUIDs: [
219+ SensorServiceUUID . advertisement. cbUUID,
220+ SensorServiceUUID . cgmService. cbUUID
221+ ] ] )
222+
237223 centralManager. scanForPeripherals ( withServices: [
238224 SensorServiceUUID . advertisement. cbUUID
239225 ] ,
240226 options: nil
241227 )
242228 delegate? . bluetoothManagerScanningStatusDidChange ( self )
243-
244- if !eventRegistrationActive {
245- eventRegistrationActive = true
246- centralManager. registerForConnectionEvents ( options: [ CBConnectionEventMatchingOption . serviceUUIDs: [
247- SensorServiceUUID . advertisement. cbUUID,
248- SensorServiceUUID . cgmService. cbUUID
249- ] ] )
250- }
251229 }
252230 }
253231
@@ -259,9 +237,9 @@ class G7BluetoothManager: NSObject {
259237 The sleep gives the transmitter time to shut down, but keeps the app running.
260238
261239 */
262- func scanAfterDelay( ) {
240+ fileprivate func scanAfterDelay( ) {
263241 DispatchQueue . global ( qos: . utility) . async {
264- Thread . sleep ( forTimeInterval: 5 )
242+ Thread . sleep ( forTimeInterval: 2 )
265243
266244 self . scanForPeripheral ( )
267245 }
@@ -295,7 +273,7 @@ class G7BluetoothManager: NSObject {
295273 if let delegate = delegate {
296274 switch delegate. bluetoothManager ( self , shouldConnectPeripheral: peripheral) {
297275 case . makeActive:
298- log. debug ( " Making peripheral active: %{public}@ " , peripheral. identifier. uuidString)
276+ log. default ( " Making peripheral active: %{public}@ " , peripheral. identifier. uuidString)
299277
300278 if let peripheralManager = activePeripheralManager {
301279 peripheralManager. peripheral = peripheral
@@ -311,7 +289,7 @@ class G7BluetoothManager: NSObject {
311289 self . centralManager. connect ( peripheral)
312290
313291 case . connect:
314- log. debug ( " Connecting to peripheral: %{public}@ " , peripheral. identifier. uuidString)
292+ log. default ( " Connecting to peripheral: %{public}@ " , peripheral. identifier. uuidString)
315293 self . centralManager. connect ( peripheral)
316294 let peripheralManager = G7PeripheralManager (
317295 peripheral: peripheral,
@@ -349,7 +327,7 @@ extension G7BluetoothManager: CBCentralManagerDelegate {
349327 fallthrough
350328 @unknown default :
351329 if central. isScanning {
352- log. debug ( " Stopping scan on central not powered on " )
330+ log. default ( " Stopping scan on central not powered on " )
353331 central. stopScan ( )
354332 delegate? . bluetoothManagerScanningStatusDidChange ( self )
355333 }
@@ -370,7 +348,7 @@ extension G7BluetoothManager: CBCentralManagerDelegate {
370348 func centralManager( _ central: CBCentralManager , didDiscover peripheral: CBPeripheral , advertisementData: [ String : Any ] , rssi RSSI: NSNumber ) {
371349 dispatchPrecondition ( condition: . onQueue( managerQueue) )
372350
373- log. info ( " %{public}@: %{public}@, data = %{public}@ " , #function, peripheral, String ( describing: advertisementData) )
351+ log. default ( " %{public}@: %{public}@, data = %{public}@ " , #function, peripheral, String ( describing: advertisementData) )
374352
375353 managerQueue. async {
376354 self . handleDiscoveredPeripheral ( peripheral)
0 commit comments