JohanDegraeve / xdripswift

xdrip for iOS, written in Swift
GNU General Public License v3.0
315 stars 310 forks source link

Dexcom connection loops and potential cause for battery drains #494

Open LiroyvH opened 6 months ago

LiroyvH commented 6 months ago

Logging it on GitHub as well so it's tracked and more people could weigh in if they like: several reports have been submitted, notably from the Anubis community - but also from ONE and G6 users, that their transmitters' batteries are drained way faster than expected when using xDrip4iOS. Official transmitters seem to die in about 45-60 days and Anubii with a larger batteries in 90-110 days or earlier. (Note: Anubis with a proper battery typically easily last double that.)

I've spent quite some time looking in to possible causes (including some time spent with Joshua from the Anubis project, whom I'd like to thank for his insights on the Dexcom firmware), and it keeps coming back to a problem I've mentioned on several occasions over the years where the transmitter goes in to a connection loop with xDrip4iOS. (Eg: you see it connect, disconnect, reconnect, disconnect, reconnect, disconnect and so on and on.)

From what I can see, but do correct me if I'm wrong and/or if the log output is deceiving, the problem lies with xDrip4iOS attempting to reconnect to the transmitter without an apparent reason. When xDrip4iOS initiates a disconnect, it also immediately attempts to reconnect. The Dexcom transmitter will still be awake* and responds to our request to connect. However, as xDrip4iOS then blocks the connection because "last connection was less than x seconds ago": the transmitter doesn't get anything from us. The connection times out after 10 seconds, after which the Dexcom disconnects again. However, as soon as that happens: xDrip4iOS once again tries to connect to the transmitter as indicated in the logs ("Trying to reconnect..."). Once again, the Dexcom transmitter allows this connection but subsequently again xDrip4iOS will not actually send anything to the transmitter because the last connection was less than x ago. After a while of this going on and on, the Dexcom transmitter will give up and phones in again at the original timestamp +5 minutes to send its new readings.

The problem is that this cannot always be reproduced. It's unclear to me why xDrip4iOS sometimes tries to reconnect and sometimes doesn't. It might also be the order of the slots in which it's connecting that might make a difference, but either way: xDrip isn't supposed to try to reconnect, especially not when xDrip itself sent the command to disconnect in the first place. I noticed today that during warm-up a similar issue occurs, but that has different log-output that I haven't analyzed yet. Did also send it to Paul.

I think the underlying issue must still lie within the original FireFly implementation where this behavior was noted, but I think was essentially "dirty patched". (Ignoring the transmitter, which inadvertently keeps it awake for 10 seconds until it times out. And then we ask it to connect again. It's like prank calling someone and every time they pick up you don't say anything.)

Here's an excerpt from the logfiles what that looks like:

2023-11-30 11:58:26.4810 4.12.0 4190 BlueToothTransmitter               error: The specified device has disconnected from us.
2023-11-30 11:58:26.4940 4.12.0 4190 BlueToothTransmitter               Will try to reconnect
2023-11-30 12:02:12.5240 4.12.0 4190 BlueToothTransmitter           connected to peripheral with name DexcomTS
2023-11-30 12:02:12.5830 4.12.0 4190 BlueToothTransmitter           didDiscoverServices for peripheral with name DexcomTS

And a longer look of such a cycle (including when it phones in again to update the values and then it goes straight back in to a loop):

2023-11-30 11:52:22.8360 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:52:22.8410 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:52:22.8490 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:52:22.8540 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:52:22.8620 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:52:22.8660 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:52:23.3290 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:52:38.1970 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:52:38.2040 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:52:38.2240 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:52:38.2280 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:52:38.2380 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:52:38.2410 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:52:38.6600 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:52:53.4010 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:52:53.4060 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:52:53.4150 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:52:53.4180 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:52:53.4250 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:52:53.4290 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:52:53.9860 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:53:08.7430 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:53:08.7640 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:53:08.7700 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:53:08.7720 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:53:08.7750 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:53:08.7800 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:53:09.2250 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:53:23.9850 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:53:23.9890 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:53:23.9960 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:53:24.0000 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:53:24.0030 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:53:24.0050 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:53:24.4660 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:53:39.2100 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:53:39.2120 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:53:39.2180 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:53:39.2220 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:53:39.2280 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:53:39.2310 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:53:39.7050 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:53:54.4550 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:53:54.4590 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:53:54.4640 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:53:54.4710 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:53:54.4740 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:53:54.4810 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:53:55.0340 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:54:09.7850 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:54:09.7910 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:54:09.7950 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:54:09.7980 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:54:09.8060 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:54:09.8110 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:54:10.3060 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:54:25.0260 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:54:25.0320 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:54:25.0370 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:54:25.0430 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:54:25.0500 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:54:25.0520 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:54:25.6060 4.12.0 4190 BlueToothTransmitter connected to peripheral with name DexcomTS 2023-11-30 11:54:25.7480 4.12.0 4190 BlueToothTransmitter didDiscoverServices for peripheral with name DexcomTS 2023-11-30 11:54:25.7520 4.12.0 4190 BlueToothTransmitter Call discovercharacteristics for service with uuid F8083532-849E-531C-C594-30F1F86A4EA5 2023-11-30 11:54:25.7560 4.12.0 4190 CGMG5 didDiscoverCharacteristicsFor 2023-11-30 11:54:25.7590 4.12.0 4190 CGMG5 characteristic : Communication 2023-11-30 11:54:25.7610 4.12.0 4190 CGMG5 characteristic UUID unknown : F8083538-849E-531C-C594-30F1F86A4EA5 2023-11-30 11:54:25.7640 4.12.0 4190 CGMG5 characteristic : Write_Control 2023-11-30 11:54:25.7660 4.12.0 4190 CGMG5 characteristic : Receive_Authentication 2023-11-30 11:54:25.7680 4.12.0 4190 CGMG5 calling setNotifyValue true for characteristic Receive_Authentication 2023-11-30 11:54:25.7710 4.12.0 4190 CGMG5 characteristic : Backfill 2023-11-30 11:54:25.7730 4.12.0 4190 CGMG5 characteristic UUID unknown : F8083537-849E-531C-C594-30F1F86A4EA5 2023-11-30 11:54:25.8090 4.12.0 4190 CGMG5 in peripheralDidUpdateNotificationStateFor. characteristic = Receive_Authentication 2023-11-30 11:54:25.8110 4.12.0 4190 CGMG5 characteristic : Receive_Authentication 2023-11-30 11:54:25.8160 4.12.0 4190 CGMG5 sending authMessage with data 01146a7282fd9d4c0f02 2023-11-30 11:54:25.8200 4.12.0 4190 BlueToothTransmitter in writeDataToPeripheral, for peripheral with name DexcomTS, for characteristic F8083535-849E-531C-C594-30F1F86A4EA5, data = 01146a7282fd9d4c0f02 2023-11-30 11:54:25.9280 4.12.0 4190 BlueToothTransmitter in didWriteValueFor. Characteristic F8083535-849E-531C-C594-30F1F86A4EA5 2023-11-30 11:54:26.0640 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:54:26.0670 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:54:26.0690 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:54:26.0710 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:54:26.0780 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:54:26.0810 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:57:11.8910 4.12.0 4190 BlueToothTransmitter connected to peripheral with name DexcomTS 2023-11-30 11:57:11.9840 4.12.0 4190 BlueToothTransmitter didDiscoverServices for peripheral with name DexcomTS 2023-11-30 11:57:11.9880 4.12.0 4190 BlueToothTransmitter Call discovercharacteristics for service with uuid F8083532-849E-531C-C594-30F1F86A4EA5 2023-11-30 11:57:11.9930 4.12.0 4190 CGMG5 didDiscoverCharacteristicsFor 2023-11-30 11:57:11.9960 4.12.0 4190 CGMG5 characteristic : Communication 2023-11-30 11:57:11.9980 4.12.0 4190 CGMG5 characteristic UUID unknown : F8083538-849E-531C-C594-30F1F86A4EA5 2023-11-30 11:57:11.9990 4.12.0 4190 CGMG5 characteristic : Write_Control 2023-11-30 11:57:12.0020 4.12.0 4190 CGMG5 characteristic : Receive_Authentication 2023-11-30 11:57:12.0060 4.12.0 4190 CGMG5 calling setNotifyValue true for characteristic Receive_Authentication 2023-11-30 11:57:12.0100 4.12.0 4190 CGMG5 characteristic : Backfill 2023-11-30 11:57:12.0130 4.12.0 4190 CGMG5 characteristic UUID unknown : F8083537-849E-531C-C594-30F1F86A4EA5 2023-11-30 11:57:12.0700 4.12.0 4190 CGMG5 in peripheralDidUpdateNotificationStateFor. characteristic = Receive_Authentication 2023-11-30 11:57:12.0740 4.12.0 4190 CGMG5 characteristic : Receive_Authentication 2023-11-30 11:57:12.0780 4.12.0 4190 CGMG5 sending authMessage with data 01175c1ab0b20e4a7802 2023-11-30 11:57:12.0810 4.12.0 4190 BlueToothTransmitter in writeDataToPeripheral, for peripheral with name DexcomTS, for characteristic F8083535-849E-531C-C594-30F1F86A4EA5, data = 01175c1ab0b20e4a7802 2023-11-30 11:57:12.1300 4.12.0 4190 BlueToothTransmitter in didWriteValueFor. Characteristic F8083535-849E-531C-C594-30F1F86A4EA5 2023-11-30 11:57:12.1320 4.12.0 4190 BlueToothTransmitter in peripheralDidUpdateValueFor, data = 037b70caf21993d9bd6da888f17ebf29c3 2023-11-30 11:57:12.1340 4.12.0 4190 CGMG5 in peripheralDidUpdateValueFor, characteristic uuid = Receive_Authentication 2023-11-30 11:57:12.1350 4.12.0 4190 CGMG5 opcode = authRequestRx 2023-11-30 11:57:12.1390 4.12.0 4190 CGMG5 sending authChallengeTxMessage with data 048aa9c577423dedca 2023-11-30 11:57:12.1410 4.12.0 4190 BlueToothTransmitter in writeDataToPeripheral, for peripheral with name DexcomTS, for characteristic F8083535-849E-531C-C594-30F1F86A4EA5, data = 048aa9c577423dedca 2023-11-30 11:57:12.1900 4.12.0 4190 BlueToothTransmitter in didWriteValueFor. Characteristic F8083535-849E-531C-C594-30F1F86A4EA5 2023-11-30 11:57:12.1930 4.12.0 4190 BlueToothTransmitter in peripheralDidUpdateValueFor, data = 050101 2023-11-30 11:57:12.1950 4.12.0 4190 CGMG5 in peripheralDidUpdateValueFor, characteristic uuid = Receive_Authentication 2023-11-30 11:57:12.1970 4.12.0 4190 CGMG5 opcode = authChallengeRx 2023-11-30 11:57:12.2000 4.12.0 4190 CGMG5 will set notifyValue for writeControlCharacteristic to true 2023-11-30 11:57:12.2020 4.12.0 4190 BlueToothTransmitter setNotifyValue, for peripheral with name DexcomTS, setting notify for characteristic F8083534-849E-531C-C594-30F1F86A4EA5, to true 2023-11-30 11:57:12.2040 4.12.0 4190 CGMG5 will set notifyValue for backfillCharacteristic to true 2023-11-30 11:57:12.2070 4.12.0 4190 BlueToothTransmitter setNotifyValue, for peripheral with name DexcomTS, setting notify for characteristic F8083536-849E-531C-C594-30F1F86A4EA5, to true 2023-11-30 11:57:12.2500 4.12.0 4190 CGMG5 in peripheralDidUpdateNotificationStateFor. characteristic = Write_Control 2023-11-30 11:57:12.2530 4.12.0 4190 CGMG5 characteristic : Write_Control 2023-11-30 11:57:12.2560 4.12.0 4190 CGMG5 start of firefly flow 2023-11-30 11:57:12.2610 4.12.0 4190 CGMG5 sending glucoseDataTxMessage with data 305336 2023-11-30 11:57:12.2640 4.12.0 4190 BlueToothTransmitter in writeDataToPeripheral, for peripheral with name DexcomTS, for characteristic F8083534-849E-531C-C594-30F1F86A4EA5, data = 305336 2023-11-30 11:57:12.2670 4.12.0 4190 CGMG5 in fireflyMessageFlow, did send glucoseTxMessage, setting glucoseTxSent to true 2023-11-30 11:57:12.3100 4.12.0 4190 CGMG5 in peripheralDidUpdateNotificationStateFor. characteristic = Backfill 2023-11-30 11:57:12.3130 4.12.0 4190 CGMG5 characteristic : Backfill 2023-11-30 11:57:12.3700 4.12.0 4190 BlueToothTransmitter in didWriteValueFor. Characteristic F8083534-849E-531C-C594-30F1F86A4EA5 2023-11-30 11:57:12.3740 4.12.0 4190 BlueToothTransmitter in peripheralDidUpdateValueFor, data = 31005007000050a808009e0006093a2c 2023-11-30 11:57:12.3760 4.12.0 4190 CGMG5 in peripheralDidUpdateValueFor, characteristic uuid = Write_Control 2023-11-30 11:57:12.3790 4.12.0 4190 CGMG5 opcode = glucoseRx 2023-11-30 11:57:12.3830 4.12.0 4190 CGMG5 in processGlucoseDataRxMessage, received glucoseDataRxMessage, value = 158.0, algorithm status = OK / Calibrated, transmitter status = 0 2023-11-30 11:57:12.3890 4.12.0 4190 CGMG5 start of firefly flow 2023-11-30 11:57:12.3910 4.12.0 4190 CGMG5 end of firefly flow 2023-11-30 11:57:12.3930 4.12.0 4190 CGMG5 will disconnect 2023-11-30 11:57:12.3950 4.12.0 4190 BlueToothTransmitter in disconnect, disconnecting, for peripheral with name DexcomTS 2023-11-30 11:57:12.4010 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:57:12.4040 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:57:12.4070 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:57:12.4130 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:57:12.4210 4.12.0 4190 CGMG5 adding glucose data that was received in GlucoseDataRxMessage/DexcomG6GlucoseDataRxMessage, value = 158.0, date = 11:57:12 CET 2023-11-30 11:57:12.4230 4.12.0 4190 CGMG5 calling cgmTransmitterInfoReceived with 1 values 2023-11-30 11:57:12.4270 4.12.0 4190 RootView transmitterBatteryInfo not received 2023-11-30 11:57:12.4290 4.12.0 4190 RootView sensor time in days not received 2023-11-30 11:57:12.4310 4.12.0 4190 RootView glucoseData size = 1 2023-11-30 11:57:12.4360 4.12.0 4190 RootView glucoseData 0, value = 158.0, timestamp = 11:57:12 CET 2023-11-30 11:57:12.4400 4.12.0 4190 RootView calibrator = NoCalibrator 2023-11-30 11:57:12.4690 4.12.0 4190 RootView timeStampToDelete = 11:36:12 CET 2023-11-30 11:57:13.3340 4.12.0 4190 RootView new reading created, timestamp = donderdag 30 november 2023 om 11:57:12 Midden-Europese standaardtijd, calculatedValue = 158,0 2023-11-30 11:57:14.6890 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:57:22.3450 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:57:22.3620 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:57:22.3770 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:57:22.3810 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:57:22.4270 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:57:22.4370 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:57:22.9090 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:57:37.6950 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:57:37.7020 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:57:37.7220 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:57:37.7240 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:57:37.7290 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:57:37.7320 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:57:38.2410 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:57:52.9980 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:57:53.0060 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:57:53.0220 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:57:53.0250 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:57:53.0300 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:57:53.0340 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:57:53.5120 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:58:08.3560 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:58:08.3800 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:58:08.7320 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:58:09.0170 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:58:09.0740 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:58:09.1490 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 11:58:09.6680 4.12.0 4190 CGMG5 connected to peripheral with name DexcomTS, but last reading was less than 2.1 minute ago, will let the connection timeout 2023-11-30 11:58:25.1470 4.12.0 4190 BlueToothTransmitter didDisconnect peripheral with name DexcomTS 2023-11-30 11:58:25.4450 4.12.0 4190 BluetoothPeripheralManager in didDisconnectFrom 2023-11-30 11:58:26.4810 4.12.0 4190 BlueToothTransmitter error: The specified device has disconnected from us. 2023-11-30 11:58:26.4940 4.12.0 4190 BlueToothTransmitter Will try to reconnect 2023-11-30 11:58:26.8530 4.12.0 4190 CGMG5 in sendGlucoseDataToDelegate 2023-11-30 11:58:27.0280 4.12.0 4190 CGMG5 glucoseDataArray has no values 2023-11-30 12:02:12.5240 4.12.0 4190 BlueToothTransmitter connected to peripheral with name DexcomTS 2023-11-30 12:02:12.5830 4.12.0 4190 BlueToothTransmitter didDiscoverServices for peripheral with name DexcomTS etc. etc.

It looks as if we don't yet expect a disconnect or that we do ask for it to disconnect, but another part of the code thinks its by error and tries to reconnect - but is then frustrated by the 2.1 minute thing. Note: in one of the very first implementations, of which I still have the source code laying around here if anyone's interested :P, this problem is continuous.

Anyway, I don't know IF this is the cause of the reported battery drains. But it seems very likely, as such: logging it as issue here. As an extra note: this seems to occur sooner for me when xDrip4iOS is woken up from slumber. (Eg: it got suspended, but transmitter wakes it up. I'll shamelessly plug my request for full implementation of Background Processing/Activity here which could potentially be good for overall stability (also compared to Loop, iAPS and the official Dexcom-app.))

* = The transmitter is still awake, because the Dexcom keeps listening for a brief moment on all 2 (or 3 in case of Anubis) connection slots to see if the second device (reader/pump) wants a connection as well or not. Instead, xDrip4iOS asks to be connected again - and it honors that request.

JohanDegraeve commented 6 months ago

this is reported already long time ago, with Dexcom G5 and Spike. xDrip4iOS works in the same way as Spike, except that it doesn't use the sound system to avoid that the app gets suspended

When this was reported for Spike, I did some tests where I would not explicitly disconnect, but let the Dexcom time out the connection, to see if that changed anything in the 'battery drain' (which I never experienced by the way, but I never used the Dexcom official app so I couldn't really compare). you could try to comment out this line https://github.com/JohanDegraeve/xdripswift/blob/master/xdrip/BluetoothTransmitter/CGM/Dexcom/G5/CGMG5Transmitter.swift#L1676 and 'see' if that helps. I tried that with Spike but couldn't notice any difference and I remember this was tested by other people also.

"the problem lies with xDrip4iOS attempting to reconnect to the transmitter without an apparent reason." => xDrip4iOS must try to reconnect after a disconnect, no matter the cause of the disconnect. Otherwise it would never be able to reconnect. You could set a timer eg of 4 minutes after which you try to reconnect. But as you probably now timers don't work for apps in the background except if you play some kind of a sound every 5 seconds which keep the app alive (I think you know what I mean).

"_However, as xDrip4iOS then blocks the connection because "last connection was less than x seconds ago": the transmitter doesn't get anything from us.__" That's not correct. In this case (ie reconnect less than 2.1 minutes after the last reading), the xDrip4iOS connects and doesn't disconnect. It let's the Dexcom do the timeout. You can see this here : https://github.com/JohanDegraeve/xdripswift/blob/master/xdrip/BluetoothTransmitter/CGM/Dexcom/G5/CGMG5Transmitter.swift#L691 You see this also in the logs at 2023-11-30 11:52:23.3290 Dexcom itself is disconnecting at 2023-11-30 11:52:38. That's 15 seconds later. It's the Dexcom that takes the initiative to disconnect. xDrip4iOS must try to reconnect.. this goes on for a while. With Dexcom G5 this was only for about 10 seconds if I remember well. For the newer Dexcom's it goes on longer. Do I understand correctly that this goes on for ever? in that case not only the battery of the Dexcom is draining but also xDrip4iOS battery. What is the battery decrease of your iPhone if you sleep with it uncharged? (I mean no other usage). It should only be about 20 to 30% even if you're running xDrip4iOS, Loop and maybe even the official Dexcom app in parallel and if you sleep a normal night say 8 hours.

"but another part of the code thinks its by error and tries to reconnect" that's not an error, it's by design. It's indeed another piece of code : https://github.com/JohanDegraeve/xdripswift/blob/master/xdrip/BluetoothTransmitter/Generic/BluetoothTransmitter.swift#L439

This code is generic for all types of bluetoothtransmitters (Libre 2, MiaoMiao, Dexcom, M5Stack, ..). It may look strange that xDrip4iOS explicitly disconnects, knowing that the other part of the code will immediately try to reconnect, and that's probably not necessary. But even if you remove that disconnect, Dexcom will timeout after 15 seconds, and it will immediately reconnect. That's the behaviour of Dexcom.

"'ll shamelessly plug my request for full implementation of Background Processing/Activity here which could potentially be good for overall stability (also compared to Loop, iAPS and the official Dexcom-app.)" I think you want to say that xDrip4iOS needs a way to avoid that it get's suspended by iOS (here an explanation of suspended state : https://developer.apple.com/documentation/watchkit/life_cycles/handling_common_state_transitions) The only way to achieve this is the sound system (ie playing a non -hearable sound of a few milliseconds every 2 to 5 seconds) No way that Loop and official Dexcom app are using such mechanism. (I don't know about iAPS, iAPS was originally freeaps, freeaps has the word 'free' which I interpret as 'I do what I want', however I actually don't believe iAPS is doing it differently than all the other apps)

Interesting to read this : https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW1

anyway that all doesn't solve the battery drain problem if xDrip4iOS is used with Dexcom, if it exists. I actually also think that it's not correctly developed, the disconnect/reconnect loops should be avoided. I tried to understand how Loop does it but it's quite difficult code to reverse engineer.

joshbohmer commented 5 months ago

"will timeout after 15 seconds, and it will immediately reconnect. That's the behaviour of Dexcom."

I think the problem is G6 needs to stay awake for a period of time (15 seconds) after the last disconnect in case a 2nd device needs to connect. Because xDrip4iOS immediately initiates a reconnect this timer constantly gets reset which prevents the nrf52 from entering sleep mode. It seems to have a 2 minute hard limit also, whereby it forcibly enters sleep if it's hasn't been able

As a test... if we introduce a 20s delay in the disconnect -> reconnect loop that should give G6 enough time to sleep correctly.

I can confirm the Dexcom battery drain issue is a thing. With Anubis we're seeing a very regular 60 day battery problem exclusively with xDrip4iOS.

JohanDegraeve commented 5 months ago

I can confirm the Dexcom battery drain issue is a thing. With Anubis we're seeing a very regular 60 day battery problem exclusively with xDrip4iOS.

with which app do you not see this problem? Do you use the official Dexcom app with Anubis?

JohanDegraeve commented 5 months ago

As a test... if we introduce a 20s delay in the disconnect -> reconnect loop that should give G6 enough time to sleep correctly.

did you test that with xDrip4iOS while running in the background and if so, it does reconnect successfully 4minutes and 4 seconds later? (meaning when Dex has a new reading). If that works that could be the solution

JohanDegraeve commented 5 months ago

I think the problem is G6 needs to stay awake for a period of time (15 seconds) after the last disconnect in case a 2nd device needs to connect.

Indeed, that's why Dexcom is different than other devices like Libre and MM or Bubble, those devices stay connected

joshbohmer commented 5 months ago

I can confirm the Dexcom battery drain issue is a thing. With Anubis we're seeing a very regular 60 day battery problem exclusively with xDrip4iOS.

with which app do you not see this problem? Do you use the official Dexcom app with Anubis?

All of them. I do the Anubis transmitters and support 1000's of users (iOS & Android) through that work

JohanDegraeve commented 5 months ago

All of them

what is all of them? is there another open source ios app that you use for that (other than Loop)

joshbohmer commented 5 months ago

All of them

what is all of them? is there another open source ios app that you use for that (other than Loop)

What I mean is across ALL Anubis users, which include all Official & Open Source apps.

Since we warranty Anubis exactly like Dexcom out to 100 days, when a battery dies early we hear about it. In almost every case that happens, it's been activated around 60 days and xDrip4iOS/Shuggah was used. Every other app lasts to 200+ days.

JohanDegraeve commented 5 months ago

I plan to switch to Dexcom (the real Dexcom) (Libre 3 is awful) in the coming 2 weeks, I'll try out the 20 second delay. But I think adding a delay of 20 second before trying to reconnect will simple put xDrip4iOS to sleep (app will be suspended by iOS) and it will not be woken up as timers will not be fired. That's why it would be interesting to see how other apps are doing this. Problem is Loop is so complex that I don't succeed to figure out how they do it. That's why I was wondering if you have another open source app.

LiroyvH commented 5 months ago

Hi Johan,

First of all - thanks for taking the time to look in to this elaborately, much appreciated. :) And congratulations on moving to a proper CGM. (Sorry, couldn't resist. :P) Why not use the Anubis? Has some nice extra convenience features! :) Also thanks to @joshbohmer for weighing in.

Just to note: I can confirm that the Dexcom battery drain/connection loops only occur with xDrip4iOS. It does not occur on the official Dexcom app (iOS) nor can I get it to that state on Android with xDrip+. It's something specifically with the instant reconnection. But what's the mindf- for me is that it doesn't always happen. (However, looking at my logs: it does happen far more often than I initially thought.) Amanda Vanosdol has mentioned that she(/her clients) has also seen the problem with xDrip4iOS + rebatteried Dexcom G5's - which seems to make sense when I read your explanation that the foundation is the same for all those transmitters. :)

