diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/g5model/DexSyncKeeper.java b/app/src/main/java/com/eveningoutpost/dexdrip/g5model/DexSyncKeeper.java index 1b8f2a4122..38cca708e6 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/g5model/DexSyncKeeper.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/g5model/DexSyncKeeper.java @@ -8,8 +8,10 @@ import com.eveningoutpost.dexdrip.utilitymodels.Constants; import com.eveningoutpost.dexdrip.utilitymodels.PersistentStore; +import static com.eveningoutpost.dexdrip.models.JoH.tsl; import static com.eveningoutpost.dexdrip.utilitymodels.BgGraphBuilder.DEXCOM_PERIOD; import static com.eveningoutpost.dexdrip.utilitymodels.BgGraphBuilder.DEX_RAPID_RECONNECT_PERIOD; +import static com.eveningoutpost.dexdrip.utilitymodels.Constants.MINUTE_IN_MS; public class DexSyncKeeper { @@ -18,7 +20,7 @@ public class DexSyncKeeper { private static final long OLDEST_POSSIBLE = 1533839836123L; private static final long GRACE_TIME = 5000; private static final long VALIDITY_PERIOD = Constants.DAY_IN_MS; - private static long dexPeriod; + private static long dexPeriod; // Local representation of the Dex period allowing us to temporarily reduce it to 1 minute. // store sync time as now public static void store(final String transmitterId) { @@ -68,7 +70,12 @@ public static long anticipate(final String transmitterId) { // anticipate next wake up from time // -1 means we don't know anything static long anticipate(final String transmitterId, final long now) { - if (Ob1G5CollectionService.rapid_reconnect_transition) { + if (Ob1G5CollectionService.rapid_reconnect_transition) { // If we have just paired with a G7 + if (tsl() - Ob1G5CollectionService.rapid_reconnect_transition_bond_time > MINUTE_IN_MS * 10) { // If it has been more than 10 minutes since pairing + Ob1G5CollectionService.rapid_reconnect_transition = false; // Terminate the Rapid Reconnect transition sequence as it seems that it has failed. + // This could happen if the user walks away from the phone less than 10 minutes after pairing. + UserError.Log.e(TAG, "Back to waking once every 5 minutes "); + } dexPeriod = DEX_RAPID_RECONNECT_PERIOD; // Set the period to 1 minute after pairing with a G7. } else { dexPeriod = DEXCOM_PERIOD; // Set the period to the default 5-minute cycle otherwise. @@ -78,7 +85,7 @@ static long anticipate(final String transmitterId, final long now) { return -1; } if (last > now) { - UserError.Log.e(TAG, "Anticipation time in the future! cannot use: " + JoH.dateTimeText(last)); + UserError.Log.e(TAG, "Last time in the future! cannot use: " + JoH.dateTimeText(last)); return -1; // can't be in the future } diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/g5model/Ob1G5StateMachine.java b/app/src/main/java/com/eveningoutpost/dexdrip/g5model/Ob1G5StateMachine.java index 9c1e3574a6..78850ec310 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/g5model/Ob1G5StateMachine.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/g5model/Ob1G5StateMachine.java @@ -92,7 +92,6 @@ import static com.eveningoutpost.dexdrip.utilitymodels.Constants.HOUR_IN_MS; import static com.eveningoutpost.dexdrip.utilitymodels.Constants.MINUTE_IN_MS; import static com.eveningoutpost.dexdrip.utilitymodels.Constants.SECOND_IN_MS; -import static com.eveningoutpost.dexdrip.utils.DexCollectionType.getBestCollectorHardwareName; import static com.eveningoutpost.dexdrip.utils.bt.Helper.getStatusName; @@ -889,14 +888,12 @@ public static boolean doGetData(Ob1G5CollectionService parent, RxBleConnection c parent.msg("Invalid Glucose"); } if (Ob1G5CollectionService.rapid_reconnect_transition) { // Manage wake frequency after pairing with G7 - Ob1G5CollectionService.rapid_reconnect_transition_count++; // We have had a handshake. Therefore, we increment the count. - UserError.Log.e(TAG, "# of Rapid Reconnect handshakes: " + Ob1G5CollectionService.rapid_reconnect_transition_count); - if (Ob1G5CollectionService.rapid_reconnect_transition_count == 2) { + Ob1G5CollectionService.rapid_reconnect_transition_handshake++; // We have had a handshake. Therefore, we increment the count. + UserError.Log.e(TAG, "# of Rapid Reconnect transition handshakes: " + Ob1G5CollectionService.rapid_reconnect_transition_handshake); + if (Ob1G5CollectionService.rapid_reconnect_transition_handshake > 2) { + Ob1G5CollectionService.rapid_reconnect_transition = false; // Disabling the Rapid Reconnect transition sequence as we have already had 3 handshakes. UserError.Log.e(TAG, "Back to waking once every 5 minutes "); } - if (Ob1G5CollectionService.rapid_reconnect_transition_count > 2) { - Ob1G5CollectionService.rapid_reconnect_transition = false; - } } break; diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/services/Ob1G5CollectionService.java b/app/src/main/java/com/eveningoutpost/dexdrip/services/Ob1G5CollectionService.java index 6255a4a189..925565ee0c 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/services/Ob1G5CollectionService.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/services/Ob1G5CollectionService.java @@ -248,8 +248,9 @@ public class Ob1G5CollectionService extends G5BaseService { private static boolean do_discovery = true; private static final boolean do_auth = true; //private static boolean initiate_bonding = false; - public static boolean rapid_reconnect_transition = false; - public static int rapid_reconnect_transition_count = 0; + public static boolean rapid_reconnect_transition = false; // True when waking once a minute after pairing with a G7 + public static int rapid_reconnect_transition_handshake = 0; // Number of successful initial handshakes after pairing with a G7 + public static long rapid_reconnect_transition_bond_time; // A record of the time we bond with a G7 private static final Set alwaysScanModels = Sets.newHashSet("SM-N910V", "G Watch"); private static final List alwaysScanModelFamilies = Arrays.asList("SM-N910"); @@ -946,14 +947,13 @@ private void handleWakeup() { UserError.Log.d(TAG, "Always scan mode"); changeState(SCAN); } else { - if ((connectFailures > 0 || (!use_auto_connect && connectNowFailures > 0)) && !rapid_reconnect_transition) { // Only go to scan always mode due to count if we have not just paired with a G7. - // After we pair with a G7, we wake once a minute. Therefore, the connect failure will trigger early and should not be used. + if ((connectFailures > 0 || (!use_auto_connect && connectNowFailures > 0)) && !rapid_reconnect_transition) { always_scan = true; UserError.Log.e(TAG, "Switching to scan always mode due to connect failures metric: " + connectFailures); changeState(SCAN); - } else if (rapid_reconnect_transition_count > 1) { // We switch to scan always mode for the third handshake of a G7 pairing to ensure it is captured. + } else if (rapid_reconnect_transition && rapid_reconnect_transition_handshake > 1) { // We switch to scan always mode for the third handshake of a G7 pairing to ensure it is captured. always_scan = true; - UserError.Log.e(TAG, "Switching to scan always mode "); + UserError.Log.e(TAG, "Switching to scan always mode for perfect time stamp capture "); changeState(SCAN); } else if (use_auto_connect && (connectNowFailures > 1) && (connectFailures < 0)) { UserError.Log.d(TAG, "Avoiding power connect due to failure metric: " + connectNowFailures + " " + connectFailures); @@ -1742,9 +1742,10 @@ public void onReceive(Context context, Intent intent) { + " Bond state " + parcel_device.getBondState() + bondState(parcel_device.getBondState()) + " " + "bs: " + bondState(bond_state_extra) + " was " + bondState(previous_bond_state_extra)); if (DexCollectionType.getBestCollectorHardwareName().equals("G7") && parcel_device.getBondState() == BluetoothDevice.BOND_BONDED) { // G7 just paired - rapid_reconnect_transition = true; // There is only 20% chance we are on the correct time grid. Let's wake once a minute to find the right grid. - rapid_reconnect_transition_count = 0; // Let's count the number of handshakes so that we can control the procedure. - UserError.Log.e(TAG, "Wake every minute to capture the correct time stamp "); + rapid_reconnect_transition = true; // There is only 20% chance now is the correct time grid. Let's wake once a minute to find the right grid. + rapid_reconnect_transition_handshake = 0; // Let's count the number of successful handshakes so that we can control the required sequence. + rapid_reconnect_transition_bond_time = tsl(); // Remember this time so that we can terminate in case of failure. + UserError.Log.e(TAG, "Attempt to wake every minute to capture the correct time stamp "); } try { if (parcel_device.getAddress().equals(transmitterMAC)) { @@ -2154,7 +2155,7 @@ public static List megaStatus() { final List l = new ArrayList<>(); - if (!DexSyncKeeper.isReady(transmitterID)) { + if (!DexSyncKeeper.isReady(transmitterID) || (rapid_reconnect_transition && rapid_reconnect_transition_handshake < 2)) { l.add(new StatusItem("Hunting Transmitter", "Stay on this page", CRITICAL)); } @@ -2191,7 +2192,11 @@ public static List megaStatus() { } if ((!lastState.startsWith("Service Stopped")) && (!lastState.startsWith("Not running"))) - l.add(new StatusItem("Brain State", state.getString() + (error_count > 1 ? " Errors: " + error_count : ""), error_count > 1 ? NOTICE : error_count > 4 ? BAD : NORMAL)); + if (rapid_reconnect_transition) { + l.add(new StatusItem("Brain State", state.getString()+ "")); + } else { + l.add(new StatusItem("Brain State", state.getString() + (error_count > 1 ? " Errors: " + error_count : ""), error_count > 1 ? NOTICE : error_count > 4 ? BAD : NORMAL)); + } if (lastUsableGlucosePacketTime != 0) { if (msSince(lastUsableGlucosePacketTime) < MINUTE_IN_MS * 15) {