|
1 |
| -diff --git a/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift b/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift |
2 |
| -index 62ab5fc..8976104 100644 |
3 |
| ---- a/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift |
4 |
| -+++ b/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift |
5 |
| -@@ -101,6 +101,9 @@ class G7BluetoothManager: NSObject { |
6 |
| - return activePeripheralManager?.peripheral |
7 |
| - } |
8 |
| - } |
9 |
| -+ |
10 |
| -+ /// Isolated to `managerQueue` |
11 |
| -+ private var eventRegistrationActive : Bool = false |
12 |
| - |
13 |
| - /// Isolated to `managerQueue` |
14 |
| - private var managedPeripherals: [UUID:G7PeripheralManager] = [:] |
15 |
| -@@ -131,7 +134,7 @@ class G7BluetoothManager: NSObject { |
16 |
| - self.centralManager = CBCentralManager(delegate: self, queue: managerQueue, options: [CBCentralManagerOptionRestoreIdentifierKey: "com.loudnate.CGMBLEKit"]) |
17 |
| - } |
18 |
| - } |
19 |
| -- |
20 |
| -+ |
21 |
| - // MARK: - Actions |
22 |
| - |
23 |
| - func scanForPeripheral() { |
24 |
| -@@ -177,8 +180,24 @@ class G7BluetoothManager: NSObject { |
25 |
| - } |
26 |
| - } |
27 |
| - } |
28 |
| -- |
29 |
| -- private func managerQueue_scanForPeripheral() { |
30 |
| -+ |
31 |
| -+ func centralManager(_ central: CBCentralManager, connectionEventDidOccur event: CBConnectionEvent, for peripheral: CBPeripheral) { |
32 |
| -+ |
33 |
| -+ managerQueue.async { |
34 |
| -+ guard self.eventRegistrationActive else { |
35 |
| -+ self.centralManager.registerForConnectionEvents(options: nil) |
36 |
| -+ return |
37 |
| -+ } |
38 |
| -+ |
39 |
| -+ self.managerQueue_establishActivePeripheral() |
40 |
| -+ |
41 |
| -+ if !self.eventRegistrationActive { |
42 |
| -+ self.centralManager.registerForConnectionEvents(options: nil) |
43 |
| -+ } |
44 |
| -+ } |
45 |
| -+ } |
46 |
| -+ |
47 |
| -+ private func managerQueue_establishActivePeripheral() { |
48 |
| - dispatchPrecondition(condition: .onQueue(managerQueue)) |
49 |
| - |
50 |
| - guard centralManager.state == .poweredOn else { |
51 |
| -@@ -187,6 +206,7 @@ class G7BluetoothManager: NSObject { |
52 |
| - |
53 |
| - let currentState = activePeripheral?.state ?? .disconnected |
54 |
| - guard currentState != .connected else { |
55 |
| -+ eventRegistrationActive = false |
56 |
| - return |
57 |
| - } |
58 |
| - |
59 |
| -@@ -201,6 +221,16 @@ class G7BluetoothManager: NSObject { |
60 |
| - handleDiscoveredPeripheral(peripheral) |
61 |
| - } |
62 |
| - } |
63 |
| -+ |
64 |
| -+ if activePeripheral != nil { |
65 |
| -+ eventRegistrationActive = false |
66 |
| -+ } |
67 |
| -+ } |
68 |
| -+ |
69 |
| -+ private func managerQueue_scanForPeripheral() { |
70 |
| -+ dispatchPrecondition(condition: .onQueue(managerQueue)) |
71 |
| -+ |
72 |
| -+ managerQueue_establishActivePeripheral() |
73 |
| - |
74 |
| - if activePeripheral == nil { |
75 |
| - log.debug("Scanning for peripherals") |
76 |
| -@@ -210,6 +240,14 @@ class G7BluetoothManager: NSObject { |
77 |
| - options: nil |
78 |
| - ) |
79 |
| - delegate?.bluetoothManagerScanningStatusDidChange(self) |
80 |
| -+ |
81 |
| -+ if !eventRegistrationActive { |
82 |
| -+ eventRegistrationActive = true |
83 |
| -+ centralManager.registerForConnectionEvents(options: [CBConnectionEventMatchingOption.serviceUUIDs: [ |
84 |
| -+ SensorServiceUUID.advertisement.cbUUID, |
85 |
| -+ SensorServiceUUID.cgmService.cbUUID |
86 |
| -+ ]]) |
87 |
| -+ } |
88 |
| - } |
89 |
| - } |
90 |
| - |
91 |
| -@@ -221,9 +259,9 @@ class G7BluetoothManager: NSObject { |
92 |
| - The sleep gives the transmitter time to shut down, but keeps the app running. |
93 |
| - |
94 |
| - */ |
95 |
| -- fileprivate func scanAfterDelay() { |
96 |
| -+ func scanAfterDelay() { |
97 |
| - DispatchQueue.global(qos: .utility).async { |
98 |
| -- Thread.sleep(forTimeInterval: 2) |
99 |
| -+ Thread.sleep(forTimeInterval: 5) |
100 |
| - |
101 |
| - self.scanForPeripheral() |
102 |
| - } |
103 |
| -diff --git a/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift b/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift |
104 |
| -index 198d5b3..e2861e6 100644 |
105 |
| ---- a/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift |
106 |
| -+++ b/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift |
107 |
| -@@ -237,14 +237,14 @@ public class G7CGMManager: CGMManager { |
108 |
| - return nil |
109 |
| - } |
110 |
| - |
111 |
| -- public func scanForNewSensor() { |
112 |
| -+ public func scanForNewSensor(scanAfterDelay: Bool = false) { |
113 |
| - logDeviceCommunication("Forgetting existing sensor and starting scan for new sensor.", type: .connection) |
114 |
| - |
115 |
| - mutateState { state in |
116 |
| - state.sensorID = nil |
117 |
| - state.activatedAt = nil |
118 |
| - } |
119 |
| -- sensor.scanForNewSensor() |
120 |
| -+ sensor.scanForNewSensor(scanAfterDelay: scanAfterDelay) |
121 |
| - } |
122 |
| - |
123 |
| - private var device: HKDevice? { |
124 |
| -@@ -319,7 +319,7 @@ extension G7CGMManager: G7SensorDelegate { |
125 |
| - public func sensorDisconnected(_ sensor: G7Sensor, suspectedEndOfSession: Bool) { |
126 |
| - logDeviceCommunication("Sensor disconnected: suspectedEndOfSession=\(suspectedEndOfSession)", type: .connection) |
127 |
| - if suspectedEndOfSession { |
128 |
| -- scanForNewSensor() |
129 |
| -+ scanForNewSensor(scanAfterDelay: true) |
130 |
| - } |
131 |
| - } |
132 |
| - |
133 | 1 | diff --git a/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift b/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
|
134 |
| -index b1745a1..5c51092 100644 |
| 2 | +index b1745a1..93d75d1 100644 |
135 | 3 | --- a/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
|
136 | 4 | +++ b/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
|
137 |
| -@@ -99,11 +99,15 @@ public final class G7Sensor: G7BluetoothManagerDelegate { |
138 |
| - bluetoothManager.delegate = self |
139 |
| - } |
| 5 | +@@ -194,7 +194,10 @@ public final class G7Sensor: G7BluetoothManagerDelegate { |
| 6 | + if let sensorID = sensorID, sensorID == peripheralManager.peripheral.name { |
| 7 | + |
| 8 | + let suspectedEndOfSession: Bool |
| 9 | +- if pendingAuth && wasRemoteDisconnect { |
| 10 | ++ |
| 11 | ++ if let activationDate = activationDate, Date() > activationDate.addingTimeInterval(G7Sensor.lifetime + G7Sensor.gracePeriod), pendingAuth, wasRemoteDisconnect |
| 12 | ++ { |
| 13 | ++ self.log.info("Sensor disconnected at %{public}@", activationDate.description) |
| 14 | + suspectedEndOfSession = true // Normal disconnect without auth is likely that G7 app stopped this session |
| 15 | + } else { |
| 16 | + suspectedEndOfSession = false |
| 17 | +@@ -233,7 +236,7 @@ public final class G7Sensor: G7BluetoothManagerDelegate { |
| 18 | + |
| 19 | + guard response.count > 0 else { return } |
140 | 20 |
|
141 |
| -- public func scanForNewSensor() { |
142 |
| -+ public func scanForNewSensor(scanAfterDelay: Bool = false) { |
143 |
| - self.sensorID = nil |
144 |
| - bluetoothManager.disconnect() |
145 |
| - bluetoothManager.forgetPeripheral() |
146 |
| -- bluetoothManager.scanForPeripheral() |
147 |
| -+ if scanAfterDelay { |
148 |
| -+ bluetoothManager.scanAfterDelay() |
149 |
| -+ } else { |
150 |
| -+ bluetoothManager.scanForPeripheral() |
151 |
| -+ } |
152 |
| - } |
| 21 | +- log.debug("Received control response: %{public}@", response.hexadecimalString) |
| 22 | ++ log.default("Received control response: %{public}@", response.hexadecimalString) |
153 | 23 |
|
154 |
| - public func resumeScanning() { |
| 24 | + switch G7Opcode(rawValue: response[0]) { |
| 25 | + case .glucoseTx?: |
0 commit comments