xDrip4iOS must try to reconnect after a disconnect, no matter the cause of the disconnect. Otherwise it would never be able to reconnect. You could set a timer eg of 4 minutes after which you try to reconnect. But as you probably now timers don't work for apps in the background except if you play some kind of a sound every 5 seconds which keep the app alive (I think you know what I mean).

If I understand correctly, it could work like that when using the Background Modes as described in the link you provided: https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html.

Currently, xDrip4iOS doesn't implement this as far as I can see. I think refactoring it to leverage these modes may also solve the issue where xDrip4iOS won't always restore itself until the user manually opens it up again when the app had become unloaded because of memory constraints. (Whilst that's another story than the battery drain, may be worth mentioning why it might be a bonus: Currently, if the app is unloaded due to memory constraints whilst it seemingly hasn't started asking iOS to connect again: we never connect anymore (as described in the introduction of that article). So this could perhaps solve two problems at once?

Now this is anecdotal as N=1, but I'm one of the people that frequently get's hit with this issue as I use the phone camera a lot which slurps resources that my 13 Mini simply doesn't have. But: iAPS, the official Dexcom app and Loop all are restored within +/- 5 minutes; even if the app clearly got killed. The only app that won't always wake back up unless it's manually opened again for me is xDrip4iOS - even in master mode. I think it's related to this, as the only difference I was able to find is all those app employ background refresh modes whereas xDrip4iOS doesn't do that.)

It's the Dexcom that takes the initiative to disconnect.

Ah I thought we did by sending 0x09 + CRC16 (disconnectTx) as defined in https://github.com/JohanDegraeve/xdripswift/blob/master/xdrip/BluetoothTransmitter/CGM/Dexcom/Generic/DexcomTransmitterOpCode.swift#L22 However, running a quick search on the codebase: we don't use that. I've asked @joshbohmer about that, but it seems it's not really relevant to the problem at hand.

xDrip4iOS must try to reconnect.. this goes on for a while. With Dexcom G5 this was only for about 10 seconds if I remember well. For the newer Dexcom's it goes on longer. Do I understand correctly that this goes on for ever? in that case not only the battery of the Dexcom is draining but also xDrip4iOS battery.

Yes, what seems to happen is when we attempt to reconnect: we do so instantly after disconnect. But doing that will be in the window @joshbohmer described wherein the Dexcom is still listening for other devices. (Further info: Dexcom has two device slots, only one can connect at the same time. So when "we" disconnect, it stays active for a bit to wait for another device to connect. Instead, we connect again. The slot isn't chosen until after the connection has been established, so the Dexcom can't "stop listening on that slot" either; it's just open for connection.)

This goes on for about 2 minutes (see logs, though if you want I have far more extensive logs of these occurrences) I think and then the Dexcom gives up and stops phoning in. Probably the hard-limit guardrail within the Dexcom that Joshua described above.

But even if you remove that disconnect, Dexcom will timeout after 15 seconds, and it will immediately reconnect. That's the behaviour of Dexcom.

I think we're causing that behavior as also mentioned by Joshua. We ask iOS to immediately reconnect to the Dexcom. But as the Dexcom can still be listening at that moment: it actually does reconnect. This then triggers xDrip4iOS to be woken up again, if it even got suspended at all, and then we start ignoring the transmitter. The transmitter gives up after 10 seconds, but what happens next is xDrip4iOS will then once again tell iOS to connect to it again. As the Dexcom is still awake, it once more obliges and connects to us and is once more frustrated by not hearing anything from us for 10 seconds. It disconnects again, but we ask....... and so on and on. :P Until finally after 2 minutes or so, the Dexcom is sick of the annoying kids prank calling him and stops listening entirely. As I'm sure Dexcom isn't the only BLE-device listening in this way, I can't escape the notion that - especially after reading that doc you linked - iOS has provisions for exactly that situation and we should somehow even in the background/suspended state be able to tell when iOS should reconnect (not instantly after Dexcom disconnects, but after x time has passed after that.).

I think you want to say that xDrip4iOS needs a way to avoid that it get's suspended by iOS (here an explanation of suspended state : https://developer.apple.com/documentation/watchkit/life_cycles/handling_common_state_transitions) The only way to achieve this is the sound system (ie playing a non -hearable sound of a few milliseconds every 2 to 5 seconds)

No, sorry. I should've been specific. I meant the Background Refresh modes that are employed by Loop, iAPS, Dexcom official, etc. They implement background modes described in the Developers docs you linked and I repeated above. Specifically, the UIBackgroundModes used by those apps are:

bluetooth-central
bluetooth-peripheral
processing

Which helps with controlling BLE connections in the background (even if suspended) and waking up even after having been killed entirely to free up resources required elsewhere.

It's my theory, but as you know I'm not a coder and am just theorizing based on what I see xDrip does and what documentation says (plus some help from @joshbohmer), that this would a.) offer a way for xDrip4iOS to solve the reconnection problem as we can probably make iOS wait X seconds before actually attempting to connect again without the need to do anything "illegal", b.) increase performance: less battery drain on the phone and Dexcom transmitters if these loops never occur. c.) increase stability: heavy apps hogging up resources will cause less trouble and the app will recover more easily.

