Skip to content

Commit

Permalink
Libre improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanDegraeve committed Dec 10, 2020
1 parent bf3b5a9 commit 2b77af6
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class CGMLibre2Transmitter:BluetoothTransmitter, CGMTransmitter {
// current sensor serial number, if nil then it's not known yet
private var sensorSerialNumber:String?

// define libreNFC as NSObject, otherwise check on iOS14 wouuld need to be added.
// it will be casted to LibreNFC when needed
private var libreNFC: NSObject?

// MARK: - Initialization
Expand Down Expand Up @@ -101,13 +103,17 @@ class CGMLibre2Transmitter:BluetoothTransmitter, CGMTransmitter {
// when user clicks the scan button, an NFC read is initiated which will enable the bluetooth streaming
// meanwhile, the real scanning can start

// create libreNFC instance and start session, as the object
// LibreNFC will store a reference to self, as a result the object will not be deinitialized as long as CGMLibre2Transmitter is not deinitialized
// create libreNFC instance and start session
if #available(iOS 14.0, *), NFCTagReaderSession.readingAvailable {

libreNFC = LibreNFC(libreNFCDelegate: self)

(libreNFC as! LibreNFC).startSession()
// startScanning is getting called several times, but we must restrict launch of nfc scan to one single time, therefore check if libreNFC == nil
if libreNFC == nil {

libreNFC = LibreNFC(libreNFCDelegate: self)

(libreNFC as! LibreNFC).startSession()

}

} else {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class LibreNFC: NSObject, NFCTagReaderSessionDelegate {
private let log = OSLog(subsystem: ConstantsLog.subSystem, category: ConstantsLog.categoryLibreNFC)

/// will be used to pass back info like sensorUid , patchInfo to delegate
private var libreNFCDelegate: LibreNFCDelegate?
private(set) weak var libreNFCDelegate: LibreNFCDelegate?

/// fixed unlock code to use
private let unlockCode: UInt32 = 42
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,15 @@ extension BluetoothPeripheralManager: BluetoothTransmitterDelegate {
trace("in didConnect, transmitter address already known. This is not a new device, will disconnect", log: log, category: ConstantsLog.categoryBluetoothPeripheralManager, type: .info)

// It's an already known BluetoothTransmitter, not storing this, on the contrary disconnecting because maybe it's a bluetoothTransmitter already known for which user has preferred not to connect to
// but before that store the current bluetoothTransmitterDelegate
let bluetoothTransmitterDelegate = bluetoothTransmitter.bluetoothTransmitterDelegate
bluetoothTransmitter.disconnect()

// If we're actually waiting for a new scan result, then there's an instance of BluetoothTransmitter stored in tempBlueToothTransmitterWhileScanningForNewBluetoothPeripheral - but this one stopped scanning, so let's recreate an instance of BluetoothTransmitter
// transmitterTypeBeingScannedFor should be non nil here, unwrap
if let transmitterTypeBeingScannedFor = transmitterTypeBeingScannedFor {

self.tempBlueToothTransmitterWhileScanningForNewBluetoothPeripheral = createNewTransmitter(type: transmitterTypeBeingScannedFor, transmitterId: buetoothPeripheral.blePeripheral.transmitterId)
self.tempBlueToothTransmitterWhileScanningForNewBluetoothPeripheral = createNewTransmitter(type: transmitterTypeBeingScannedFor, transmitterId: buetoothPeripheral.blePeripheral.transmitterId, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate)

_ = self.tempBlueToothTransmitterWhileScanningForNewBluetoothPeripheral?.startScanning()

Expand Down
31 changes: 16 additions & 15 deletions xdrip/Managers/BluetoothPeripheral/BluetoothPeripheralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -793,65 +793,66 @@ class BluetoothPeripheralManager: NSObject {

/// - parameters:
/// - transmitterId : only for transmitter types that need it (at the moment only Dexcom and Blucon)
public func createNewTransmitter(type: BluetoothPeripheralType, transmitterId: String?) -> BluetoothTransmitter {
/// - bluetoothTransmitterDelegate : if not nil then this bluetoothTransmitterDelegate will be used when creating bluetoothTransmitter, otherwise self is used
public func createNewTransmitter(type: BluetoothPeripheralType, transmitterId: String?, bluetoothTransmitterDelegate: BluetoothTransmitterDelegate?) -> BluetoothTransmitter {

switch type {

case .M5StackType, .M5StickCType:

return M5StackBluetoothTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: self, m5StackBluetoothTransmitterDelegate: self, blePassword: UserDefaults.standard.m5StackBlePassword, bluetoothPeripheralType: type)
return M5StackBluetoothTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, m5StackBluetoothTransmitterDelegate: self, blePassword: UserDefaults.standard.m5StackBlePassword, bluetoothPeripheralType: type)

case .WatlaaType:

return WatlaaBluetoothTransmitter(address: nil, name: nil, cgmTransmitterDelegate: cgmTransmitterDelegate, bluetoothTransmitterDelegate: self, watlaaBluetoothTransmitterDelegate: self, sensorSerialNumber: nil, webOOPEnabled: nil, oopWebSite: nil, oopWebToken: nil, nonFixedSlopeEnabled: nil)
return WatlaaBluetoothTransmitter(address: nil, name: nil, cgmTransmitterDelegate: cgmTransmitterDelegate, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, watlaaBluetoothTransmitterDelegate: self, sensorSerialNumber: nil, webOOPEnabled: nil, oopWebSite: nil, oopWebToken: nil, nonFixedSlopeEnabled: nil)

case .DexcomG5Type:

guard let transmitterId = transmitterId, let cgmTransmitterDelegate = cgmTransmitterDelegate else {
fatalError("in createNewTransmitter, type DexcomG5Type, transmitterId is nil or cgmTransmitterDelegate is nil")
}

return CGMG5Transmitter(address: nil, name: nil, transmitterID: transmitterId, bluetoothTransmitterDelegate: self, cGMG5TransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate)
return CGMG5Transmitter(address: nil, name: nil, transmitterID: transmitterId, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMG5TransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate)

case .DexcomG6Type:

guard let transmitterId = transmitterId, let cgmTransmitterDelegate = cgmTransmitterDelegate else {
fatalError("in createNewTransmitter, type DexcomG6Type, transmitterId is nil or cgmTransmitterDelegate is nil")
}

return CGMG6Transmitter(address: nil, name: nil, transmitterID: transmitterId, bluetoothTransmitterDelegate: self, cGMG6TransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate)
return CGMG6Transmitter(address: nil, name: nil, transmitterID: transmitterId, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMG6TransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate)

case .BubbleType:

guard let cgmTransmitterDelegate = cgmTransmitterDelegate else {
fatalError("in createNewTransmitter, type DexcomG5Type, cgmTransmitterDelegate is nil")
}

return CGMBubbleTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: self, cGMBubbleTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: nil, webOOPEnabled: nil, oopWebSite: nil, oopWebToken: nil, nonFixedSlopeEnabled: nil)
return CGMBubbleTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMBubbleTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: nil, webOOPEnabled: nil, oopWebSite: nil, oopWebToken: nil, nonFixedSlopeEnabled: nil)

case .MiaoMiaoType:

guard let cgmTransmitterDelegate = cgmTransmitterDelegate else {
fatalError("in createNewTransmitter, MiaoMiaoType, cgmTransmitterDelegate is nil")
}

return CGMMiaoMiaoTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: self, cGMMiaoMiaoTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: nil, webOOPEnabled: nil, oopWebSite: nil, oopWebToken: nil, nonFixedSlopeEnabled: nil)
return CGMMiaoMiaoTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMMiaoMiaoTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: nil, webOOPEnabled: nil, oopWebSite: nil, oopWebToken: nil, nonFixedSlopeEnabled: nil)

case .DropletType:

guard let cgmTransmitterDelegate = cgmTransmitterDelegate else {
fatalError("in createNewTransmitter, DropletType, cgmTransmitterDelegate is nil")
}

return CGMDroplet1Transmitter(address: nil, name: nil, bluetoothTransmitterDelegate: self, cGMDropletTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: nil)
return CGMDroplet1Transmitter(address: nil, name: nil, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMDropletTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: nil)

case .GNSentryType:

guard let cgmTransmitterDelegate = cgmTransmitterDelegate else {
fatalError("in createNewTransmitter, GNSEntryType, cgmTransmitterDelegate is nil")
}

return CGMGNSEntryTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: self, cGMGNSEntryTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: nil)
return CGMGNSEntryTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMGNSEntryTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: nil)

case .BlueReaderType:

Expand All @@ -860,31 +861,31 @@ class BluetoothPeripheralManager: NSObject {
}


return CGMBlueReaderTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: self, cGMBlueReaderTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: nil)
return CGMBlueReaderTransmitter(address: nil, name: nil, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMBlueReaderTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: nil)

