diff --git a/xdrip/BluetoothTransmitter/CGM/Dexcom/G4/CGMG4xDripTransmitter.swift b/xdrip/BluetoothTransmitter/CGM/Dexcom/G4/CGMG4xDripTransmitter.swift index 3d735012..49853aa9 100644 --- a/xdrip/BluetoothTransmitter/CGM/Dexcom/G4/CGMG4xDripTransmitter.swift +++ b/xdrip/BluetoothTransmitter/CGM/Dexcom/G4/CGMG4xDripTransmitter.swift @@ -155,12 +155,20 @@ final class CGMG4xDripTransmitter: BluetoothTransmitter, CGMTransmitter { } - // MARK:- CGMTransmitter protocol functions + // MARK: - CGMTransmitter protocol functions func cgmTransmitterType() -> CGMTransmitterType { return .dexcomG4 } + func getCBUUID_Service() -> String { + return CBUUID_Service_G4 + } + + func getCBUUID_Receive() -> String { + return CBUUID_ReceiveCharacteristic_G4 + } + // MARK:- helper functions private func processxBridgeDataPacket(value:Data) -> (glucoseData:GlucoseData?, batteryLevel:Int?, transmitterID:String?) { diff --git a/xdrip/BluetoothTransmitter/CGM/Dexcom/G5/CGMG5Transmitter.swift b/xdrip/BluetoothTransmitter/CGM/Dexcom/G5/CGMG5Transmitter.swift index 3fa51341..4db8bbdf 100644 --- a/xdrip/BluetoothTransmitter/CGM/Dexcom/G5/CGMG5Transmitter.swift +++ b/xdrip/BluetoothTransmitter/CGM/Dexcom/G5/CGMG5Transmitter.swift @@ -914,6 +914,14 @@ class CGMG5Transmitter:BluetoothTransmitter, CGMTransmitter { return !transmitterId.isFireFly() } + func getCBUUID_Service() -> String { + return CBUUID_Service_G5 + } + + func getCBUUID_Receive() -> String { + return CBUUID_Characteristic_UUID.CBUUID_Receive_Authentication.rawValue + } + // MARK: - private helper functions /// sends SensorTxMessage to transmitter diff --git a/xdrip/BluetoothTransmitter/CGM/Generic/CGMTransmitter.swift b/xdrip/BluetoothTransmitter/CGM/Generic/CGMTransmitter.swift index 0d07c41c..16304872 100644 --- a/xdrip/BluetoothTransmitter/CGM/Generic/CGMTransmitter.swift +++ b/xdrip/BluetoothTransmitter/CGM/Generic/CGMTransmitter.swift @@ -67,6 +67,12 @@ protocol CGMTransmitter:AnyObject { /// - default false func needsSensorStartCode() -> Bool + /// returns the service CBUUID + func getCBUUID_Service() -> String + + /// returns the receive characteristic CBUUID + func getCBUUID_Receive() -> String + } /// cgm transmitter types diff --git a/xdrip/BluetoothTransmitter/CGM/Libre/Atom/CGMAtomTransmitter.swift b/xdrip/BluetoothTransmitter/CGM/Libre/Atom/CGMAtomTransmitter.swift index 719ab925..bdf8e7c5 100644 --- a/xdrip/BluetoothTransmitter/CGM/Libre/Atom/CGMAtomTransmitter.swift +++ b/xdrip/BluetoothTransmitter/CGM/Libre/Atom/CGMAtomTransmitter.swift @@ -451,6 +451,14 @@ class CGMAtomTransmitter:BluetoothTransmitter, CGMTransmitter { } + func getCBUUID_Service() -> String { + return CBUUID_Service_Atom + } + + func getCBUUID_Receive() -> String { + return CBUUID_ReceiveCharacteristic_Atom + } + } diff --git a/xdrip/BluetoothTransmitter/CGM/Libre/Blucon/CGMBluconTransmitter.swift b/xdrip/BluetoothTransmitter/CGM/Libre/Blucon/CGMBluconTransmitter.swift index 1aa0dcf3..956602c3 100644 --- a/xdrip/BluetoothTransmitter/CGM/Libre/Blucon/CGMBluconTransmitter.swift +++ b/xdrip/BluetoothTransmitter/CGM/Libre/Blucon/CGMBluconTransmitter.swift @@ -544,5 +544,13 @@ extension CGMBluconTransmitter: CGMTransmitter { return nonFixedSlopeEnabled } + func getCBUUID_Service() -> String { + return CBUUID_BluconService + } + + func getCBUUID_Receive() -> String { + return CBUUID_ReceiveCharacteristic_Blucon + } + } diff --git a/xdrip/BluetoothTransmitter/CGM/Libre/BlueReader/CGMBlueReaderTransmitter.swift b/xdrip/BluetoothTransmitter/CGM/Libre/BlueReader/CGMBlueReaderTransmitter.swift index b9e2452c..5216292e 100644 --- a/xdrip/BluetoothTransmitter/CGM/Libre/BlueReader/CGMBlueReaderTransmitter.swift +++ b/xdrip/BluetoothTransmitter/CGM/Libre/BlueReader/CGMBlueReaderTransmitter.swift @@ -136,4 +136,12 @@ class CGMBlueReaderTransmitter:BluetoothTransmitter, CGMTransmitter { return nonFixedSlopeEnabled } + func getCBUUID_Service() -> String { + return CBUUID_Service_BlueReader + } + + func getCBUUID_Receive() -> String { + return CBUUID_ReceiveCharacteristic_BlueReader + } + } diff --git a/xdrip/BluetoothTransmitter/CGM/Libre/Bubble/CGMBubbleTransmitter.swift b/xdrip/BluetoothTransmitter/CGM/Libre/Bubble/CGMBubbleTransmitter.swift index 8820923a..540cbd57 100644 --- a/xdrip/BluetoothTransmitter/CGM/Libre/Bubble/CGMBubbleTransmitter.swift +++ b/xdrip/BluetoothTransmitter/CGM/Libre/Bubble/CGMBubbleTransmitter.swift @@ -354,6 +354,15 @@ class CGMBubbleTransmitter:BluetoothTransmitter, CGMTransmitter { } + func getCBUUID_Service() -> String { + return CBUUID_Service_Bubble + } + + func getCBUUID_Receive() -> String { + return CBUUID_ReceiveCharacteristic_Bubble + } + + // MARK: - helpers /// reset rxBuffer, reset startDate, stop packetRxMonitorTimer, set resendPacketCounter to 0 diff --git a/xdrip/BluetoothTransmitter/CGM/Libre/Droplet/CGMDroplet1Transmitter.swift b/xdrip/BluetoothTransmitter/CGM/Libre/Droplet/CGMDroplet1Transmitter.swift index cf26ecf6..0a81dbfd 100644 --- a/xdrip/BluetoothTransmitter/CGM/Libre/Droplet/CGMDroplet1Transmitter.swift +++ b/xdrip/BluetoothTransmitter/CGM/Libre/Droplet/CGMDroplet1Transmitter.swift @@ -140,4 +140,12 @@ class CGMDroplet1Transmitter:BluetoothTransmitter, CGMTransmitter { return nonFixedSlopeEnabled } + func getCBUUID_Service() -> String { + return CBUUID_Service_Droplet + } + + func getCBUUID_Receive() -> String { + return CBUUID_ReceiveCharacteristic_Droplet + } + } diff --git a/xdrip/BluetoothTransmitter/CGM/Libre/GNSEntry/CGMGNSEntryTransmitter.swift b/xdrip/BluetoothTransmitter/CGM/Libre/GNSEntry/CGMGNSEntryTransmitter.swift index cd5b0012..464d901e 100644 --- a/xdrip/BluetoothTransmitter/CGM/Libre/GNSEntry/CGMGNSEntryTransmitter.swift +++ b/xdrip/BluetoothTransmitter/CGM/Libre/GNSEntry/CGMGNSEntryTransmitter.swift @@ -237,7 +237,7 @@ class CGMGNSEntryTransmitter:BluetoothTransmitter, CGMTransmitter { } - // MARK: CGMTransmitter protocol functions + // MARK: - CGMTransmitter protocol functions func setNonFixedSlopeEnabled(enabled: Bool) { nonFixedSlopeEnabled = enabled @@ -251,7 +251,15 @@ class CGMGNSEntryTransmitter:BluetoothTransmitter, CGMTransmitter { return nonFixedSlopeEnabled } - // MARK: CBCentralManager overriden functions + func getCBUUID_Service() -> String { + return CBUUID_GNWService + } + + func getCBUUID_Receive() -> String { + return CBUUID_Characteristic_UUID.CBUUID_GNW_Notify.rawValue + } + + // MARK: - CBCentralManager overriden functions override func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { trace("didDiscoverCharacteristicsFor", log: log, category: ConstantsLog.categoryCGMGNSEntry, type: .info) diff --git a/xdrip/BluetoothTransmitter/CGM/Libre/Libre2/CGMLibre2Transmitter.swift b/xdrip/BluetoothTransmitter/CGM/Libre/Libre2/CGMLibre2Transmitter.swift index 41a5eaf6..a1e0fe8d 100644 --- a/xdrip/BluetoothTransmitter/CGM/Libre/Libre2/CGMLibre2Transmitter.swift +++ b/xdrip/BluetoothTransmitter/CGM/Libre/Libre2/CGMLibre2Transmitter.swift @@ -351,6 +351,14 @@ class CGMLibre2Transmitter:BluetoothTransmitter, CGMTransmitter { } + func getCBUUID_Service() -> String { + return CBUUID_Service_Libre2 + } + + func getCBUUID_Receive() -> String { + return CBUUID_ReceiveCharacteristic_Libre2 + } + } #else diff --git a/xdrip/BluetoothTransmitter/CGM/Libre/MiaoMiao/CGMMiaoMiaoTransmitter.swift b/xdrip/BluetoothTransmitter/CGM/Libre/MiaoMiao/CGMMiaoMiaoTransmitter.swift index 32814f8a..46ebc16f 100644 --- a/xdrip/BluetoothTransmitter/CGM/Libre/MiaoMiao/CGMMiaoMiaoTransmitter.swift +++ b/xdrip/BluetoothTransmitter/CGM/Libre/MiaoMiao/CGMMiaoMiaoTransmitter.swift @@ -333,6 +333,14 @@ class CGMMiaoMiaoTransmitter:BluetoothTransmitter, CGMTransmitter { } + func getCBUUID_Service() -> String { + return CBUUID_Service_MiaoMiao + } + + func getCBUUID_Receive() -> String { + return CBUUID_ReceiveCharacteristic_MiaoMiao + } + // MARK: - helpers /// reset rxBuffer, reset startDate, set resendPacketCounter to 0 diff --git a/xdrip/BluetoothTransmitter/watlaa/WatlaaBluetoothTransmitter.swift b/xdrip/BluetoothTransmitter/watlaa/WatlaaBluetoothTransmitter.swift index 8caf4932..ddd46896 100644 --- a/xdrip/BluetoothTransmitter/watlaa/WatlaaBluetoothTransmitter.swift +++ b/xdrip/BluetoothTransmitter/watlaa/WatlaaBluetoothTransmitter.swift @@ -20,7 +20,7 @@ final class WatlaaBluetoothTransmitter: BluetoothTransmitter { let CBUUID_Battery_Service = "0000180F-0000-1000-8000-00805F9B34FB" /// characteristic uuids (created them in an enum as there's a lot of them, it's easy to switch through the list) - private enum CBUUID_Characteristic_UUID:String, CustomStringConvertible, CaseIterable { + public enum CBUUID_Characteristic_UUID:String, CustomStringConvertible, CaseIterable { /// Bridge connection status characteristic case CBUUID_BridgeConnectionStatus_Characteristic = "00001012-1212-EFDE-0137-875F45AC0113" diff --git a/xdrip/BluetoothTransmitter/watlaa/WatlaaBluetoothTransmitterMaster+CGMTransmitter.swift b/xdrip/BluetoothTransmitter/watlaa/WatlaaBluetoothTransmitterMaster+CGMTransmitter.swift index 1ffa663f..71f9768c 100644 --- a/xdrip/BluetoothTransmitter/watlaa/WatlaaBluetoothTransmitterMaster+CGMTransmitter.swift +++ b/xdrip/BluetoothTransmitter/watlaa/WatlaaBluetoothTransmitterMaster+CGMTransmitter.swift @@ -36,4 +36,12 @@ extension WatlaaBluetoothTransmitter: CGMTransmitter { _ = sendStartReadingCommand() } + func getCBUUID_Service() -> String { + return CBUUID_Data_Service + } + + func getCBUUID_Receive() -> String { + return CBUUID_Characteristic_UUID.CBUUID_ReceiveCharacteristic.rawValue + } + } diff --git a/xdrip/Managers/BluetoothPeripheral/BluetoothPeripheralManager.swift b/xdrip/Managers/BluetoothPeripheral/BluetoothPeripheralManager.swift index d3c27553..8b71de00 100644 --- a/xdrip/Managers/BluetoothPeripheral/BluetoothPeripheralManager.swift +++ b/xdrip/Managers/BluetoothPeripheral/BluetoothPeripheralManager.swift @@ -43,11 +43,19 @@ class BluetoothPeripheralManager: NSObject { /// /// this is to keep track of changes in cgmTransmitter (ie if switching from transmitter A to B) public var currentCgmTransmitterAddress: String? { + didSet(newValue) { + if newValue != currentCgmTransmitterAddress { + cgmTransmitterInfoChanged() + + setCGMTransmitterInSharedUserDefaults() + } + } + } // MARK: - private properties @@ -92,378 +100,14 @@ class BluetoothPeripheralManager: NSObject { super.init() - // loop through blePeripherals - for blePeripheral in bLEPeripheralAccessor.getBLEPeripherals() { - - // each time the app launches, we will send the parameters to all BluetoothPeripherals (only used for M5Stack for now) - blePeripheral.parameterUpdateNeededAtNextConnect = true - - // need to initialize all types of bluetoothperipheral - // using enum here to make sure future types are not forgotten - bluetoothPeripheralTypeLoop: for bluetoothPeripheralType in BluetoothPeripheralType.allCases { - - switch bluetoothPeripheralType { - - case .M5StackType: - // no seperate handling needed for M5StickC because M5StickC is stored in coredata as M5Stack objecct, so it will be handled when going through case M5StackType - // in other words this case will never be applicable - break - - case .M5StickCType: - if let m5Stack = blePeripheral.m5Stack { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: m5Stack) - - if m5Stack.blePeripheral.shouldconnect { - - // create an instance of M5StackBluetoothTransmitter, M5StackBluetoothTransmitter will automatically try to connect to the M5Stack with the address that is stored in m5Stack - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(M5StackBluetoothTransmitter(address: m5Stack.blePeripheral.address, name: m5Stack.blePeripheral.name, bluetoothTransmitterDelegate: self, m5StackBluetoothTransmitterDelegate: self, blePassword: m5Stack.blepassword, bluetoothPeripheralType: m5Stack.isM5StickC ? .M5StickCType : .M5StackType), at: index) - - } else { - - // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - } - - case .WatlaaType: - - if let watlaa = blePeripheral.watlaa { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: watlaa) - - if watlaa.blePeripheral.shouldconnect { - - // create an instance of WatlaaBluetoothTransmitter, WatlaaBluetoothTransmitter will automatically try to connect to the watlaa with the address that is stored in watlaa - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(WatlaaBluetoothTransmitter(address: watlaa.blePeripheral.address, name: watlaa.blePeripheral.name, cgmTransmitterDelegate: cgmTransmitterDelegate, bluetoothTransmitterDelegate: self, watlaaBluetoothTransmitterDelegate: self, sensorSerialNumber: watlaa.blePeripheral.sensorSerialNumber, webOOPEnabled: watlaa.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: watlaa.blePeripheral.nonFixedSlopeEnabled), at: index) - - // if watlaa is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true - if bluetoothPeripheralType.category() == .CGM { - currentCgmTransmitterAddress = blePeripheral.address - } - - } else { - - // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - } - - case .DexcomType: - - if let dexcomG5orG6 = blePeripheral.dexcomG5 { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: dexcomG5orG6) - - if dexcomG5orG6.blePeripheral.shouldconnect { - - if let transmitterId = dexcomG5orG6.blePeripheral.transmitterId { - - // create an instance of CGMG5Transmitter, will automatically try to connect to the dexcom with the address that is stored in dexcom - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(CGMG5Transmitter(address: dexcomG5orG6.blePeripheral.address, name: dexcomG5orG6.blePeripheral.name, transmitterID: transmitterId, bluetoothTransmitterDelegate: self, cGMG5TransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, transmitterStartDate: dexcomG5orG6.transmitterStartDate, sensorStartDate: dexcomG5orG6.sensorStartDate, calibrationToSendToTransmitter: calibrationsAccessor.lastCalibrationForActiveSensor(withActivesensor: sensorsAccessor.fetchActiveSensor()), firmware: dexcomG5orG6.firmwareVersion, webOOPEnabled: dexcomG5orG6.blePeripheral.webOOPEnabled, useOtherApp: dexcomG5orG6.useOtherApp), at: index) - - // if DexcomG5Type is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true - if bluetoothPeripheralType.category() == .CGM { - currentCgmTransmitterAddress = blePeripheral.address - } - - } - - } else { - - // bluetoothTransmitters array (which should have the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - // because two types are being handled here (DexcomG5Type and DexcomG6Type) we need to avoid that the same blePeripheral is added two times - // this we do by breaking the bluetoothPeripheralTypeLoop - break bluetoothPeripheralTypeLoop - - } - - case .BubbleType: - - if let bubble = blePeripheral.bubble { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: bubble) - - if bubble.blePeripheral.shouldconnect { - - // create an instance of BubbleBluetoothTransmitter, BubbleBluetoothTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(CGMBubbleTransmitter(address: bubble.blePeripheral.address, name: bubble.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMBubbleTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: bubble.blePeripheral.sensorSerialNumber, webOOPEnabled: bubble.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: bubble.blePeripheral.nonFixedSlopeEnabled), at: index) - - // if BubbleType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true - if bluetoothPeripheralType.category() == .CGM { - currentCgmTransmitterAddress = blePeripheral.address - } - - } else { - - // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - } - - case .MiaoMiaoType: - - if let miaoMiao = blePeripheral.miaoMiao { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: miaoMiao) - - if miaoMiao.blePeripheral.shouldconnect { - - // create an instance of CGMMiaoMiaoTransmitter, CGMMiaoMiaoTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(CGMMiaoMiaoTransmitter(address: miaoMiao.blePeripheral.address, name: miaoMiao.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMMiaoMiaoTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: miaoMiao.blePeripheral.sensorSerialNumber, webOOPEnabled: miaoMiao.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: miaoMiao.blePeripheral.nonFixedSlopeEnabled), at: index) - - // if MiaoMiaoType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true - if bluetoothPeripheralType.category() == .CGM { - currentCgmTransmitterAddress = blePeripheral.address - } - - } else { - - // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - } - - case .AtomType: - - if let atom = blePeripheral.atom { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: atom) - - if atom.blePeripheral.shouldconnect { - - // create an instance of CGMAtomTransmitter, CGMAtomTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(CGMAtomTransmitter(address: atom.blePeripheral.address, name: atom.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMAtomTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: atom.blePeripheral.sensorSerialNumber, webOOPEnabled: atom.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: atom.blePeripheral.nonFixedSlopeEnabled, firmWare: atom.firmware), at: index) - - // if AtomType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true - if bluetoothPeripheralType.category() == .CGM { - currentCgmTransmitterAddress = blePeripheral.address - } - - } else { - - // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - } - - case .DexcomG4Type: - - if let dexcomG4 = blePeripheral.dexcomG4 { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: dexcomG4) - - if dexcomG4.blePeripheral.shouldconnect { - - if let transmitterId = dexcomG4.blePeripheral.transmitterId { - - // create an instance of CGMDexcomG4Transmitter, CGMDexcomG4Transmitter will automatically try to connect to the Bubble with the address that is stored in bubble - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(CGMG4xDripTransmitter(address: dexcomG4.blePeripheral.address, name: dexcomG4.blePeripheral.name, transmitterID: transmitterId, bluetoothTransmitterDelegate: self, cGMDexcomG4TransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate), at: index) - - // if DexcomG4Type is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true - if bluetoothPeripheralType.category() == .CGM { - currentCgmTransmitterAddress = blePeripheral.address - } - - } - - } else { - - // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - } - - case .Libre2Type: - - if let libre2 = blePeripheral.libre2 { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: libre2) - - if libre2.blePeripheral.shouldconnect { - - // create an instance of CGMDropletTransmitter, CGMDropletTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(CGMLibre2Transmitter(address: libre2.blePeripheral.address, name: libre2.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMLibre2TransmitterDelegate: self, sensorSerialNumber: libre2.blePeripheral.sensorSerialNumber, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: libre2.blePeripheral.nonFixedSlopeEnabled, webOOPEnabled: libre2.blePeripheral.webOOPEnabled), at: index) - - // if Libre2Type is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true - if bluetoothPeripheralType.category() == .CGM { - currentCgmTransmitterAddress = blePeripheral.address - } - - } else { - - // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - - } - - case .DropletType: - - if let droplet = blePeripheral.droplet { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: droplet) - - if droplet.blePeripheral.shouldconnect { - - // create an instance of CGMDropletTransmitter, CGMDropletTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(CGMDroplet1Transmitter(address: droplet.blePeripheral.address, name: droplet.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMDropletTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: droplet.blePeripheral.nonFixedSlopeEnabled), at: index) - - // if DropletType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true - if bluetoothPeripheralType.category() == .CGM { - currentCgmTransmitterAddress = blePeripheral.address - } - - } else { - - // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - } - - case .BlueReaderType: - - if let blueReader = blePeripheral.blueReader { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: blueReader) - - if blueReader.blePeripheral.shouldconnect { - - // create an instance of CGMBlueReaderTransmitter, CGMBlueReaderTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(CGMBlueReaderTransmitter(address: blueReader.blePeripheral.address, name: blueReader.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMBlueReaderTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: blueReader.blePeripheral.nonFixedSlopeEnabled), at: index) - - // if BlueReaderType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true - if bluetoothPeripheralType.category() == .CGM { - currentCgmTransmitterAddress = blePeripheral.address - } - - } else { - - // bluetoothTransmitters array (which should have the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - } - - case .GNSentryType: - - if let gNSEntry = blePeripheral.gNSEntry { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: gNSEntry) - - if gNSEntry.blePeripheral.shouldconnect { - - // create an instance of CGMGNSEntryTransmitter, CGMGNSEntryTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(CGMGNSEntryTransmitter(address: gNSEntry.blePeripheral.address, name: gNSEntry.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMGNSEntryTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: gNSEntry.blePeripheral.nonFixedSlopeEnabled), at: index) - - // if GNSentryType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true - if bluetoothPeripheralType.category() == .CGM { - currentCgmTransmitterAddress = blePeripheral.address - } - - } else { - - // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - } - - case .BluconType: - - if let blucon = blePeripheral.blucon { - - // add it to the list of bluetoothPeripherals - let index = insertInBluetoothPeripherals(bluetoothPeripheral: blucon) - - if blucon.blePeripheral.shouldconnect { - - if let transmitterId = blucon.blePeripheral.transmitterId { - - // create an instance of CGMBluconTransmitter, CGMBluconTransmitter will automatically try to connect to the Bluon with the address that is stored in blucon - // add it to the array of bluetoothTransmitters - bluetoothTransmitters.insert(CGMBluconTransmitter(address: blucon.blePeripheral.address, name: blucon.blePeripheral.name, transmitterID: transmitterId, bluetoothTransmitterDelegate: self, cGMBluconTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: blucon.blePeripheral.sensorSerialNumber, nonFixedSlopeEnabled: blucon.blePeripheral.nonFixedSlopeEnabled), at: index) - - // if bluconType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true - if bluetoothPeripheralType.category() == .CGM { - currentCgmTransmitterAddress = blePeripheral.address - } - - } - - } else { - - // bluetoothTransmitters array (which should have the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter - bluetoothTransmitters.insert(nil, at: index) - - } - - } - } - - } - - } - - - // CAN BE DELETED ONCE 3.X IS NOT USED ANYMORE - // if cgmTransUserDefaults.standard.cgmTransmitterTypemitterType is nil but UserDefaults.standard.cgmTransmitterDeviceAddress is not nil, then this is the first install of 4.x after 3.x - if UserDefaults.standard.cgmTransmitterType != nil && UserDefaults.standard.cgmTransmitterDeviceAddress != nil { - - uIViewController.present(UIAlertController(title: Texts_Common.warning, message: "Transmitters are now created in the bluetooth tab. You will need to recreate your transmitter first. Your sensor status will remain", actionHandler: nil), animated: true, completion: nil) - - UserDefaults.standard.cgmTransmitterDeviceAddress = nil - - } - // DELETE UP TO HERE - - // when user changes any of the buetooth peripheral related settings, that need to be sent to the transmitter addObservers() + + // by calling this function in defer block, it will be executed after init has finished. + // when assigning a value to currentCgmTransmitterAddress, the didset in that func will be triggered (which was not the case if the assignment happens within the init function) + defer { + setupBLEPeripherals(cgmTransmitterDelegate: cgmTransmitterDelegate) + } } @@ -956,6 +600,41 @@ class BluetoothPeripheralManager: NSObject { // MARK: - private functions + /// sets cgmTransmitterDeviceAddress, cgmTransmitter_CBUUID_Service, cgmTransmitter_CBUUID_Receive in shared userdefaults + /// for use with xdrip-client-swift + private func setCGMTransmitterInSharedUserDefaults() { + + if let sharedUserDefaults = UserDefaults(suiteName: Bundle.main.appGroupSuiteName) { + + if let cgmTransmitter = getCGMTransmitter(), let cgmtransmitterAddress = currentCgmTransmitterAddress { + + debuglogging("setting shared user defaults to cgmtransmitteraddress " + cgmtransmitterAddress) + + // store getCBUUID_Receive + sharedUserDefaults.set(cgmTransmitter.getCBUUID_Receive(), forKey: "cgmTransmitter_CBUUID_Receive") + + // store cgm transmitter device address + sharedUserDefaults.set(cgmtransmitterAddress, forKey: "cgmTransmitterDeviceAddress") + + // store getCBUUID_Service + sharedUserDefaults.set(cgmTransmitter.getCBUUID_Service(), forKey: "cgmTransmitter_CBUUID_Service") + + } else { + + // there's no cgm transmitter currently active (ie configured as 'connect') + // store nil as cgm transmitter device address + // we don't care about CBUUID_Service and CBUUID_Receive + + debuglogging("setting shared user defaults to nil") + + sharedUserDefaults.set(nil, forKey: "cgmTransmitterDeviceAddress") + + } + + } + + } + /// when user changes M5Stack related settings, then the transmitter need to get that info, add observers private func addObservers() { @@ -1025,6 +704,368 @@ class BluetoothPeripheralManager: NSObject { } + /// setup bleperipherals + private func setupBLEPeripherals(cgmTransmitterDelegate: CGMTransmitterDelegate) { + + // loop through blePeripherals + for blePeripheral in bLEPeripheralAccessor.getBLEPeripherals() { + + // each time the app launches, we will send the parameters to all BluetoothPeripherals (only used for M5Stack for now) + blePeripheral.parameterUpdateNeededAtNextConnect = true + + // need to initialize all types of bluetoothperipheral + // using enum here to make sure future types are not forgotten + bluetoothPeripheralTypeLoop: for bluetoothPeripheralType in BluetoothPeripheralType.allCases { + + switch bluetoothPeripheralType { + + case .M5StackType: + // no seperate handling needed for M5StickC because M5StickC is stored in coredata as M5Stack objecct, so it will be handled when going through case M5StackType + // in other words this case will never be applicable + break + + case .M5StickCType: + if let m5Stack = blePeripheral.m5Stack { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: m5Stack) + + if m5Stack.blePeripheral.shouldconnect { + + // create an instance of M5StackBluetoothTransmitter, M5StackBluetoothTransmitter will automatically try to connect to the M5Stack with the address that is stored in m5Stack + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(M5StackBluetoothTransmitter(address: m5Stack.blePeripheral.address, name: m5Stack.blePeripheral.name, bluetoothTransmitterDelegate: self, m5StackBluetoothTransmitterDelegate: self, blePassword: m5Stack.blepassword, bluetoothPeripheralType: m5Stack.isM5StickC ? .M5StickCType : .M5StackType), at: index) + + } else { + + // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + } + + case .WatlaaType: + + if let watlaa = blePeripheral.watlaa { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: watlaa) + + if watlaa.blePeripheral.shouldconnect { + + // create an instance of WatlaaBluetoothTransmitter, WatlaaBluetoothTransmitter will automatically try to connect to the watlaa with the address that is stored in watlaa + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(WatlaaBluetoothTransmitter(address: watlaa.blePeripheral.address, name: watlaa.blePeripheral.name, cgmTransmitterDelegate: cgmTransmitterDelegate, bluetoothTransmitterDelegate: self, watlaaBluetoothTransmitterDelegate: self, sensorSerialNumber: watlaa.blePeripheral.sensorSerialNumber, webOOPEnabled: watlaa.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: watlaa.blePeripheral.nonFixedSlopeEnabled), at: index) + + // if watlaa is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true + if bluetoothPeripheralType.category() == .CGM { + currentCgmTransmitterAddress = blePeripheral.address + } + + } else { + + // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + } + + case .DexcomType: + + if let dexcomG5orG6 = blePeripheral.dexcomG5 { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: dexcomG5orG6) + + if dexcomG5orG6.blePeripheral.shouldconnect { + + if let transmitterId = dexcomG5orG6.blePeripheral.transmitterId { + + // create an instance of CGMG5Transmitter, will automatically try to connect to the dexcom with the address that is stored in dexcom + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(CGMG5Transmitter(address: dexcomG5orG6.blePeripheral.address, name: dexcomG5orG6.blePeripheral.name, transmitterID: transmitterId, bluetoothTransmitterDelegate: self, cGMG5TransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, transmitterStartDate: dexcomG5orG6.transmitterStartDate, sensorStartDate: dexcomG5orG6.sensorStartDate, calibrationToSendToTransmitter: calibrationsAccessor.lastCalibrationForActiveSensor(withActivesensor: sensorsAccessor.fetchActiveSensor()), firmware: dexcomG5orG6.firmwareVersion, webOOPEnabled: dexcomG5orG6.blePeripheral.webOOPEnabled, useOtherApp: dexcomG5orG6.useOtherApp), at: index) + + // if DexcomG5Type is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true + if bluetoothPeripheralType.category() == .CGM { + currentCgmTransmitterAddress = blePeripheral.address + } + + } + + } else { + + // bluetoothTransmitters array (which should have the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + // because two types are being handled here (DexcomG5Type and DexcomG6Type) we need to avoid that the same blePeripheral is added two times + // this we do by breaking the bluetoothPeripheralTypeLoop + break bluetoothPeripheralTypeLoop + + } + + case .BubbleType: + + if let bubble = blePeripheral.bubble { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: bubble) + + if bubble.blePeripheral.shouldconnect { + + // create an instance of BubbleBluetoothTransmitter, BubbleBluetoothTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(CGMBubbleTransmitter(address: bubble.blePeripheral.address, name: bubble.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMBubbleTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: bubble.blePeripheral.sensorSerialNumber, webOOPEnabled: bubble.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: bubble.blePeripheral.nonFixedSlopeEnabled), at: index) + + // if BubbleType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true + if bluetoothPeripheralType.category() == .CGM { + currentCgmTransmitterAddress = blePeripheral.address + } + + } else { + + // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + } + + case .MiaoMiaoType: + + if let miaoMiao = blePeripheral.miaoMiao { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: miaoMiao) + + if miaoMiao.blePeripheral.shouldconnect { + + // create an instance of CGMMiaoMiaoTransmitter, CGMMiaoMiaoTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(CGMMiaoMiaoTransmitter(address: miaoMiao.blePeripheral.address, name: miaoMiao.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMMiaoMiaoTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: miaoMiao.blePeripheral.sensorSerialNumber, webOOPEnabled: miaoMiao.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: miaoMiao.blePeripheral.nonFixedSlopeEnabled), at: index) + + // if MiaoMiaoType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true + if bluetoothPeripheralType.category() == .CGM { + currentCgmTransmitterAddress = blePeripheral.address + } + + } else { + + // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + } + + case .AtomType: + + if let atom = blePeripheral.atom { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: atom) + + if atom.blePeripheral.shouldconnect { + + // create an instance of CGMAtomTransmitter, CGMAtomTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(CGMAtomTransmitter(address: atom.blePeripheral.address, name: atom.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMAtomTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: atom.blePeripheral.sensorSerialNumber, webOOPEnabled: atom.blePeripheral.webOOPEnabled, nonFixedSlopeEnabled: atom.blePeripheral.nonFixedSlopeEnabled, firmWare: atom.firmware), at: index) + + // if AtomType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true + if bluetoothPeripheralType.category() == .CGM { + currentCgmTransmitterAddress = blePeripheral.address + } + + } else { + + // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + } + + case .DexcomG4Type: + + if let dexcomG4 = blePeripheral.dexcomG4 { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: dexcomG4) + + if dexcomG4.blePeripheral.shouldconnect { + + if let transmitterId = dexcomG4.blePeripheral.transmitterId { + + // create an instance of CGMDexcomG4Transmitter, CGMDexcomG4Transmitter will automatically try to connect to the Bubble with the address that is stored in bubble + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(CGMG4xDripTransmitter(address: dexcomG4.blePeripheral.address, name: dexcomG4.blePeripheral.name, transmitterID: transmitterId, bluetoothTransmitterDelegate: self, cGMDexcomG4TransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate), at: index) + + // if DexcomG4Type is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true + if bluetoothPeripheralType.category() == .CGM { + currentCgmTransmitterAddress = blePeripheral.address + } + + } + + } else { + + // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + } + + case .Libre2Type: + + if let libre2 = blePeripheral.libre2 { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: libre2) + + if libre2.blePeripheral.shouldconnect { + + // create an instance of CGMDropletTransmitter, CGMDropletTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(CGMLibre2Transmitter(address: libre2.blePeripheral.address, name: libre2.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMLibre2TransmitterDelegate: self, sensorSerialNumber: libre2.blePeripheral.sensorSerialNumber, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: libre2.blePeripheral.nonFixedSlopeEnabled, webOOPEnabled: libre2.blePeripheral.webOOPEnabled), at: index) + + // if Libre2Type is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true + if bluetoothPeripheralType.category() == .CGM { + currentCgmTransmitterAddress = blePeripheral.address + } + + } else { + + // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + + } + + case .DropletType: + + if let droplet = blePeripheral.droplet { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: droplet) + + if droplet.blePeripheral.shouldconnect { + + // create an instance of CGMDropletTransmitter, CGMDropletTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(CGMDroplet1Transmitter(address: droplet.blePeripheral.address, name: droplet.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMDropletTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: droplet.blePeripheral.nonFixedSlopeEnabled), at: index) + + // if DropletType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true + if bluetoothPeripheralType.category() == .CGM { + currentCgmTransmitterAddress = blePeripheral.address + } + + } else { + + // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + } + + case .BlueReaderType: + + if let blueReader = blePeripheral.blueReader { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: blueReader) + + if blueReader.blePeripheral.shouldconnect { + + // create an instance of CGMBlueReaderTransmitter, CGMBlueReaderTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(CGMBlueReaderTransmitter(address: blueReader.blePeripheral.address, name: blueReader.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMBlueReaderTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: blueReader.blePeripheral.nonFixedSlopeEnabled), at: index) + + // if BlueReaderType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true + if bluetoothPeripheralType.category() == .CGM { + currentCgmTransmitterAddress = blePeripheral.address + } + + } else { + + // bluetoothTransmitters array (which should have the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + } + + case .GNSentryType: + + if let gNSEntry = blePeripheral.gNSEntry { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: gNSEntry) + + if gNSEntry.blePeripheral.shouldconnect { + + // create an instance of CGMGNSEntryTransmitter, CGMGNSEntryTransmitter will automatically try to connect to the Bubble with the address that is stored in bubble + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(CGMGNSEntryTransmitter(address: gNSEntry.blePeripheral.address, name: gNSEntry.blePeripheral.name, bluetoothTransmitterDelegate: self, cGMGNSEntryTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, nonFixedSlopeEnabled: gNSEntry.blePeripheral.nonFixedSlopeEnabled), at: index) + + // if GNSentryType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true + if bluetoothPeripheralType.category() == .CGM { + currentCgmTransmitterAddress = blePeripheral.address + } + + } else { + + // bluetoothTransmitters array (which shoul dhave the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + } + + case .BluconType: + + if let blucon = blePeripheral.blucon { + + // add it to the list of bluetoothPeripherals + let index = insertInBluetoothPeripherals(bluetoothPeripheral: blucon) + + if blucon.blePeripheral.shouldconnect { + + if let transmitterId = blucon.blePeripheral.transmitterId { + + // create an instance of CGMBluconTransmitter, CGMBluconTransmitter will automatically try to connect to the Bluon with the address that is stored in blucon + // add it to the array of bluetoothTransmitters + bluetoothTransmitters.insert(CGMBluconTransmitter(address: blucon.blePeripheral.address, name: blucon.blePeripheral.name, transmitterID: transmitterId, bluetoothTransmitterDelegate: self, cGMBluconTransmitterDelegate: self, cGMTransmitterDelegate: cgmTransmitterDelegate, sensorSerialNumber: blucon.blePeripheral.sensorSerialNumber, nonFixedSlopeEnabled: blucon.blePeripheral.nonFixedSlopeEnabled), at: index) + + // if bluconType is of type CGM, then assign the address to currentCgmTransmitterAddress, there shouldn't be any other bluetoothPeripherals of type .CGM with shouldconnect = true + if bluetoothPeripheralType.category() == .CGM { + currentCgmTransmitterAddress = blePeripheral.address + } + + } + + } else { + + // bluetoothTransmitters array (which should have the same number of elements as bluetoothPeripherals) needs to have an empty row for the transmitter + bluetoothTransmitters.insert(nil, at: index) + + } + + } + } + + } + + } + + } + // MARK:- override observe function override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {