aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHerbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com>2011-05-05 21:23:03 +0200
committerHerbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com>2011-05-12 18:07:43 +0200
commitee03eba64ee9ed1fc744b528f99ed218f6cc1ee0 (patch)
treef3c64c9e0f770c3bf2c8399d4ad153398a100f2c
parent4890589a876740cde6508e6e3aa5d39c9ef658e5 (diff)
downloadBlueGPS-ee03eba64ee9ed1fc744b528f99ed218f6cc1ee0.zip
BlueGPS-ee03eba64ee9ed1fc744b528f99ed218f6cc1ee0.tar.gz
BlueGPS-ee03eba64ee9ed1fc744b528f99ed218f6cc1ee0.tar.bz2
try to better close bt socket...
Close input and output streams before closing the socket
-rw-r--r--src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java69
1 files changed, 61 insertions, 8 deletions
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java
index 22691a6..bfdb0cf 100644
--- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java
+++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java
@@ -79,6 +79,10 @@ public class BlueetoothGpsManager {
*/
private class ConnectedGps extends Thread {
/**
+ * GPS bluetooth socket used for communication.
+ */
+ private final BluetoothSocket socket;
+ /**
* GPS InputStream from which we read data.
*/
private final InputStream in;
@@ -97,6 +101,7 @@ public class BlueetoothGpsManager {
private boolean ready = false;
public ConnectedGps(BluetoothSocket socket) {
+ this.socket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
PrintStream tmpOut2 = null;
@@ -141,7 +146,8 @@ public class BlueetoothGpsManager {
Log.e(LOG_TAG, "error while getting data", e);
setMockLocationProviderOutOfService();
} finally {
- ready = false;
+ // cleanly closing everything...
+ this.close();
disableIfNeeded();
}
}
@@ -180,6 +186,31 @@ public class BlueetoothGpsManager {
Log.e(LOG_TAG, "Exception during write", e);
}
}
+
+ public void close(){
+ ready = false;
+ try {
+ Log.d(LOG_TAG, "closing Bluetooth GPS output sream");
+ in.close();
+ } catch (IOException e) {
+ Log.e(LOG_TAG, "error while closing GPS NMEA output stream", e);
+ } finally {
+ try {
+ Log.d(LOG_TAG, "closing Bluetooth GPS input streams");
+ out2.close();
+ out.close();
+ } catch (IOException e) {
+ Log.e(LOG_TAG, "error while closing GPS input streams", e);
+ } finally {
+ try {
+ Log.d(LOG_TAG, "closing Bluetooth GPS socket");
+ socket.close();
+ } catch (IOException e) {
+ Log.e(LOG_TAG, "error while closing GPS socket", e);
+ }
+ }
+ }
+ }
}
private Service callingService;
@@ -304,6 +335,9 @@ public class BlueetoothGpsManager {
Log.v(LOG_TAG, "current device: "+gpsDevice.getName() + " -- " + gpsDevice.getAddress());
if ((bluetoothAdapter.isEnabled()) && (nbRetriesRemaining > 0 )){
try {
+ if (connectedGps != null){
+ connectedGps.close();
+ }
if (gpsSocket != null){
Log.d(LOG_TAG, "trying to close old socket");
gpsSocket.close();
@@ -444,17 +478,36 @@ public class BlueetoothGpsManager {
if (enabled){
Log.d(LOG_TAG, "disabling Bluetooth GPS manager");
enabled = false;
- if (gpsSocket != null){
- try {
- gpsSocket.close();
- } catch (IOException closeException) {
- Log.e(LOG_TAG, "error while closing socket", closeException);
+ connectionAndReadingPool.shutdown();
+ Runnable closeAndShutdown = new Runnable() {
+ @Override
+ public void run(){
+ try {
+ connectionAndReadingPool.awaitTermination(10, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ if (!connectionAndReadingPool.isTerminated()){
+ if (connectedGps != null){
+ connectedGps.close();
+ }
+ if (gpsSocket != null){
+ try {
+ Log.d(LOG_TAG, "closing Bluetooth GPS socket");
+ gpsSocket.close();
+ } catch (IOException closeException) {
+ Log.e(LOG_TAG, "error while closing socket", closeException);
+ }
+ }
+ connectionAndReadingPool.shutdownNow();
+ }
}
- }
+ };
+ notificationPool.execute(closeAndShutdown);
nmeaListeners.clear();
disableMockLocationProvider();
notificationPool.shutdown();
- connectionAndReadingPool.shutdown();
+// connectionAndReadingPool.shutdown();
callingService.stopSelf();
Log.d(LOG_TAG, "Bluetooth GPS manager disabled");
}