case .BluconType:

guard let transmitterId = transmitterId, let cgmTransmitterDelegate = cgmTransmitterDelegate else {
fatalError("in createNewTransmitter, type BluconType, transmitterId is nil or cgmTransmitterDelegate is nil")
}

return CGMBluconTransmitter(address: nil, name: nil, transmitterID: transmitterId, bluetoothTransmitterDelegate: self, cGMBluconTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: nil, nonFixedSlopeEnabled: nil)
return CGMBluconTransmitter(address: nil, name: nil, transmitterID: transmitterId, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMBluconTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: nil, nonFixedSlopeEnabled: nil)

case .DexcomG4Type:

guard let transmitterId = transmitterId, let cgmTransmitterDelegate = cgmTransmitterDelegate else {
fatalError("in createNewTransmitter, type DexcomG4Type, transmitterId is nil or cgmTransmitterDelegate is nil")
}

return CGMG4xDripTransmitter(address: nil, name: nil, transmitterID: transmitterId, bluetoothTransmitterDelegate: self, cGMDexcomG4TransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate)
return CGMG4xDripTransmitter(address: nil, name: nil, transmitterID: transmitterId, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMDexcomG4TransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate)

case .Libre2Type:

guard let cgmTransmitterDelegate = cgmTransmitterDelegate else {
fatalError("in createNewTransmitter, Libre2Type, cgmTransmitterDelegate is nil")
}