At least, I hope so. But I'm going farrrrrrr beyond what is my comfort zone in terms of coding (and that zone is very very small :P) and handling BLE-devices, so maybe it's all BS. But I like to think it sounds logical and feasible based on the developer documentation and observing the behavior of xDrip4iOS and other apps communicating with BLE devices. :P

Greetings! :) Enjoy the cold weather... :/ It's snowing here.

JohanDegraeve commented 5 months ago

well I won't reply it all. It's funny to see all apps have 'back ground processing' enabled while xDrip4iOS has not :)

If I go to my iOS settings (background refresh, in Dutch "ververs op achtergrond") i have about 20 apps where I can enable background refresh, but I've disabled them all, and all apps keep working.

Libre 2 and 3 also don't use that setting by the way (you will not find it in the list) One exception is LibreLinkUp : that app needs background refresh enabled because a remote server is sending push notifications to the app, asking the app to wake up. Which is exactly what it's used for. Try it out and go through those iOS settings, refresh background, disable it for all your apps ...

There's also a lot of misunderstanding about apps behaviour in the background. Apps don't get 'unloaded' because of memory constraints. Apps that go to the background (because you lock your phone or press the home button or open another app), and that have nothing else to do (like playing music or tracking your location with gps or ongoing communication with a BLE device), they will be pushed by iOS to 'suspend' mode within seconds. In case of all those bluetooth apps (dexcom, libre, xdrip, ...) : they will be rewoken up as soon as the device either disconnects, connects, or sends data. There's a prerequisite for that, the app must really tell to iOS that it wants to reconnect after a disconnect. (Note also that Spike was an exception, Spike wasn't forced woken up by iOS in those cases. Probably because Spike is using the Adobe action script framework - that's why Spike needed two sound wake up system .. i think you know what I mean)

Also this issue was about 'battery drain' for the transmitter. Now you talk about xDrip4iOS not being able to wake up unless you bring the app to the foreground?

PS please don't reply anymore. As as long as I don't have a Dexcom transmitter and sensor I can't do anything but keep on debating. I have an appointment next week at hospital and should get the Dexcom (I don't know actually if it's G6 or G7 here in Belgium)

It's cold indeed :) but it's better than all the rain we had last months

joshbohmer commented 5 months ago

@JohanDegraeve I can organise to send you a couple Anubis & a G6 Sensor Simulator to help development. Sorry, I thought you already had one.

"Also, this issue was about 'battery drain' for the transmitter. Now you talk about xDrip4iOS not being able to wake up unless you bring the app to the foreground?"

What's happening is Dexcom transmitters can't enter sleep mode fast enough because of the disconnect -> reconnect loop. The transmitter stays awake for x seconds after the last disconnect to give other devices an opportunity to download data. With how xDrip4iOS handles the BLE connection, the transmitter is kept awake each 5 minute cycle for up to 2 minutes (it's hard cut off) instead of the normal ~15 seconds. This longer than normal time with the nRF52 radio active is killing batteries early

The background handling conversation is an attempted to remove the reconnect loop requirement and let the transmitter sleep the radio sooner

LiroyvH commented 5 months ago

I thought you had an Anubis + sensor simulator? If you don't, we should get them to you as they make playing around with it farrrrrr easier and ensure no sensors are wasted. (The simulator can also be used with Dexcom ONE transmitters.) -edit- Ha, ninja'd by @joshbohmer :)

Last input then: according to both Apple and Dexcom, background refresh is critical (and the behaviour of apps with it implemented or enabled rather than disabled is, on my end, definitely far more reliable). (And with "unloaded" I was referring to this statement by Apple: "At some point, the system may need to terminate your app to free up memory for the current foreground app—causing any active or pending connections to be lost, for instance." and then goes on to state how this is solved with background modes).

If you don't want to implement it: ok, it's your call. :) I think it'd be a waste not to leverage these features, but if all problems can be sorted without it: all good, that's what matters in the end. :D

Thanks in advance for fixing it! :)

P.S. the primary concern is indeed the rapid battery drain on Dexcom transmitters - that's a critical problem. The manual intervention after termination was solely mentioned as a bonus issue that might be solved.

JohanDegraeve commented 5 months ago

@JohanDegraeve I can organise to send you a couple Anubis & a G6 Sensor Simulator to help development. Sorry, I thought you already had one.

"Also, this issue was about 'battery drain' for the transmitter. Now you talk about xDrip4iOS not being able to wake up unless you bring the app to the foreground?"

What's happening is Dexcom transmitters can't enter sleep mode fast enough because of the disconnect -> reconnect loop. The transmitter stays awake for x seconds after the last disconnect to give other devices an opportunity to download data. With how xDrip4iOS handles the BLE connection, the transmitter is kept awake each 5 minute cycle for up to 2 minutes (it's hard cut off) instead of the normal ~15 seconds. This longer than normal time with the nRF52 radio active is killing batteries early

Yes that's probably all correct. but the way to solve it as discussed above is not what will fix it. I mean it's not about the background processing settings.

I've seen in the Loop code (but can't find it back anymore) that they wait 2 seconds before attempting to reconnect, the comment added is very interesting https://github.com/LoopKit/G7SensorKit/blob/main/G7SensorKit/G7CGMManager/G7BluetoothManager.swift#L218 (they call it scanForPeripheral but in the end they also just reconnect to an already known device)

I had already seen this before but didn't want to implement it because I think it's very risky as iOS in the beginning it takes more seconds before it suspends the app (in the beginning I mean right after it's launched), but after a while the suspension goes faster and faster. The follower mode for xDrip4iOS works with the sound every 5 seconds and sometimes that's even too short.

I was aware about all the communication ongoing with Dexcom, and indeed with G5 it was much shorter (I think 15 seconds as you say), and G6 or G7 it's much longer. I wans't sure actually if it would drain the battery or not.

JohanDegraeve commented 5 months ago

I thought you had an Anubis + sensor simulator? If you don't, we should get them to you as they make playing around with it farrrrrr easier and ensure no sensors are wasted. (The simulator can also be used with Dexcom ONE transmitters.) -edit- Ha, ninja'd by @joshbohmer :)

Last input then: according to both Apple and Dexcom, background refresh is critical (and the behaviour of apps with it implemented or enabled rather than disabled is, on my end, definitely far more reliable). (And with "unloaded" I was referring to this statement by Apple: "At some point, the system may need to terminate your app to free up memory for the current foreground app—causing any active or pending connections to be lost, for instance." and then goes on to state how this is solved with background modes).

If you don't want to implement it: ok, it's your call. :) I think it'd be a waste not to leverage these features, but if all problems can be sorted without it: all good, that's what matters in the end. :D

Thanks in advance for fixing it! :)

P.S. the primary concern is indeed the rapid battery drain on Dexcom transmitters - that's a critical problem. The manual intervention after termination was solely mentioned as a bonus issue that might be solved.

sure also Apple says background processing is critical?

"At some point, the system may need to terminate your app to free up memory for the current foreground app—causing any active or pending connections to be lost, for instance." and then goes on to state how this is solved with background modes). =>can you show me where apple says "and then goes on to state how this is solved with background modes)"

