diff options
author | Herbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com> | 2010-10-01 00:37:15 +0200 |
---|---|---|
committer | Herbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com> | 2010-10-01 00:37:15 +0200 |
commit | a77f4923dc64d502da4089c3c4c555b06b3bf03a (patch) | |
tree | b6945f75ebd392f50ccae04fe76cbf5abf809425 /src/org/broeuschmeul/android/gps/bluetooth | |
parent | 359b9eecf647f1480dfb92df0cf6d16afe938495 (diff) | |
download | BlueGPS-a77f4923dc64d502da4089c3c4c555b06b3bf03a.zip BlueGPS-a77f4923dc64d502da4089c3c4c555b06b3bf03a.tar.gz BlueGPS-a77f4923dc64d502da4089c3c4c555b06b3bf03a.tar.bz2 |
rewrite connection/disconnection parts
Diffstat (limited to 'src/org/broeuschmeul/android/gps/bluetooth')
-rw-r--r-- | src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java | 115 | ||||
-rw-r--r-- | src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java | 21 |
2 files changed, 71 insertions, 65 deletions
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index 2229e6a..41cc8c4 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -86,8 +86,7 @@ public class BlueetoothGpsManager { Log.e("BT test", "error while getting data", e);
setMockLocationProviderOutOfService();
} finally {
- // remove because we want to retry...
- // disable();
+ disableIfNeeded();
}
}
}
@@ -107,11 +106,14 @@ public class BlueetoothGpsManager { private Context appContext;
private NotificationManager notificationManager;
private int maxConnectionRetries;
+ private int nbRetriesRemaining;
+ private boolean connected = false;
public BlueetoothGpsManager(Service callingService, String deviceAddress, int maxRetries) {
this.gpsDeviceAddress = deviceAddress;
this.callingService = callingService;
this.maxConnectionRetries = maxRetries;
+ this.nbRetriesRemaining = 1+maxRetries;
this.appContext = callingService.getApplicationContext();
locationManager = (LocationManager)callingService.getSystemService(Context.LOCATION_SERVICE);
notificationManager = (NotificationManager)callingService.getSystemService(Context.NOTIFICATION_SERVICE);
@@ -121,7 +123,7 @@ public class BlueetoothGpsManager { connectionProblemNotification.icon = R.drawable.icon;
Intent stopIntent = new Intent(BluetoothGpsProviderService.ACTION_STOP_GPS_PROVIDER);
// PendingIntent stopPendingIntent = PendingIntent.getService(appContext, 0, stopIntent, PendingIntent.FLAG_CANCEL_CURRENT);
- PendingIntent stopPendingIntent = PendingIntent.getService(appContext, 0, stopIntent, 0);
+ PendingIntent stopPendingIntent = PendingIntent.getService(appContext, 0, stopIntent, PendingIntent.FLAG_CANCEL_CURRENT);
connectionProblemNotification.contentIntent = stopPendingIntent;
serviceStoppedNotification = new Notification();
@@ -169,64 +171,49 @@ public class BlueetoothGpsManager { Log.e("BT test", "Error while establishing connection: no socket");
} else {
Runnable connectThread = new Runnable() {
- private int connectionTry=0;
@Override
public void run() {
try {
- connectionTry++;
+ connected = false;
Log.e("BT test", "current device: "+gpsDevice.getName() + " -- " + gpsDevice.getAddress());
- try {
- if (gpsSocket != null){
- Log.e("BT test", "trying to close old socket");
- gpsSocket.close();
+ if ((bluetoothAdapter.isEnabled()) && (nbRetriesRemaining > 0 )){
+ try {
+ if (gpsSocket != null){
+ Log.e("BT test", "trying to close old socket");
+ gpsSocket.close();
+ }
+ } catch (IOException e) {
+ Log.e("BT test", "Error during disconnection", e);
+ }
+ try {
+ gpsSocket = gpsDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
+ } catch (IOException e) {
+ Log.e("BT test", "Error during connection", e);
+ }
+ if (gpsSocket == null){
+ Log.e("BT test", "Error while establishing connection: no socket");
+ } else {
+ // Cancel discovery because it will slow down the connection
+ bluetoothAdapter.cancelDiscovery();
+ // we increment the number of connection try
+ // Connect the device through the socket. This will block
+ // until it succeeds or throws an exception
+ gpsSocket.connect();
+ // connection obtained so reset the number of connection try
+ connected = true;
+ nbRetriesRemaining = 1+maxConnectionRetries ;
+ notificationManager.cancel(R.string.connection_problem_notification_title);
+ connectedGps = new ConnectedGps(gpsSocket);
+ connectionAndReadingPool.execute(connectedGps);
}
- } catch (IOException e) {
- Log.e("BT test", "Error during disconnection", e);
- }
- try {
- gpsSocket = gpsDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
- } catch (IOException e) {
- Log.e("BT test", "Error during connection", e);
- }
- if (gpsSocket == null){
- Log.e("BT test", "Error while establishing connection: no socket");
- } else {
- // Cancel discovery because it will slow down the connection
- bluetoothAdapter.cancelDiscovery();
- // we increment the number of connection try
- // Connect the device through the socket. This will block
- // until it succeeds or throws an exception
- gpsSocket.connect();
- // connection obtained so reset the number of connection try
- connectionTry=0;
- notificationManager.cancel(R.string.connection_problem_notification_title);
- connectedGps = new ConnectedGps(gpsSocket);
- connectionAndReadingPool.execute(connectedGps);
}
} catch (IOException connectException) {
// Unable to connect
- Log.e("BT test", "error while connecting to socket", connectException);
+ Log.e("BT test", "error while connecting to socket", connectException);
} finally {
- if (connectionTry > 0)
- {
- // Unable to connect
- Log.e("BT test", "Unable to establish connection");
- connectionProblemNotification.when = System.currentTimeMillis();
- String pbMessage = appContext.getResources().getQuantityString(R.plurals.connection_problem_notification, 5-connectionTry, 5-connectionTry);
- connectionProblemNotification.setLatestEventInfo(appContext,
- appContext.getString(R.string.connection_problem_notification_title),
- pbMessage,
- connectionProblemNotification.contentIntent);
- connectionProblemNotification.number = connectionTry;
- notificationManager.notify(R.string.connection_problem_notification_title, connectionProblemNotification);
- }
- // if bluetooth has bean disabled or
- // if two much tries consider that we are enable to connect. So close everything and get out
- if ((!bluetoothAdapter.isEnabled()) || (connectionTry >= maxConnectionRetries )){
- notificationManager.cancel(R.string.connection_problem_notification_title);
- serviceStoppedNotification.when = System.currentTimeMillis();
- notificationManager.notify(R.string.service_closed_because_connection_problem_notification_title, serviceStoppedNotification);
- disable();
+ nbRetriesRemaining--;
+ if (! connected) {
+ disableIfNeeded();
}
}
}
@@ -234,7 +221,7 @@ public class BlueetoothGpsManager { this.enabled = true;
notificationPool = Executors.newSingleThreadExecutor();
connectionAndReadingPool = Executors.newSingleThreadScheduledExecutor();
- connectionAndReadingPool.scheduleWithFixedDelay(connectThread, 100, 60000, TimeUnit.MILLISECONDS);
+ connectionAndReadingPool.scheduleWithFixedDelay(connectThread, 5000, 60000, TimeUnit.MILLISECONDS);
}
}
}
@@ -242,6 +229,28 @@ public class BlueetoothGpsManager { return this.enabled;
}
+ private synchronized void disableIfNeeded(){
+ if (enabled){
+ if (nbRetriesRemaining > 0){
+ // Unable to connect
+ Log.e("BT test", "Unable to establish connection");
+ 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 - nbRetriesRemaining;
+ notificationManager.notify(R.string.connection_problem_notification_title, connectionProblemNotification);
+ } else {
+ notificationManager.cancel(R.string.connection_problem_notification_title);
+ serviceStoppedNotification.when = System.currentTimeMillis();
+ notificationManager.notify(R.string.service_closed_because_connection_problem_notification_title, serviceStoppedNotification);
+ disable();
+ }
+ }
+ }
+
public synchronized void disable() {
if (enabled){
enabled = false;
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java index b3349b4..e09ae34 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java @@ -131,30 +131,27 @@ public class BluetoothGpsActivity extends PreferenceActivity implements OnPrefer } @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, - String key) { + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (BluetoothGpsProviderService.PREF_START_GPS_PROVIDER.equals(key)){ - boolean val = false; - if (val = sharedPreferences.getBoolean(key, false)){ + boolean val = sharedPreferences.getBoolean(key, false); + CheckBoxPreference pref = (CheckBoxPreference)findPreference(key); + if (pref.isChecked() != val){ + pref.setChecked(val); + } else if (val){ startService(new Intent(BluetoothGpsProviderService.ACTION_START_GPS_PROVIDER)); } else { startService(new Intent(BluetoothGpsProviderService.ACTION_STOP_GPS_PROVIDER)); } + } else if (BluetoothGpsProviderService.PREF_TRACK_RECORDING.equals(key)){ + boolean val = sharedPreferences.getBoolean(key, false); CheckBoxPreference pref = (CheckBoxPreference)findPreference(key); if (pref.isChecked() != val){ pref.setChecked(val); - } - } else if (BluetoothGpsProviderService.PREF_TRACK_RECORDING.equals(key)){ - boolean val = false; - if (val = sharedPreferences.getBoolean(key, false)){ + } else if (val){ startService(new Intent(BluetoothGpsProviderService.ACTION_START_TRACK_RECORDING)); } else { startService(new Intent(BluetoothGpsProviderService.ACTION_STOP_TRACK_RECORDING)); } - CheckBoxPreference pref = (CheckBoxPreference)findPreference(key); - if (pref.isChecked() != val){ - pref.setChecked(val); - } } else if (BluetoothGpsProviderService.PREF_BLUETOOTH_DEVICE.equals(key)){ updateDevicePreferenceSummary(); } |