return CGMLibre2Transmitter(address: nil, name: nil, bluetoothTransmitterDelegate: self, cGMLibre2TransmitterDelegate: self, sensorSerialNumber: nil, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: nil, webOOPEnabled: nil)
return CGMLibre2Transmitter(address: nil, name: nil, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self, cGMLibre2TransmitterDelegate: self, sensorSerialNumber: nil, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: nil, webOOPEnabled: nil)

}

Expand Down Expand Up @@ -1220,12 +1221,12 @@ extension BluetoothPeripheralManager: BluetoothPeripheralManaging {

}

func startScanningForNewDevice(type: BluetoothPeripheralType, transmitterId: String?, callBackForScanningResult: ((BluetoothTransmitter.startScanningResult) -> Void)?, callback: @escaping (BluetoothPeripheral) -> Void) {
func startScanningForNewDevice(type: BluetoothPeripheralType, transmitterId: String?, bluetoothTransmitterDelegate: BluetoothTransmitterDelegate?, callBackForScanningResult: ((BluetoothTransmitter.startScanningResult) -> Void)?, callback: @escaping (BluetoothPeripheral) -> Void) {

callBackAfterDiscoveringDevice = callback

// create a temporary transmitter of requested type
let newBluetoothTranmsitter = createNewTransmitter(type: type, transmitterId: transmitterId)
let newBluetoothTranmsitter = createNewTransmitter(type: type, transmitterId: transmitterId, bluetoothTransmitterDelegate: bluetoothTransmitterDelegate ?? self)

// assign transmitterTypeBeingScannedFor, will be needed in case tempBlueToothTransmitterWhileScanningForNewBluetoothPeripheral is being recreated (search for transmitterTypeBeingScannedFor in BluetoothPeripheralManager+BluetoothTransmitterDelegate
transmitterTypeBeingScannedFor = type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ protocol BluetoothPeripheralManaging: BluetoothTransmitterDelegate {
/// - parameters:
/// - transmitterId : only for devices that need a transmitterID (currently only Dexcom and Blucon)
/// - callBackForScanningResult : to be called with result of startScanning
func startScanningForNewDevice(type: BluetoothPeripheralType, transmitterId: String?, callBackForScanningResult: ((BluetoothTransmitter.startScanningResult) -> Void)?, callback: @escaping (BluetoothPeripheral) -> Void)
/// - bluetoothTransmitterDelegate : optional
func startScanningForNewDevice(type: BluetoothPeripheralType, transmitterId: String?, bluetoothTransmitterDelegate: BluetoothTransmitterDelegate?, callBackForScanningResult: ((BluetoothTransmitter.startScanningResult) -> Void)?, callback: @escaping (BluetoothPeripheral) -> Void)

/// stops scanning for new device
func stopScanningForNewDevice()
Expand Down
1 change: 1 addition & 0 deletions xdrip/Storyboards/LibreNFC.strings
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"scanComplete" = "Scan Complete";
"holdTopOfIphoneNearSensor" = "Hold the top of your iOS device near the sensor";
"deviceMustSupportNFCAndIOS14" = "Device must support NFC and must run at least iOS 14.0";

6 changes: 5 additions & 1 deletion xdrip/Texts/TextsLibreNFC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class TextsLibreNFC {
}()

static let holdTopOfIphoneNearSensor: String = {
return NSLocalizedString("scanComplete", tableName: filename, bundle: Bundle.main, value: "Hold the top of your iOS device near the sensor", comment: "when NFC scanning is started, this message will appear")
return NSLocalizedString("holdTopOfIphoneNearSensor", tableName: filename, bundle: Bundle.main, value: "Hold the top of your iOS device near the sensor", comment: "when NFC scanning is started, this message will appear")
}()

static let deviceMustSupportNFCAndIOS14: String = {
return NSLocalizedString("deviceMustSupportNFCAndIOS14", tableName: filename, bundle: Bundle.main, value: "Hold the top of your iOS device near the sensor", comment: "Device must support NFC and must run at least iOS 14.0")
}()

}
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ class BluetoothPeripheralViewController: UIViewController {
// initiailize previousScanningResult to nil
previousScanningResult = nil

bluetoothPeripheralManager.startScanningForNewDevice(type: type, transmitterId: transmitterIdTempValue, callBackForScanningResult: handleScanningResult(startScanningResult:), callback: { (bluetoothPeripheral) in
bluetoothPeripheralManager.startScanningForNewDevice(type: type, transmitterId: transmitterIdTempValue, bluetoothTransmitterDelegate: self, callBackForScanningResult: handleScanningResult(startScanningResult:), callback: { (bluetoothPeripheral) in

// remove info alert screen which may still be there
self.dismissInfoAlertWhenScanningStarts()
Expand Down Expand Up @@ -1269,6 +1269,8 @@ extension BluetoothPeripheralViewController: UITableViewDataSource, UITableViewD

}

// MARK: - extension BluetoothTransmitterDelegate

extension BluetoothPeripheralViewController: BluetoothTransmitterDelegate {

func transmitterNeedsPairing(bluetoothTransmitter: BluetoothTransmitter) {
Expand Down

0 comments on commit 2b77af6

Please sign in to comment.