There's another interesting feature that makes sure the app gets relaunched when necessary, it's this https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerdelegate/1518819-centralmanager I tested this : after 3 readings, make a divison by 0. This crashes the app, but depending on the device you're using, the app relaunches. In case of for instande MM, that's within a few milliseconds. Because due the the crash, the MM is losing connection, it immediately starts advertising again, iOS detects this and relaunches the app. In case of Libre this may take a bit longer, because Libre always needs a minute before restarting advertising. Dexcom it depends if it went to sleep or not

"definitely far more reliable" ==> it should be 100% reliable, there's nothing more than 100 :)

"I think it'd be a waste not to leverage these features" => they cause confusing and save work for the moderators on the Facebook group. xDrip4iOS moderators can't ask whether people have enabled the background mode in the iOS settings, because it's not there.

"I thought you had an Anubis ", sorry I may have one but can't find it back.

Actually I don't believe that all that communication has a significant impact on the battery. I think some transmitters just don't last long enough (especially Anubis?) and then developers start looking at the xDrip4iOS logs and see all that communication ongoing so you think this is the cause. It happened before with Spike also (Spike used the same mechanisms as xDrip4iOS just not the restorestate - I know background refresh was enabled in Spike but that doesn't make any difference)

LiroyvH commented 5 months ago

can you show me where apple says "and then goes on to state how this is solved with background modes)"

Just continue reading the link you gave. :) It opens with the terminator quote and then goes on to explain the options to prevent it from happening. Probably the parts from "Core Bluetooth Background Execution Modes" and "The bluetooth-central Background Execution Mode" bits are most relevant and explains why it prevents the quoted scenario from occurring.

This should allow for better connection management even if the app gets suspended or even terminated. (By iOS anyway. User invoked termination is probably different.)

they cause confusing and save work for the moderators on the Facebook group.

If a handful of users randomly start flipping switches of features they shouldn’t be disabling (without checking), that’s their fault tbh. It can be noted in the docs. And it’s not like we don't have to ask them about other bad choices all day anyway; such as enabling Bluetooth on LibreLink which is located in the exact same settings as where Background Refresh would be. ;)

I think some transmitters just don't last long enough (especially Anubis?) and then developers start looking at the xDrip4iOS logs and see all that communication ongoing so you think this is the cause.

No it’s not like that at all and I’m sad to hear that you think I’d say something like this if I hadn’t properly looked in to it and hadn’t tried eliminating other potential culprits. Heck, I even tried pinning it down to older/cheaper iPhone's but then got logs sent to me from people on the 15 Pro.

Background to this problem: Multiple people (and not just a handful) are reporting severe battery drains on their transmitters when using xDrip4iOS, myself included. These problems immediately disappear when xDrip4iOS is taken out of the equation. Whether it’s replaced with the official G6 app or xDrip+ doesn’t matter - the problem stops when xDrip4iOS isn’t used anymore and returns when xDrip4iOS is being used again.

It’s been several months of observing this behaviour between various groups of people, including the Anubis team and the Designs & Replacement team (whom are the first groups of people to detect large amounts of complaints), to establish the culprit. Subsequently, we (me and @joshbohmer) started looking in to what could be causing this problem so that a substantiated problem report could be submitted. The connection loops are a very fine candidate, because the dexcom is supposed to be awake for +/- 10-20 seconds and then go to sleep; but instead it is kept awake and keeps broadcasting for nearly 2 minutes because of xDrip4iOS. Sometimes for days on end.

Adding background refresh leveraging Apple's described framework was just a suggestion for a potential route to solve the problem btw, no idea if there are more roads that'll lead to Rome or if it's the road at all; I'm just going by what the docs say. :)

If you have another idea of what could cause the problem: ok, this was simply the best theory we could come up with. It makes sense and it’s months of looking in to it - not just some random users’ j’accuse. ;)

Maybe it isn’t xDrip4iOS, maybe it is but it’s another problem than what we found. But it cannot be denied that it’s extremely suspicious that every time these issues occur: they’re fixed when people switch away from xDrip4iOS.

And lest not forget… There were ONE users complaining about this phenomenon as well, but we dismissed it because one or two experienced it with ONE and naturally complaints ceased completely when they were all forced to let the Dexcom app take the lead as xDrip4iOS doesn’t support the new auth mechanism. Yet.

it should be 100% reliable, there's nothing more than 100 :)

In an ideal world, absolutely. But just like 100% secure, 100% reliable is an utopia for any app. However, let me put it this way: xDrip4iOS became so unreliable for me as a source for loop data, and the battery drain so high, that I was forced to abandon it and switch to the official G6 app - which has been 99.9% reliable and all battery drainage stopped. And I really really hate the official app and I really like xDrip4iOS - so it’s absolutely not a decision I took lightly, believe me. But, let's focus on the battery drain - stability is another (potentially bonus) matter.

Anyway, since I can’t possibly add any more information or arguments to this - I’ll leave it at this and hope the problem will be addressed. :)

Thanks either way for your consideration. :)

joshbohmer commented 5 months ago

"Actually I don't believe that all that communication has a significant impact on the battery"

One note in relation to this. The problem isn't the increased communication exactly... It's that the nRF52 can't enter low power mode while this extra comms are taking place and those extra CPU cycles (~120s vs ~15s) are what drains the battery prematurely.

During the development of Anubis I triggered a similar issue where the battery was draining faster than expected. It took me weeks of research and a power profiler to workout the cause of that battery drain problem. That fix was to resolve the low power mode also.

I'm not taking a guess at the problem here... I have explicit experience fixing this in Dexcom hardware.

JohanDegraeve commented 5 months ago

Intereseting : I google for "At some point, the system may need to terminate your app to free up memory for the current foreground app—causing any active or pending connections to be lost, for instance"

the first google result brings me to an issue in stackoverflow https://stackoverflow.com/questions/32563512/push-notification-app-running-in-background-when-ble-is-connected

the last answer is intereseting image

JohanDegraeve commented 5 months ago

"Actually I don't believe that all that communication has a significant impact on the battery"

One note in relation to this. The problem isn't the increased communication exactly... It's that the nRF52 can't enter low power mode while this extra comms are taking place and those extra CPU cycles (~120s vs ~15s) are what drains the battery prematurely.

During the development of Anubis I triggered a similar issue where the battery was draining faster than expected. It took me weeks of research and a power profiler to workout the cause of that battery drain problem. That fix was to resolve the low power mode also.

I'm not taking a guess at the problem here... I have explicit experience fixing this in Dexcom hardware.

I wonder if there's an explicit command to put the Dex in a sleep mode. Or is this the solution https://github.com/LoopKit/G7SensorKit/blob/main/G7SensorKit/G7CGMManager/G7BluetoothManager.swift#L224 which is delay 2 seconds before trying to reconnect That would be strange because Dex is awake and restarts advertising for longer than 2 seconds

JohanDegraeve commented 5 months ago

Just continue reading the link you gave. :) It opens with the terminator quote and then goes on to explain the options to prevent it from happening. Probably the parts from "Core Bluetooth Background Execution Modes" and "The bluetooth-central Background Execution Mode" bits are most relevant and explains why it prevents the quoted scenario from occurring.

Liroy , the way it's implemented in xDrip4iOS is exactly as it's described in that Apple documentation.

joshbohmer commented 5 months ago

"Actually I don't believe that all that communication has a significant impact on the battery" One note in relation to this. The problem isn't the increased communication exactly... It's that the nRF52 can't enter low power mode while this extra comms are taking place and those extra CPU cycles (~120s vs ~15s) are what drains the battery prematurely. During the development of Anubis I triggered a similar issue where the battery was draining faster than expected. It took me weeks of research and a power profiler to workout the cause of that battery drain problem. That fix was to resolve the low power mode also. I'm not taking a guess at the problem here... I have explicit experience fixing this in Dexcom hardware.

I wonder if there's an explicit command to put the Dex in a sleep mode. Or is this the solution https://github.com/LoopKit/G7SensorKit/blob/main/G7SensorKit/G7CGMManager/G7BluetoothManager.swift#L224 which is delay 2 seconds before trying to reconnect That would be strange because Dex is awake and restarts advertising for longer than 2 seconds

A sleep command would prevent other devices from retrieving data. As such there is no sleep command, only 0x09 disconnect

I'm not sure about the 2 second thread delay but it's worth a try.

LiroyvH commented 5 months ago

Liroy , the way it's implemented in xDrip4iOS is exactly as it's described in that Apple documentation.

Huh? How's that possible without adding the background modes about which that Apple doc says they're mandatory for proper functioning and processing whilst suspended? oO (Honestly a genuine question and curiosity, not trying to be a pain :))

(From "Core Bluetooth Background Execution Modes" quote: "If your app needs to run in background to perform certain Bluetooth-related tasks, it must declare that it supports a Core Bluetooth background execution mode in its Information property list (Info.plist) file. When your app declares this, the system wakes it up from a suspended state to allow it to handle Bluetooth-related events. This support is important for apps that interact with Bluetooth low energy devices that deliver data at regular intervals, such as a heart rate monitor.")

I'll be happy to declare those and then give it a try if the rest of the requires foundation is already implemented if that helps? :) (Note: that'd just be to see about stability/re-launch after termination, I assume this won't fix connection loops.)

JohanDegraeve commented 5 months ago

"If your app needs to run in background to perform certain Bluetooth-related tasks, it must declare that it supports a Core Bluetooth background execution mode" => see https://github.com/JohanDegraeve/xdripswift/blob/master/xdrip/Supporting%20Files/Info.plist#L54

In the issue you started with "I'll shamelessly plug my request for full implementation of Background Processing/Activity" well I don't know exactly what you mean but I know that some other applications are also adding "fetch" and/or "remote-notification" and/or "processing" Loop for instance uses "remote-notification" and "processing", see https://github.com/LoopKit/Loop/blob/c6b058b4276681600979aaeba518c635f06ac135/Loop/Info.plist#L79 remote-notification is definitely not necessary (Loop is not using push notifications) "processing" is necessary to run BGTaskScheduler tasks, Loop is using this for exporting critical event logs, see https://github.com/LoopKit/Loop/blob/c6b058b4276681600979aaeba518c635f06ac135/Loop/Managers/DeviceDataManager.swift#L1531

Please stop this discussion.

LiroyvH commented 5 months ago

Yes let's focus on fixing the reconnection loops as that's currently a more prudent problem and the primary subject for this issue. I'll try working on fixing the relaunch/stability issues on my own.

dnzxy commented 4 months ago

Since we warranty Anubis exactly like Dexcom out to 100 days, when a battery dies early we hear about it. In almost every case that happens, it's been activated around 60 days and xDrip4iOS/Shuggah was used. Every other app lasts to 200+ days.

No it’s not like that at all and I’m sad to hear that you think I’d say something like this if I hadn’t properly looked in to it and hadn’t tried eliminating other potential culprits. Heck, I even tried pinning it down to older/cheaper iPhone's but then got logs sent to me from people on the 15 Pro.

Just want to comment there to chime in and second this comment by @joshbohmer and @LiroyvH Had 2 Anubis as well as a stock G6 TX die prematurely while using xDrip4iOS. I was one of the people providing logs to Liroy from an old iPhone as well as an iPhone 15 Pro.

My last TX that died early after not even 60 days (57 days IIRC) was an Anubis, so I turned to the Anubis FB group and learned battery drainage and prematurely drained batteries are a known issue associated with xDiOS. When using the stock G6 app with either Anubis or stock G6 TX this does not happen.

JohanDegraeve commented 4 months ago

@dnzxy can you build the app in xcode? I could make a change and then you could test to see if it improves. Needs to be tested for longer period.

dnzxy commented 4 months ago

@JohanDegraeve I can build with Xcode or via fastlane, patch-based, branch-based, however you’d like. I will be out of country next week until the week after and will be switching to a fresh Anubis (re-battery’d) this week, so I could read battery voltage before and then after or during. How long does this test need to run for? Would you like to connect on Discord or somewhere else?

JohanDegraeve commented 4 months ago

Here's the branch to test https://github.com/JohanDegraeve/xdripswift/commits/issue494/

First thing would be to see if that solves the connect loops.

It would also be useful to start with a shorter value for delayInSeconds, if you set this to 1.0 https://github.com/JohanDegraeve/xdripswift/commit/295b64b12eed2751d529d62c5d868d65fefd54d3#diff-92e251c9866fabfbf175d735eb29d37cf187f760453fc7765a7948d38f0ce689R292

If it does solve the loops : install it and use it for a longer period (days, weeks, months). I expect that UI will not respond every 5 minutes for about 1-2 seconds. If that's experienced as an issue we could find another way to introduce this delay. We also need to see if the app remains stable, ie keeps on running for ever.

If 1 seconds doesn't solve it try with 2 seconds. Longer waiting period is probably not a good idea because there's a change that iOS will crash the app.

And then off course to see if that solves the battery drain issue. Only people who experienced the drain will be able to test that.

JohanDegraeve commented 4 months ago

Would you like to connect on Discord or somewhere else? Let's continue here for the moment. But don't add logs here. If needed send logs to xdrip@proximus.be but I think you can already analyse the logs yourself also?

dnzxy commented 4 months ago

Thanks for providing the code changes, @JohanDegraeve. I will look into building and getting set up over the next few days when changing to a new sensor (Wednesday the latest).

If needed send logs to xdrip@proximus.be but I think you can already analyse the logs yourself also?

No, so far I have only forwarded logs and verbose descriptions of my issue to Liroy, who seems to have been collecting these type of reports in the past. If you want to parse logs and look for anomalies (or lack thereof 😅), please let me know and I‘ll send them through.

JohanDegraeve commented 4 months ago

yes please do send the logs, to see if it solves the reconnect loop

dnzxy commented 4 months ago

It would also be useful to start with a shorter value for delayInSeconds, if you set this to 1.0 295b64b#diff-92e251c9866fabfbf175d735eb29d37cf187f760453fc7765a7948d38f0ce689R292

If it does solve the loops : install it and use it for a longer period (days, weeks, months). I expect that UI will not respond every 5 minutes for about 1-2 seconds. If that's experienced as an issue we could find another way to introduce this delay. We also need to see if the app remains stable, ie keeps on running for ever.

Sorry, I only got to look at this today. Question regarding your change here. Should that variable delayInSeconds not be used in L296 as value for the .sleep() instead of hard coding 2? Should only delayInSeconds be changed to 1 for starting the test or should both the sleep and the variable be changed to 1?

Edit to add: I will be out of country this weekend so might skip starting this test until I come back. I'll forward this to knowledgeable users I know who may feel comfortable testing this.

JohanDegraeve commented 4 months ago

Should that variable delayInSeconds not be used in L296 as value for the .sleep() instead of hard coding 2?

yes off course, I changed it and made a new commit

Edit to add: I will be out of country this weekend so might skip starting this test until I come back.

no problem

JohanDegraeve commented 3 months ago

It would also be useful to start with a shorter value for delayInSeconds, if you set this to 1.0 295b64b#diff-92e251c9866fabfbf175d735eb29d37cf187f760453fc7765a7948d38f0ce689R292 If it does solve the loops : install it and use it for a longer period (days, weeks, months). I expect that UI will not respond every 5 minutes for about 1-2 seconds. If that's experienced as an issue we could find another way to introduce this delay. We also need to see if the app remains stable, ie keeps on running for ever.

