Skip to content

Fix to recover from sensor disconnects #33

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

Closed
wants to merge 3 commits into from
Closed
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
48 changes: 43 additions & 5 deletions G7SensorKit/G7CGMManager/G7BluetoothManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [:]
Expand Down Expand Up @@ -131,7 +134,7 @@ class G7BluetoothManager: NSObject {
self.centralManager = CBCentralManager(delegate: self, queue: managerQueue, options: [CBCentralManagerOptionRestoreIdentifierKey: "com.loudnate.CGMBLEKit"])
}
}

// MARK: - Actions

func scanForPeripheral() {
Expand Down Expand Up @@ -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 {
Expand All @@ -187,6 +206,7 @@ class G7BluetoothManager: NSObject {

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

Expand All @@ -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")
Expand All @@ -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
]])
}
}
}

Expand All @@ -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()
}
Expand Down
6 changes: 3 additions & 3 deletions G7SensorKit/G7CGMManager/G7CGMManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down Expand Up @@ -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)
}
}

Expand Down
8 changes: 6 additions & 2 deletions G7SensorKit/G7CGMManager/G7Sensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down