From 09dac632f9d175ab4118f7a90dddaf0709d274a7 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Fri, 1 Dec 2017 13:57:53 +0100 Subject: [PATCH] bluetooth watchdog --- .../info/nightscout/androidaps/Constants.java | 1 + .../androidaps/queue/QueueThread.java | 25 +++++++++++++++++-- app/src/main/res/values/strings.xml | 5 ++++ app/src/main/res/xml/pref_advanced.xml | 10 ++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) 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/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0e2833c970..33f8f466e2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -772,5 +772,10 @@ 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. 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"/> + + + + +