Skip to content

Update to use G7SensorKit PR 34 instead of PR 33 for G7 issues #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions g7_scan/archive/g7_scan.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
diff --git a/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift b/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift
index 62ab5fc..8976104 100644
--- a/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift
+++ b/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift
@@ -101,6 +101,9 @@ class G7BluetoothManager: NSObject {
return activePeripheralManager?.peripheral
}
}
+
+ /// Isolated to `managerQueue`
+ private var eventRegistrationActive : Bool = false

/// Isolated to `managerQueue`
private var managedPeripherals: [UUID:G7PeripheralManager] = [:]
@@ -131,7 +134,7 @@ class G7BluetoothManager: NSObject {
self.centralManager = CBCentralManager(delegate: self, queue: managerQueue, options: [CBCentralManagerOptionRestoreIdentifierKey: "com.loudnate.CGMBLEKit"])
}
}
-
+
// MARK: - Actions

func scanForPeripheral() {
@@ -177,8 +180,24 @@ class G7BluetoothManager: NSObject {
}
}
}
-
- private func managerQueue_scanForPeripheral() {
+
+ func centralManager(_ central: CBCentralManager, connectionEventDidOccur event: CBConnectionEvent, for peripheral: CBPeripheral) {
+
+ managerQueue.async {
+ guard self.eventRegistrationActive else {
+ self.centralManager.registerForConnectionEvents(options: nil)
+ return
+ }
+
+ self.managerQueue_establishActivePeripheral()
+
+ if !self.eventRegistrationActive {
+ self.centralManager.registerForConnectionEvents(options: nil)
+ }
+ }
+ }
+
+ private func managerQueue_establishActivePeripheral() {
dispatchPrecondition(condition: .onQueue(managerQueue))

guard centralManager.state == .poweredOn else {
@@ -187,6 +206,7 @@ class G7BluetoothManager: NSObject {

let currentState = activePeripheral?.state ?? .disconnected
guard currentState != .connected else {
+ eventRegistrationActive = false
return
}

@@ -201,6 +221,16 @@ class G7BluetoothManager: NSObject {
handleDiscoveredPeripheral(peripheral)
}
}
+
+ if activePeripheral != nil {
+ eventRegistrationActive = false
+ }
+ }
+
+ private func managerQueue_scanForPeripheral() {
+ dispatchPrecondition(condition: .onQueue(managerQueue))
+
+ managerQueue_establishActivePeripheral()

if activePeripheral == nil {
log.debug("Scanning for peripherals")
@@ -210,6 +240,14 @@ class G7BluetoothManager: NSObject {
options: nil
)
delegate?.bluetoothManagerScanningStatusDidChange(self)
+
+ if !eventRegistrationActive {
+ eventRegistrationActive = true
+ centralManager.registerForConnectionEvents(options: [CBConnectionEventMatchingOption.serviceUUIDs: [
+ SensorServiceUUID.advertisement.cbUUID,
+ SensorServiceUUID.cgmService.cbUUID
+ ]])
+ }
}
}

@@ -221,9 +259,9 @@ class G7BluetoothManager: NSObject {
The sleep gives the transmitter time to shut down, but keeps the app running.

*/
- fileprivate func scanAfterDelay() {
+ func scanAfterDelay() {
DispatchQueue.global(qos: .utility).async {
- Thread.sleep(forTimeInterval: 2)
+ Thread.sleep(forTimeInterval: 5)

self.scanForPeripheral()
}
diff --git a/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift b/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift
index 198d5b3..e2861e6 100644
--- a/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift
+++ b/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift
@@ -237,14 +237,14 @@ public class G7CGMManager: CGMManager {
return nil
}

- public func scanForNewSensor() {
+ public func scanForNewSensor(scanAfterDelay: Bool = false) {
logDeviceCommunication("Forgetting existing sensor and starting scan for new sensor.", type: .connection)

mutateState { state in
state.sensorID = nil
state.activatedAt = nil
}
- sensor.scanForNewSensor()
+ sensor.scanForNewSensor(scanAfterDelay: scanAfterDelay)
}

private var device: HKDevice? {
@@ -319,7 +319,7 @@ extension G7CGMManager: G7SensorDelegate {
public func sensorDisconnected(_ sensor: G7Sensor, suspectedEndOfSession: Bool) {
logDeviceCommunication("Sensor disconnected: suspectedEndOfSession=\(suspectedEndOfSession)", type: .connection)
if suspectedEndOfSession {
- scanForNewSensor()
+ scanForNewSensor(scanAfterDelay: true)
}
}

diff --git a/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift b/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
index b1745a1..5c51092 100644
--- a/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
+++ b/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
@@ -99,11 +99,15 @@ public final class G7Sensor: G7BluetoothManagerDelegate {
bluetoothManager.delegate = self
}

- public func scanForNewSensor() {
+ public func scanForNewSensor(scanAfterDelay: Bool = false) {
self.sensorID = nil
bluetoothManager.disconnect()
bluetoothManager.forgetPeripheral()
- bluetoothManager.scanForPeripheral()
+ if scanAfterDelay {
+ bluetoothManager.scanAfterDelay()
+ } else {
+ bluetoothManager.scanForPeripheral()
+ }
}

public func resumeScanning() {
169 changes: 20 additions & 149 deletions g7_scan/g7_scan.patch
Original file line number Diff line number Diff line change
@@ -1,154 +1,25 @@
diff --git a/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift b/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift
index 62ab5fc..8976104 100644
--- a/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift
+++ b/G7SensorKit/G7SensorKit/G7CGMManager/G7BluetoothManager.swift
@@ -101,6 +101,9 @@ class G7BluetoothManager: NSObject {
return activePeripheralManager?.peripheral
}
}
+
+ /// Isolated to `managerQueue`
+ private var eventRegistrationActive : Bool = false

/// Isolated to `managerQueue`
private var managedPeripherals: [UUID:G7PeripheralManager] = [:]
@@ -131,7 +134,7 @@ class G7BluetoothManager: NSObject {
self.centralManager = CBCentralManager(delegate: self, queue: managerQueue, options: [CBCentralManagerOptionRestoreIdentifierKey: "com.loudnate.CGMBLEKit"])
}
}
-
+
// MARK: - Actions

func scanForPeripheral() {
@@ -177,8 +180,24 @@ class G7BluetoothManager: NSObject {
}
}
}
-
- private func managerQueue_scanForPeripheral() {
+
+ func centralManager(_ central: CBCentralManager, connectionEventDidOccur event: CBConnectionEvent, for peripheral: CBPeripheral) {
+
+ managerQueue.async {
+ guard self.eventRegistrationActive else {
+ self.centralManager.registerForConnectionEvents(options: nil)
+ return
+ }
+
+ self.managerQueue_establishActivePeripheral()
+
+ if !self.eventRegistrationActive {
+ self.centralManager.registerForConnectionEvents(options: nil)
+ }
+ }
+ }
+
+ private func managerQueue_establishActivePeripheral() {
dispatchPrecondition(condition: .onQueue(managerQueue))

guard centralManager.state == .poweredOn else {
@@ -187,6 +206,7 @@ class G7BluetoothManager: NSObject {

let currentState = activePeripheral?.state ?? .disconnected
guard currentState != .connected else {
+ eventRegistrationActive = false
return
}

@@ -201,6 +221,16 @@ class G7BluetoothManager: NSObject {
handleDiscoveredPeripheral(peripheral)
}
}
+
+ if activePeripheral != nil {
+ eventRegistrationActive = false
+ }
+ }
+
+ private func managerQueue_scanForPeripheral() {
+ dispatchPrecondition(condition: .onQueue(managerQueue))
+
+ managerQueue_establishActivePeripheral()

if activePeripheral == nil {
log.debug("Scanning for peripherals")
@@ -210,6 +240,14 @@ class G7BluetoothManager: NSObject {
options: nil
)
delegate?.bluetoothManagerScanningStatusDidChange(self)
+
+ if !eventRegistrationActive {
+ eventRegistrationActive = true
+ centralManager.registerForConnectionEvents(options: [CBConnectionEventMatchingOption.serviceUUIDs: [
+ SensorServiceUUID.advertisement.cbUUID,
+ SensorServiceUUID.cgmService.cbUUID
+ ]])
+ }
}
}

@@ -221,9 +259,9 @@ class G7BluetoothManager: NSObject {
The sleep gives the transmitter time to shut down, but keeps the app running.

*/
- fileprivate func scanAfterDelay() {
+ func scanAfterDelay() {
DispatchQueue.global(qos: .utility).async {
- Thread.sleep(forTimeInterval: 2)
+ Thread.sleep(forTimeInterval: 5)

self.scanForPeripheral()
}
diff --git a/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift b/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift
index 198d5b3..e2861e6 100644
--- a/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift
+++ b/G7SensorKit/G7SensorKit/G7CGMManager/G7CGMManager.swift
@@ -237,14 +237,14 @@ public class G7CGMManager: CGMManager {
return nil
}

- public func scanForNewSensor() {
+ public func scanForNewSensor(scanAfterDelay: Bool = false) {
logDeviceCommunication("Forgetting existing sensor and starting scan for new sensor.", type: .connection)

mutateState { state in
state.sensorID = nil
state.activatedAt = nil
}
- sensor.scanForNewSensor()
+ sensor.scanForNewSensor(scanAfterDelay: scanAfterDelay)
}

private var device: HKDevice? {
@@ -319,7 +319,7 @@ extension G7CGMManager: G7SensorDelegate {
public func sensorDisconnected(_ sensor: G7Sensor, suspectedEndOfSession: Bool) {
logDeviceCommunication("Sensor disconnected: suspectedEndOfSession=\(suspectedEndOfSession)", type: .connection)
if suspectedEndOfSession {
- scanForNewSensor()
+ scanForNewSensor(scanAfterDelay: true)
}
}

diff --git a/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift b/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
index b1745a1..5c51092 100644
index b1745a1..93d75d1 100644
--- a/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
+++ b/G7SensorKit/G7SensorKit/G7CGMManager/G7Sensor.swift
@@ -99,11 +99,15 @@ public final class G7Sensor: G7BluetoothManagerDelegate {
bluetoothManager.delegate = self
}
@@ -194,7 +194,10 @@ public final class G7Sensor: G7BluetoothManagerDelegate {
if let sensorID = sensorID, sensorID == peripheralManager.peripheral.name {

let suspectedEndOfSession: Bool
- if pendingAuth && wasRemoteDisconnect {
+
+ if let activationDate = activationDate, Date() > activationDate.addingTimeInterval(G7Sensor.lifetime + G7Sensor.gracePeriod), pendingAuth, wasRemoteDisconnect
+ {
+ self.log.info("Sensor disconnected at %{public}@", activationDate.description)
suspectedEndOfSession = true // Normal disconnect without auth is likely that G7 app stopped this session
} else {
suspectedEndOfSession = false
@@ -233,7 +236,7 @@ public final class G7Sensor: G7BluetoothManagerDelegate {

guard response.count > 0 else { return }

- public func scanForNewSensor() {
+ public func scanForNewSensor(scanAfterDelay: Bool = false) {
self.sensorID = nil
bluetoothManager.disconnect()
bluetoothManager.forgetPeripheral()
- bluetoothManager.scanForPeripheral()
+ if scanAfterDelay {
+ bluetoothManager.scanAfterDelay()
+ } else {
+ bluetoothManager.scanForPeripheral()
+ }
}
- log.debug("Received control response: %{public}@", response.hexadecimalString)
+ log.default("Received control response: %{public}@", response.hexadecimalString)

public func resumeScanning() {
switch G7Opcode(rawValue: response[0]) {
case .glucoseTx?: