From 58bbcb003558cef00759b4cf0fc592980f7ad2ec Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Thu, 17 Sep 2015 15:30:33 +0200 Subject: [PATCH] Pebble: allow to configure reconnect attempts This should help #89 --- .../gadgetbridge/activities/SettingsActivity.java | 2 ++ .../service/devices/pebble/PebbleIoThread.java | 12 +++++------- app/src/main/res/values/strings.xml | 4 ++-- app/src/main/res/xml/preferences.xml | 6 ++++++ 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java index f726ad1a8..cf9f3102a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java @@ -97,6 +97,7 @@ public class SettingsActivity extends AbstractSettingsActivity { final ListPreference audioPlayer = (ListPreference) findPreference("audio_player"); audioPlayer.setEntries(newEntries); audioPlayer.setEntryValues(newValues); + audioPlayer.setDefaultValue(newValues[0]); } @Override @@ -107,6 +108,7 @@ public class SettingsActivity extends AbstractSettingsActivity { "notification_mode_k9mail", "pebble_emu_addr", "pebble_emu_port", + "pebble_reconnect_attempts", }; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java index 4f6767cf1..b649621cf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java @@ -56,6 +56,8 @@ public class PebbleIoThread extends GBDeviceIoThread { public static final String PEBBLEKIT_ACTION_APP_START = "com.getpebble.action.app.START"; public static final String PEBBLEKIT_ACTION_APP_STOP = "com.getpebble.action.app.STOP"; + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + private final PebbleProtocol mPebbleProtocol; private final PebbleSupport mPebbleSupport; private boolean mIsTCP = false; @@ -67,7 +69,6 @@ public class PebbleIoThread extends GBDeviceIoThread { private boolean mQuit = false; private boolean mIsConnected = false; private boolean mIsInstalling = false; - private int mConnectionAttempts = 0; private PBWReader mPBWReader = null; private int mAppInstallToken = -1; @@ -148,7 +149,6 @@ public class PebbleIoThread extends GBDeviceIoThread { return false; } - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); mPebbleProtocol.setForceProtocol(sharedPrefs.getBoolean("pebble_force_protocol", false)); gbDevice.setState(GBDevice.State.CONNECTED); gbDevice.sendDeviceUpdateIntent(getContext()); @@ -311,13 +311,13 @@ public class PebbleIoThread extends GBDeviceIoThread { gbDevice.setState(GBDevice.State.CONNECTING); gbDevice.sendDeviceUpdateIntent(getContext()); - while (mConnectionAttempts++ < 10 && !mQuit) { - LOG.info("Trying to reconnect (attempt " + mConnectionAttempts + ")"); + int reconnectAttempts = Integer.valueOf(sharedPrefs.getString("pebble_reconnect_attempts", "10")); + while (reconnectAttempts-- > 0 && !mQuit) { + LOG.info("Trying to reconnect (attempts left " + reconnectAttempts + ")"); mIsConnected = connect(gbDevice.getAddress()); if (mIsConnected) break; } - mConnectionAttempts = 0; if (!mIsConnected) { mBtSocket = null; LOG.info("Bluetooth socket closed, will quit IO Thread"); @@ -341,7 +341,6 @@ public class PebbleIoThread extends GBDeviceIoThread { } private void enablePebbleKitReceiver(boolean enable) { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); boolean force_untested = sharedPrefs.getBoolean("pebble_force_untested", false); if (enable && force_untested) { @@ -403,7 +402,6 @@ public class PebbleIoThread extends GBDeviceIoThread { private boolean evaluateGBDeviceEventPebble(GBDeviceEvent deviceEvent) { if (deviceEvent instanceof GBDeviceEventVersionInfo) { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); if (sharedPrefs.getBoolean("datetime_synconconnect", true)) { LOG.info("syncing time"); write(mPebbleProtocol.encodeSetTime()); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4df4ecf94..9fe36c74f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -34,7 +34,7 @@ General Settings Connect to device when Bluetooth turned on Preferred audio player - default + Default Date and Time Sync time Sync time to device when connecting and when time or timezone changes on Android @@ -63,7 +63,7 @@ This option forces using the latest notification protocol depending on the firmware version. ENABLE ONLY IF YOU KNOW WHAT YOU ARE DOING! Enable untested features Enable features that are untested. ENABLE ONLY IF YOU KNOW WHAT YOU ARE DOING! - + Reconnect attempts not connected connecting connected diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 04ca66491..4f10aaf0c 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -84,6 +84,12 @@ android:key="pebble_force_untested" android:summary="@string/pref_summary_pebble_forceuntested" android:title="@string/pref_title_pebble_forceuntested" /> +