diff options
author | Herbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com> | 2011-05-05 22:52:09 +0200 |
---|---|---|
committer | Herbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com> | 2012-10-10 03:22:25 +0200 |
commit | 0f3f8565b8778e8c1d8fae9d33bc987a272a5911 (patch) | |
tree | 6e28d13615ca361503b4defe132980fc5653098d | |
parent | e67872ca05740d9b01a6ef14a3424ccafcb1a17c (diff) | |
download | BlueGPS-0f3f8565b8778e8c1d8fae9d33bc987a272a5911.zip BlueGPS-0f3f8565b8778e8c1d8fae9d33bc987a272a5911.tar.gz BlueGPS-0f3f8565b8778e8c1d8fae9d33bc987a272a5911.tar.bz2 |
try to reimplement connection timeout and connection retries management
-rw-r--r-- | src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index da76754..d3e0bc3 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -124,12 +124,12 @@ public class BlueetoothGpsManager { }
public void run() {
+ long now = SystemClock.uptimeMillis();
+ long lastRead = now;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"US-ASCII"));
String s;
- long now = SystemClock.uptimeMillis();
- long lastRead = now;
- while((enabled) && (now < lastRead+5000 )){
+ while((enabled) && (now < lastRead+10000 )){
if (reader.ready()){
s = reader.readLine();
Log.v(LOG_TAG, "data: "+System.currentTimeMillis()+" "+s);
@@ -148,6 +148,7 @@ public class BlueetoothGpsManager { } finally {
// cleanly closing everything...
this.close();
+ retriesTimeout = lastRead + maxConnectionRetries*60000;
disableIfNeeded();
}
}
@@ -233,6 +234,7 @@ public class BlueetoothGpsManager { private NotificationManager notificationManager;
private int maxConnectionRetries;
private int nbRetriesRemaining;
+ private long retriesTimeout = 0L;
private boolean connected = false;
/**
@@ -327,7 +329,7 @@ public class BlueetoothGpsManager { }
if (gpsSocket == null){
Log.e(LOG_TAG, "Error while establishing connection: no socket");
- disable(R.string.msg_bluetooth_gps_unavaible);
+ disable(R.string.msg_bluetooth_gps_unavaible);
} else {
Runnable connectThread = new Runnable() {
@Override
@@ -398,7 +400,9 @@ public class BlueetoothGpsManager { Log.v(LOG_TAG, "starting connection and reading thread");
connectionAndReadingPool = Executors.newSingleThreadScheduledExecutor();
Log.v(LOG_TAG, "starting connection to socket task");
- connectionAndReadingPool.scheduleWithFixedDelay(connectThread, 5000, 60000, TimeUnit.MILLISECONDS);
+ this.retriesTimeout = SystemClock.uptimeMillis()+maxConnectionRetries*60000 ;
+ connectionAndReadingPool.scheduleWithFixedDelay(connectThread, 3000, 10000, TimeUnit.MILLISECONDS);
+// connectionAndReadingPool.scheduleWithFixedDelay(connectThread, 5000, 60000, TimeUnit.MILLISECONDS);
}
}
}
@@ -414,17 +418,21 @@ public class BlueetoothGpsManager { */
private synchronized void disableIfNeeded(){
if (enabled){
- if (nbRetriesRemaining > 0){
+ long currentTime = Math.min(SystemClock.uptimeMillis(),retriesTimeout);
+ if (retriesTimeout > currentTime){
+// if (nbRetriesRemaining > 0){
// Unable to connect
- Log.e(LOG_TAG, "Unable to establish connection");
+ nbRetriesRemaining = 1 + (int)((retriesTimeout - currentTime)/60000) ;
connectionProblemNotification.when = System.currentTimeMillis();
String pbMessage = appContext.getResources().getQuantityString(R.plurals.connection_problem_notification, nbRetriesRemaining, nbRetriesRemaining);
connectionProblemNotification.setLatestEventInfo(appContext,
appContext.getString(R.string.connection_problem_notification_title),
pbMessage,
connectionProblemNotification.contentIntent);
+// connectionProblemNotification.number = 1 + maxConnectionRetries - (int)((retriesTimeout - currentTime)/60000);
connectionProblemNotification.number = 1 + maxConnectionRetries - nbRetriesRemaining;
notificationManager.notify(R.string.connection_problem_notification_title, connectionProblemNotification);
+ Log.e(LOG_TAG, "Unable to establish connection since "+connectionProblemNotification.number + " still " + nbRetriesRemaining +" remaining");
} else {
// notificationManager.cancel(R.string.connection_problem_notification_title);
// serviceStoppedNotification.when = System.currentTimeMillis();
|