diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index 46e8bc33a2..6765b15558 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -61,4 +61,5 @@ public class Constants { // Pump public static final int PUMP_MAX_CONNECTION_TIME_IN_SECONDS = 60 - 1; + public static final int MIN_WATCHDOG_INTERVAL_IN_SECONDS = 12 * 60; } diff --git a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java index 36b43d2181..68811a1486 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.queue; +import android.bluetooth.BluetoothAdapter; import android.content.Context; import android.os.PowerManager; import android.os.SystemClock; @@ -16,6 +17,7 @@ import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventDismissBolusprogressIfRunning; import info.nightscout.androidaps.queue.events.EventQueueChanged; +import info.nightscout.utils.SP; /** * Created by mike on 09.11.2017. @@ -61,8 +63,27 @@ public class QueueThread extends Thread { MainApp.bus().post(new EventDismissBolusprogressIfRunning(new PumpEnactResult())); MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.connectiontimedout))); pump.stopConnecting(); - queue.clear(); - return; + + //BLUETOOTH-WATCHDOG + boolean watchdog = SP.getBoolean(R.string.key_btwatchdog, false); + long last_watchdog = SP.getLong(R.string.key_btwatchdog_lastbark, 0l); + watchdog = watchdog && System.currentTimeMillis() - last_watchdog > (Constants.MIN_WATCHDOG_INTERVAL_IN_SECONDS * 1000); + if(watchdog) { + log.debug("BT watchdog - toggeling the phonest bluetooth"); + //write time + SP.putLong(R.string.key_btwatchdog_lastbark, System.currentTimeMillis()); + //toggle BT + BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + mBluetoothAdapter.disable(); + SystemClock.sleep(1000); + mBluetoothAdapter.enable(); + SystemClock.sleep(1000); + //start over again once after watchdog barked + connectionStartTime = System.currentTimeMillis(); + } else { + queue.clear(); + return; + } } if (!pump.isConnected()) { diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java index 828f2c5bed..38ca9ee2a1 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java @@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory; import java.util.Date; +import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -81,7 +82,7 @@ public class KeepAliveReceiver extends BroadcastReceiver { boolean alarmTimeoutExpired = lastConnection.getTime() + pumpUnreachableThreshold() < System.currentTimeMillis(); boolean nextAlarmOccurrenceReached = SP.getLong("nextPumpDisconnectedAlarm", 0l) < System.currentTimeMillis(); - if (SP.getBoolean(MainApp.sResources.getString(R.string.key_enable_pump_unreachable_alert), true) + if (Config.APS && SP.getBoolean(MainApp.sResources.getString(R.string.key_enable_pump_unreachable_alert), true) && isStatusOutdated && alarmTimeoutExpired && nextAlarmOccurrenceReached && !ConfigBuilderPlugin.getActiveLoop().isDisconnected()) { Notification n = new Notification(Notification.PUMP_UNREACHABLE, MainApp.sResources.getString(R.string.pump_unreachable), Notification.URGENT); n.soundId = R.raw.alarm; @@ -145,4 +146,4 @@ public class KeepAliveReceiver extends BroadcastReceiver { nextPumpDisconnectedAlarm = Math.min(System.currentTimeMillis() + pumpUnreachableThreshold(), nextPumpDisconnectedAlarm); SP.putLong("nextPumpDisconnectedAlarm", nextPumpDisconnectedAlarm); } -} \ No newline at end of file +} diff --git a/app/src/main/java/info/nightscout/utils/SP.java b/app/src/main/java/info/nightscout/utils/SP.java index 36ab3b05e7..16015d8956 100644 --- a/app/src/main/java/info/nightscout/utils/SP.java +++ b/app/src/main/java/info/nightscout/utils/SP.java @@ -65,7 +65,11 @@ public class SP { } static public long getLong(int resourceID, Long defaultValue) { - return SafeParse.stringToLong(sharedPreferences.getString(MainApp.sResources.getString(resourceID), defaultValue.toString())); + try { + return sharedPreferences.getLong(MainApp.sResources.getString(resourceID), defaultValue); + } catch (Exception e) { + return SafeParse.stringToLong(sharedPreferences.getString(MainApp.sResources.getString(resourceID), defaultValue.toString())); + } } static public long getLong(String key, Long defaultValue) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1c423b4918..1dd663764a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -772,10 +772,16 @@ missed_bg_readings_threshold Urgent Alarm INFO + bt_watchdog + bt_watchdog_last + Bluetooth + BT Watchdog + Switches off the phone\'s bluetooth for one second if no connection to the pump is possible. This may help on some phones where the bluetooth stack freezes. DexcomG5 App (patched) Upload BG data to NS dexcomg5_nsupload G5 upload settings Customized APK for download + diff --git a/app/src/main/res/xml/pref_advanced.xml b/app/src/main/res/xml/pref_advanced.xml index 5395a75f1e..25beb5fda7 100644 --- a/app/src/main/res/xml/pref_advanced.xml +++ b/app/src/main/res/xml/pref_advanced.xml @@ -124,6 +124,16 @@ android:title="@string/do_not_track_profile_switch" android:summary="@string/do_not_track_profile_switch_summary"/> + + + + +