Skip to content

Commit

Permalink
Libre 2 NFC re-write
Browse files Browse the repository at this point in the history
- rewrite of NFC scan workflow
- initial scan of NFC-enabled sensors will now *not* initial BLE scanning
- NFC scan logic redone to perform several loops until a successful NFC scan with valid data is hopefully performed
- UI updated to inform user of scan attempts and give much clearer alert messages.
- haptic feedback (vibrations/noise) added to guide user during scan cycles
- a failed NFC scan will now inform the user and offer to immediately scan again. If the user declines the transmitter will be disconnected (if existing) or removed (if adding new)
- BLE Peripheral view should show correct status in connection row if pending NFC scan to continue
- once a valid NFC scan has been performed, the user will be informed and the superclass will be told to start BLE scanning to find the sensor/transmitter. This will avoid trying to connect before a valid NFC is performed
- the scanned sensor serial number is passed back through the delegate to update the expected device name at the same time as BLE scanning is started. This prevents the app from connecting to a different transmitter (i.e. Libre 2).
- when a Libre 2 sensor is disconnected, the active sensor is set to nil. This prevents the countdown from staying on screen with no valid sensor connected.
- as above but the libre1DerivedParameters are also set to nil to prevent an issue that can show very high readings when initially connecting to a new sensor
- related alert messages changed to improve clarity
- translations added for EN/NL/ES/FR/DE/PT/SV
  • Loading branch information
paulplant committed Jan 4, 2023
1 parent ba90cbc commit 603db60
Show file tree
Hide file tree
Showing 73 changed files with 1,079 additions and 582 deletions.
15 changes: 15 additions & 0 deletions xdrip/BluetoothPeripheral/Generic/BluetoothPeripheralType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,19 @@ enum BluetoothPeripheralType: String, CaseIterable {

}

/// needs an NFC scan before connecting via BLE, or not
func needsNFCScanToConnect() -> Bool {

switch self {

case .M5StackType, .M5StickCType, .DexcomG4Type, .DexcomType, .BubbleType, .MiaoMiaoType, .WatlaaType, .BluconType, .BlueReaderType, .DropletType , .GNSentryType, .AtomType:
return false

case .Libre2Type:
return true

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ class CGMLibre2Transmitter:BluetoothTransmitter, CGMTransmitter {
init(address:String?, name: String?, bluetoothTransmitterDelegate: BluetoothTransmitterDelegate, cGMLibre2TransmitterDelegate : CGMLibre2TransmitterDelegate, sensorSerialNumber:String?, cGMTransmitterDelegate:CGMTransmitterDelegate, nonFixedSlopeEnabled: Bool?, webOOPEnabled: Bool?) {

// assign addressname and name or expected devicename
var newAddressAndName:BluetoothTransmitter.DeviceAddressAndName = BluetoothTransmitter.DeviceAddressAndName.notYetConnected(expectedName: "abbott")
// (actually this now isn't really necessary as for new devices, sensorSerialNumber will be nil and we'll update the superclass expectedName anyway after the NFC scan via the delegate)
var newAddressAndName:BluetoothTransmitter.DeviceAddressAndName = BluetoothTransmitter.DeviceAddressAndName.notYetConnected(expectedName: "ABBOTT" + (sensorSerialNumber ?? ""))

if let address = address {
newAddressAndName = BluetoothTransmitter.DeviceAddressAndName.alreadyConnectedBefore(address: address, name: name)
newAddressAndName = BluetoothTransmitter.DeviceAddressAndName.alreadyConnectedBefore(address: address, name: "ABBOTT" + (sensorSerialNumber ?? ""))
}

// initialize sensorSerialNumber
Expand Down Expand Up @@ -131,15 +133,14 @@ class CGMLibre2Transmitter:BluetoothTransmitter, CGMTransmitter {

}


} else {

bluetoothTransmitterDelegate?.error(message: TextsLibreNFC.deviceMustSupportIOS14)

}

// start the bluetooth scanning
return super.startScanning()
// start the NFC scan (not BLE scanning)
return .nfcScanNeeded

}

Expand Down Expand Up @@ -376,7 +377,7 @@ class CGMLibre2Transmitter:BluetoothTransmitter, CGMTransmitter {
// MARK: - LibreNFCDelegate functions

extension CGMLibre2Transmitter: LibreNFCDelegate {

func received(fram: Data) {

trace("received fram : %{public}@", log: log, category: ConstantsLog.categoryCGMLibre2, type: .info, fram.toHexString())
Expand All @@ -394,6 +395,7 @@ extension CGMLibre2Transmitter: LibreNFCDelegate {
// we have all date to create libre1DerivedAlgorithmParameters
UserDefaults.standard.libre1DerivedAlgorithmParameters = Libre1DerivedAlgorithmParameters(bytes: framCopy, serialNumber: serialNumber, libreSensorType: libreSensorType)


}

}
Expand Down Expand Up @@ -458,4 +460,44 @@ extension CGMLibre2Transmitter: LibreNFCDelegate {

}

func nfcScanResult(successful: Bool) {

if successful {

trace("received NFC scan result from NFC with result successful", log: log, category: ConstantsLog.categoryCGMLibre2, type: .info)

// only process if userdefaults needs changing to true to avoid triggering the observer unnecessarily
if !UserDefaults.standard.nfcScanSuccessful {

UserDefaults.standard.nfcScanSuccessful = true

}

} else {

trace("received NFC scan result from NFC with result unsuccessful", log: log, category: ConstantsLog.categoryCGMLibre2, type: .info)

// only process if userdefaults needs changing to true to avoid triggering the observer unnecessarily
if !UserDefaults.standard.nfcScanFailed {

UserDefaults.standard.nfcScanFailed = true

}

}

}

func startBLEScanning() {

_ = super.startScanning()

}

func nfcScanSerialNumber(sensorSerialNumber: String) {

self.updateExpectedDeviceName(sensorSerialNumber: sensorSerialNumber)

}

}
Loading

0 comments on commit 603db60

Please sign in to comment.