diff options
author | Herbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com> | 2010-09-26 01:33:30 +0200 |
---|---|---|
committer | Herbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com> | 2010-09-26 01:33:30 +0200 |
commit | 80b98b1f2743f89244b0ed1dc66000e5a92f8d08 (patch) | |
tree | bdd41fdd5106263d40cb7f79d19c62d7f3969221 | |
parent | 1a394d2c37a50dd2b7ba00a480cda41dd7e30ebf (diff) | |
download | BlueGPS-80b98b1f2743f89244b0ed1dc66000e5a92f8d08.zip BlueGPS-80b98b1f2743f89244b0ed1dc66000e5a92f8d08.tar.gz BlueGPS-80b98b1f2743f89244b0ed1dc66000e5a92f8d08.tar.bz2 |
reconnect on unexpected disconnection
3 files changed, 77 insertions, 22 deletions
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index 7f2a5fb..14cca52 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -32,6 +32,8 @@ import java.util.List; import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
import org.broeuschmeul.android.gps.nmea.util.NmeaParser;
@@ -42,16 +44,18 @@ import android.bluetooth.BluetoothSocket; import android.content.Context;
import android.location.LocationManager;
import android.location.GpsStatus.NmeaListener;
+import android.os.SystemClock;
import android.util.Log;
public class BlueetoothGpsManager {
- private class ConnectedThread extends Thread {
+ private class ConnectedGps extends Thread {
private final InputStream in;
private final OutputStream out;
private final PrintStream out2;
+ private boolean ready = false;
- public ConnectedThread(BluetoothSocket socket) {
+ public ConnectedGps(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
PrintStream tmpOut2 = null;
@@ -73,19 +77,32 @@ public class BlueetoothGpsManager { try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"US-ASCII"));
String s;
- while((enabled && (s = reader.readLine()) != null)){
+ long now = SystemClock.uptimeMillis();
+ long lastRead = now;
+// while((enabled && (s = reader.readLine()) != null)){
+ while((enabled) && (now < lastRead+5000 )){
+ if (reader.ready()){
+ s = reader.readLine();
Log.e("BT test", "data: "+System.currentTimeMillis()+" "+s + "xxx");
notifyNmeaSentence(s+"\r\n");
+ ready = true;
+ lastRead = SystemClock.uptimeMillis();
// parser.parseNmeaSentence(s);
// // writer.println(s);
// addNMEAString(s);
// nmeaSentenceHandler.ob
+ } else {
+ Log.e("BT test", "data: not ready "+System.currentTimeMillis());
+ SystemClock.sleep(500);
+ }
+ now = SystemClock.uptimeMillis();
}
} catch (IOException e) {
Log.e("BT test", "error while getting data", e);
setMockLocationProviderOutOfService();
} finally {
- disable();
+ // remove because we want to retry...
+// disable();
}
}
@@ -96,6 +113,7 @@ public class BlueetoothGpsManager { public void write(byte[] buffer) {
try {
out.write(buffer);
+ out.flush();
} catch (IOException e) {
// Log.e(TAG, "Exception during write", e);
}
@@ -121,11 +139,12 @@ public class BlueetoothGpsManager { private NmeaParser parser = new NmeaParser(10f);
private boolean enabled = false;
private ExecutorService notificationPool;
+ private ScheduledExecutorService connectionAndReadingPool;
private List<NmeaListener> nmeaListeners = Collections.synchronizedList(new LinkedList<NmeaListener>());
private LocationManager locationManager;
// private boolean mockGpsEnabled = true;
// private String mockLocationProvider = LocationManager.GPS_PROVIDER;
- private ConnectedThread connectedThread;
+ private ConnectedGps connectedGps;
// private Handler nmeaSentenceHandler = new Handler();
@@ -133,7 +152,7 @@ public class BlueetoothGpsManager { /**
* @return true if the bluetooth GPS is enabled
*/
- public boolean isEnabled() {
+ public synchronized boolean isEnabled() {
return enabled;
}
@@ -144,9 +163,8 @@ public class BlueetoothGpsManager { parser.setLocationManager(locationManager);
}
- public synchronized void enable() {
+ public synchronized boolean enable() {
if (! enabled){
- this.enabled = true;
final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// Device does not support Bluetooth
@@ -156,7 +174,7 @@ public class BlueetoothGpsManager { // startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Log.e("BT test", "Bluetooth is not enabled");
} else {
- BluetoothDevice gpsDevice = bluetoothAdapter.getRemoteDevice(gpsDeviceAddress);
+ final BluetoothDevice gpsDevice = bluetoothAdapter.getRemoteDevice(gpsDeviceAddress);
if (gpsDevice == null){
Log.e("BT test", "GPS device not found");
} else {
@@ -170,16 +188,39 @@ 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++;
+ 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();
+ }
+ } 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();
- try {
+ // 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();
- connectedThread = new ConnectedThread(gpsSocket);
- connectedThread.start();
+ // connection obtained so reset the number of connection try
+ connectionTry=0;
+ connectedGps = new ConnectedGps(gpsSocket);
+ connectionAndReadingPool.execute(connectedGps);
+// connectedGps.start();
// String command = callingService.getString(R.string.sirf_gll_on);
// String sentence = String.format((Locale)null,"$%s*%X\r\n", command, parser.computeChecksum(command));
// String command1 = callingService.getString(R.string.sirf_gll_off);
@@ -193,26 +234,35 @@ public class BlueetoothGpsManager { // e.printStackTrace();
// }
// Log.e("BT test", "sending NMEA sentence: "+"$PSRF105,1*3E\r\n");
-// connectedThread.write("$PSRF105,1*3E\r\n");
+// connectedGps.write("$PSRF105,1*3E\r\n");
// Log.e("BT test", "sending NMEA sentence: "+sentence1);
-// connectedThread.write(sentence1);
+// connectedGps.write(sentence1);
// Log.e("BT test", "sending NMEA sentence: "+sentence2);
-// connectedThread.write(sentence2);
+// connectedGps.write(sentence2);
+ }
} catch (IOException connectException) {
// Unable to connect; close everything and get out
Log.e("BT test", "error while connecting to socket", connectException);
+// callingService.stopSelf();
+ } finally {
+ // 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 > 5 )){
disable();
-// callingService.stopSelf();
}
}
+ }
};
+ this.enabled = true;
notificationPool = Executors.newSingleThreadExecutor();
- notificationPool.execute(connectThread);
+ connectionAndReadingPool = Executors.newSingleThreadScheduledExecutor();
+ connectionAndReadingPool.scheduleWithFixedDelay(connectThread, 100, 60000, TimeUnit.MILLISECONDS);
// enableMockLocationProvider(LocationManager.GPS_PROVIDER);
}
}
}
}
+ return this.enabled;
}
public synchronized void disable() {
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java index 456c752..9ac1b07 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java @@ -105,7 +105,6 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener Intent myIntent = new Intent(this, BluetoothGpsActivity.class);
PendingIntent myPendingIntent = PendingIntent.getActivity(this, 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(getApplicationContext(), this.getString(R.string.foreground_service_started_notification_title), this.getString(R.string.foreground_gps_provider_started_notification), myPendingIntent);
- startForeground(R.string.foreground_gps_provider_started_notification, notification);
if (BluetoothAdapter.checkBluetoothAddress(deviceAddress)){
String mockProvider = LocationManager.GPS_PROVIDER;
if (! sharedPreferences.getBoolean(PREF_REPLACE_STD_GPS, true)){
@@ -114,13 +113,16 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener gpsManager = new BlueetoothGpsManager(this, deviceAddress);
gpsManager.enableMockLocationProvider(mockProvider);
// gpsManager.enableMockLocationProvider(LocationManager.GPS_PROVIDER);
- gpsManager.enable();
- if (! sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, false)){
- edit.putBoolean(PREF_START_GPS_PROVIDER,true);
+ boolean enabled = gpsManager.enable();
+ if (sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, false) != enabled){
+ edit.putBoolean(PREF_START_GPS_PROVIDER,enabled);
edit.commit();
}
+ if (enabled) {
+ startForeground(R.string.foreground_gps_provider_started_notification, notification);
toast.setText(this.getString(R.string.msg_gps_provider_started));
toast.show();
+ }
} else {
// if (! sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, true)){
// edit.putBoolean(PREF_START_GPS_PROVIDER,false);
diff --git a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java index 5a3ac08..b690285 100644 --- a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java +++ b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java @@ -178,11 +178,13 @@ public class NmeaParser { }
// parse NMEA Sentence
- public void parseNmeaSentence(String gpsSentence){
+ public String parseNmeaSentence(String gpsSentence){
+ String nmeaSentence = null;
Log.e("BT test", "data: "+System.currentTimeMillis()+" "+gpsSentence);
Pattern xx = Pattern.compile("\\$([^*$]*)\\*([0-9A-F][0-9A-F])?\r\n");
Matcher m = xx.matcher(gpsSentence);
if (m.matches()){
+ nmeaSentence = m.group(0);
String sentence = m.group(1);
String checkSum = m.group(2);
Log.e("BT test", "data: "+System.currentTimeMillis()+" "+sentence+" cheksum; "+checkSum +" control: "+String.format("%X",computeChecksum(sentence)));
@@ -448,6 +450,7 @@ public class NmeaParser { // Mode indicator, (A=autonomous, D=differential, E=Estimated, N=not valid, S=Simulator )
}
}
+ return nmeaSentence;
}
public double parseNmeaLatitude(String lat,String orientation){
|