aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--res/xml/pref.xml1
-rw-r--r--src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java117
-rw-r--r--src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java21
3 files changed, 72 insertions, 67 deletions
diff --git a/res/xml/pref.xml b/res/xml/pref.xml
index 9f39cc1..efe9f60 100644
--- a/res/xml/pref.xml
+++ b/res/xml/pref.xml
@@ -44,6 +44,7 @@
android:key="@string/pref_replace_std_gps_key"
android:persistent="true"
android:disableDependentsState="true"
+ android:defaultValue="true"
android:title="@string/pref_replace_std_gps_title"
android:summaryOn="@string/pref_replace_std_gps_summary_on"
android:summaryOff="@string/pref_replace_std_gps_summary_off" />
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java
index 58b4ede..4db6d65 100644
--- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java
+++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java
@@ -102,8 +102,7 @@ public class BlueetoothGpsManager {
Log.e("BT test", "error while getting data", e);
setMockLocationProviderOutOfService();
} finally {
- // remove because we want to retry...
- // disable();
+ disableIfNeeded();
}
}
@@ -158,11 +157,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);
@@ -172,7 +174,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();
@@ -219,65 +221,50 @@ public class BlueetoothGpsManager {
if (gpsSocket == null){
Log.e("BT test", "Error while establishing connection: no socket");
} else {
- Runnable connectThread = new Runnable() {
- private int connectionTry=0;
+ Runnable connectThread = new Runnable() {
@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();
}
}
}
@@ -285,13 +272,34 @@ 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);
}
}
}
}
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){
@@ -312,7 +320,6 @@ public class BlueetoothGpsManager {
}
}
-
public void enableMockLocationProvider(String gpsName){
if (parser != null){
parser.enableMockLocationProvider(gpsName);
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java
index f5f936e..a398e19 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();
} else if (BluetoothGpsProviderService.PREF_SIRF_ENABLE_GLL.equals(key)