Sorry, I only got to look at this today. Question regarding your change here. Should that variable delayInSeconds not be used in L296 as value for the .sleep() instead of hard coding 2? Should only delayInSeconds be changed to 1 for starting the test or should both the sleep and the variable be changed to 1?

Edit to add: I will be out of country this weekend so might skip starting this test until I come back. I'll forward this to knowledgeable users I know who may feel comfortable testing this.

did you get a change to test it?

dnzxy commented 3 months ago

@JohanDegraeve came home very late Sunday night after a week of travel and attending ATTD. Will build and start the test with my next sensor change 👍

I sent this to a few other folks, not sure if they had the chance to test test. Let me ask around!

bjornoleh commented 3 months ago

Here's the branch to test https://github.com/JohanDegraeve/xdripswift/commits/issue494/

First thing would be to see if that solves the connect loops.

It would also be useful to start with a shorter value for delayInSeconds, if you set this to 1.0 295b64b#diff-92e251c9866fabfbf175d735eb29d37cf187f760453fc7765a7948d38f0ce689R292

If it does solve the loops : install it and use it for a longer period (days, weeks, months). I expect that UI will not respond every 5 minutes for about 1-2 seconds. If that's experienced as an issue we could find another way to introduce this delay. We also need to see if the app remains stable, ie keeps on running for ever.

If 1 seconds doesn't solve it try with 2 seconds. Longer waiting period is probably not a good idea because there's a change that iOS will crash the app.

And then off course to see if that solves the battery drain issue. Only people who experienced the drain will be able to test that.

Hi, did @dnzxy or anyone else start testing this yet? I have an Anubis battery change coming up now, and would like to start testing the suggested improvements.

Specifically, i wonder if I should start by setting delayInSeconds to 1 or 2 seconds?

dnzxy commented 3 months ago

@bjornoleh have it planned for my next sensor change. I forwarded this to @mikeplante1 as well, so maybe he has already started his test? Maybe @LiroyvH or @joshbohmer or any associated devs / techies have also started to run this as a test?

I think 1 second should be the starting value from what Johan was saying.

bjornoleh commented 3 months ago

Ok, I see that now. Will try with 1 second unless someone already knows differently.

2 seconds is currently set in the testing branch, I’ll change that to 1

https://github.com/JohanDegraeve/xdripswift/compare/develop...issue494

LiroyvH commented 3 months ago

@dnzxy For some reason the GitHub-app stopped sending notifications, didn't see these. I'm currently out of the country, but will build this when I get home. Probably Sunday or Monday. Can also probably put a transmitter on the sensor simulator hooked up to a spare iPhone to avoid other app interactions.

JohanDegraeve commented 3 months ago

it just made another change, unfortunately not compiled https://github.com/JohanDegraeve/xdripswift/commits/issue494/ It's a change that Paul had done for other cases where a sleep is itroduced

bjornoleh commented 3 months ago

I compiled the app based on the commits in the issue494 branch now, with and without that latest commit. I kept delayInSeconds at 2 seconds for these tests.

There seems to be issues when not including the latest commit https://github.com/JohanDegraeve/xdripswift/commit/e53154bd1e87993c3615e0deed90d044b3e85435. The app that was running an Anubis session crashed, and required a reboot of the phone to allow installing a working version again: image

I repeated this once more after a working build without the issue494 branch commits in between. The app still crashed, but this time a rebuild including the latest commit was enough to get back up and running, without a reboot .

When including the last commit, the app seems to work fine.

If only running in follower mode from Nightscout, the "Background Keep-alive" mode set to "Disabled" caused missed readings even with the screen on, while "Normal" mode seems to work. Not sure about his, perhaps something else affected the results here. But BG values did update when testing current master without the commits from the issue494 branch.

bjornoleh commented 3 months ago

Here's the branch to test https://github.com/JohanDegraeve/xdripswift/commits/issue494/

First thing would be to see if that solves the connect loops.

It would also be useful to start with a shorter value for delayInSeconds, if you set this to 1.0 295b64b#diff-92e251c9866fabfbf175d735eb29d37cf187f760453fc7765a7948d38f0ce689R292

If it does solve the loops : install it and use it for a longer period (days, weeks, months). I expect that UI will not respond every 5 minutes for about 1-2 seconds. If that's experienced as an issue we could find another way to introduce this delay. We also need to see if the app remains stable, ie keeps on running for ever.

If 1 seconds doesn't solve it try with 2 seconds. Longer waiting period is probably not a good idea because there's a change that iOS will crash the app.

And then off course to see if that solves the battery drain issue. Only people who experienced the drain will be able to test that.

I have not noticed any unresponsiveness due to the 2 second delayInSeconds.

Anything specific to look for in Xcode logs? Or are there logs in Xdrip4iOS too?

Edit: I am seeing the logs that can be sent by email. They do contain lots of "Will try to reconnect" also after running with the issue494 branch commits. But not sure if that is what matters here?

Edit2: with delayInSeconds = 2 seconds, I really only see "Will try to reconnect" once every five minutes:

2024-03-21 23:00:50.7380 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-21 23:05:51.9490 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-21 23:15:53.2450 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-21 23:20:51.0910 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-21 23:25:52.1980 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-21 23:30:52.5380 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-21 23:31:09.8660 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-21 23:35:52.2450 5.0.2 4193 BlueToothTransmitter               Will try to reconnect

If we compare to what this looked like yesterday, it was more frequent than every five minutes:

2024-03-20 23:06:04.5100 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:06:20.1130 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:06:35.8330 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:10:53.6580 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:15:50.5180 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:20:48.5350 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:21:04.4100 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:21:20.3190 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:21:35.8570 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:25:53.3930 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:30:49.9400 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:35:50.5700 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:36:06.2150 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:36:22.3830 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:40:49.1490 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:41:04.5660 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:41:20.5700 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:45:50.3800 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:46:05.9930 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:46:21.5660 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:46:37.1340 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:50:48.4840 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
2024-03-20 23:51:04.0620 5.0.2 4194 BlueToothTransmitter               Will try to reconnect
bjornoleh commented 3 months ago

I briefely tested setting delayInSeconds to 1 second. The app still works. Will monitor if the frequency of "Will try to reconnect" in logs change compared to having delayInSeconds set to 2 seconds.

JohanDegraeve commented 3 months ago

this looks very good actually. The 2 seconds give the Dex time to go to sleep.

bjornoleh commented 3 months ago

Looks like delayInSeconds = 1 second doesn't quite do the trick, there are more "Will try to reconnect" than once every five minutes in this little sample:

2024-03-22 00:00:51.6910 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-22 00:01:08.0530 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-22 00:05:51.5960 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-22 00:06:07.8980 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-22 00:10:50.8330 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
2024-03-22 00:15:54.2920 5.0.2 4193 BlueToothTransmitter               Will try to reconnect
JohanDegraeve commented 3 months ago

interesting

bjornoleh commented 3 months ago

interesting

This was a very short test with 1 second, so someone might want to test this again to confirm if 1 second is too little.

I am now starting to use the patch with 2 seconds for longer term testing on our looping phones.

bjornoleh commented 3 months ago

That was a short test, someone else might want to confirm if 1 sec is too little. We are starting to use 2 seconds for longer term testing now.

For other testers, the logs can be sent to oneself by email from the “Send Issue Report” feature. To see relevant logs in Xcode, toggle on OSLog first. Timestamps can be displayed by a setting in the bottom of the debug window.

image

https://github.com/JohanDegraeve/xdripswift/compare/develop...issue494

paulplant commented 3 months ago

Nice job on this one 👏

bjornoleh commented 3 months ago

I can confirm that the patch kills updating of BG values running in follower mode from Nightscout with the "Background Keep-alive" mode set to "Disabled". No BG updates happen until set to “Normal”, even with the screen on, while "Normal" mode does work.