diff options
Diffstat (limited to 'src/org/broeuschmeul/android')
-rw-r--r-- | src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java | 71 | ||||
-rw-r--r-- | src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java | 44 |
2 files changed, 68 insertions, 47 deletions
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index 8a373b7..d835df7 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -52,6 +52,7 @@ public class BlueetoothGpsManager { private final InputStream in;
private final OutputStream out;
private final PrintStream out2;
+ private boolean ready = false;
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
@@ -78,6 +79,7 @@ public class BlueetoothGpsManager { while((enabled && (s = reader.readLine()) != null)){
Log.e("BT test", "data: "+System.currentTimeMillis()+" "+s + "xxx");
notifyNmeaSentence(s+"\r\n");
+ ready = true;
// parser.parseNmeaSentence(s);
// // writer.println(s);
// addNMEAString(s);
@@ -97,9 +99,15 @@ public class BlueetoothGpsManager { */
public void write(byte[] buffer) {
try {
+ do {
+ Thread.sleep(100);
+ } while (! ready);
out.write(buffer);
+ out.flush();
} catch (IOException e) {
-// Log.e(TAG, "Exception during write", e);
+ Log.e("BT test", "Exception during write", e);
+ } catch (InterruptedException e) {
+ Log.e("BT test", "Exception during write", e);
}
}
/**
@@ -108,10 +116,15 @@ public class BlueetoothGpsManager { */
public void write(String buffer) {
try {
+ do {
+ Thread.sleep(100);
+ } while (! ready);
out2.print(buffer);
- out.flush();
- } catch (IOException e) {
-// Log.e(TAG, "Exception during write", e);
+ out2.flush();
+// } catch (IOException e) {
+// Log.e("BT test", "Exception during write", e);
+ } catch (InterruptedException e) {
+ Log.e("BT test", "Exception during write", e);
}
}
}
@@ -135,7 +148,7 @@ public class BlueetoothGpsManager { /**
* @return true if the bluetooth GPS is enabled
*/
- public boolean isEnabled() {
+ public synchronized boolean isEnabled() {
return enabled;
}
@@ -146,9 +159,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
@@ -157,7 +169,7 @@ public class BlueetoothGpsManager { // Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Log.e("BT test", "Bluetooth is not enabled");
- } else {
+ } else {
BluetoothDevice gpsDevice = bluetoothAdapter.getRemoteDevice(gpsDeviceAddress);
if (gpsDevice == null){
Log.e("BT test", "GPS device not found");
@@ -200,6 +212,9 @@ public class BlueetoothGpsManager { // connectedThread.write(sentence1);
// Log.e("BT test", "sending NMEA sentence: "+sentence2);
// connectedThread.write(sentence2);
+// Thread.sleep(5000);
+// } catch (InterruptedException e) {
+// Log.e("BT test", "error while connecting to socket", e);
} catch (IOException connectException) {
// Unable to connect; close everything and get out
Log.e("BT test", "error while connecting to socket", connectException);
@@ -208,6 +223,7 @@ public class BlueetoothGpsManager { }
}
};
+ this.enabled = true;
notificationPool = Executors.newSingleThreadExecutor();
notificationPool.execute(connectThread);
// enableMockLocationProvider(LocationManager.GPS_PROVIDER);
@@ -215,6 +231,7 @@ public class BlueetoothGpsManager { }
}
}
+ return this.enabled;
}
public synchronized void disable() {
@@ -319,29 +336,33 @@ public class BlueetoothGpsManager { public void sendPackagedNmeaCommand(final String command){
Log.e("BT test", "sending NMEA sentence: "+command);
- notificationPool.execute( new Runnable() {
- @Override
- public void run() {
- if (isEnabled() && (connectedThread != null)){
- connectedThread.write(command);
- Log.e("BT test", "sent NMEA sentence: "+command);
+ if (isEnabled()){
+ notificationPool.execute( new Runnable() {
+ @Override
+ public void run() {
+ if (isEnabled() && (connectedThread != null)){
+ connectedThread.write(command);
+ Log.e("BT test", "sent NMEA sentence: "+command);
+ }
}
- }
- });
+ });
+ }
}
public void sendPackagedSirfCommand(final String commandHexa){
Log.e("BT test", "sending SIRF sentence: "+commandHexa);
- final byte[] command = SirfUtils.genSirfCommand(commandHexa);
- notificationPool.execute( new Runnable() {
- @Override
- public void run() {
- if (isEnabled() && (connectedThread != null)){
- connectedThread.write(command);
- Log.e("BT test", "sent SIRF sentence: "+commandHexa);
+ if (isEnabled()){
+ final byte[] command = SirfUtils.genSirfCommand(commandHexa);
+ notificationPool.execute( new Runnable() {
+ @Override
+ public void run() {
+ if (isEnabled() && (connectedThread != null)){
+ connectedThread.write(command);
+ Log.e("BT test", "sent SIRF sentence: "+commandHexa);
+ }
}
- }
- });
+ });
+ }
}
public void sendNmeaCommand(String sentence){
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java index 1fffc86..37ac056 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java @@ -117,7 +117,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)){
@@ -126,7 +125,7 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener gpsManager = new BlueetoothGpsManager(this, deviceAddress);
gpsManager.enableMockLocationProvider(mockProvider);
// gpsManager.enableMockLocationProvider(LocationManager.GPS_PROVIDER);
- gpsManager.enable();
+ boolean enabled = gpsManager.enable();
// String command1 = this.getString(R.string.sirf_nmea_gga_on);
// String command2 = this.getString(R.string.sirf_nmea_gsa_off);
// String command3 = this.getString(R.string.sirf_nmea_gsv_off);
@@ -141,15 +140,18 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener // gpsManager.sendNmeaCommand(command6);
// Bundle extras = intent.getExtras();
- if (sharedPreferences.getBoolean(PREF_SIRF_GPS, false)){
- enableSirfConfig(sharedPreferences);
- }
- if (! sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, false)){
- edit.putBoolean(PREF_START_GPS_PROVIDER,true);
+ if (sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, false) != enabled){
+ edit.putBoolean(PREF_START_GPS_PROVIDER,enabled);
edit.commit();
}
- toast.setText(this.getString(R.string.msg_gps_provider_started));
- toast.show();
+ if (enabled) {
+ startForeground(R.string.foreground_gps_provider_started_notification, notification);
+ if (sharedPreferences.getBoolean(PREF_SIRF_GPS, false)){
+ enableSirfConfig(sharedPreferences);
+ }
+ 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);
@@ -228,15 +230,14 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener if (extras.containsKey(PREF_SIRF_ENABLE_ZDA)){
enableNmeaZDA(extras.getBoolean(PREF_SIRF_ENABLE_ZDA, false));
}
- if (extras.containsKey(PREF_SIRF_ENABLE_SBAS)){
- enableSBAS(extras.getBoolean(PREF_SIRF_ENABLE_SBAS, true));
- }
if (extras.containsKey(PREF_SIRF_ENABLE_STATIC_NAVIGATION)){
enableStaticNavigation(extras.getBoolean(PREF_SIRF_ENABLE_STATIC_NAVIGATION, false));
- }
- if (extras.containsKey(PREF_SIRF_ENABLE_NMEA)){
+ } else if (extras.containsKey(PREF_SIRF_ENABLE_NMEA)){
enableNMEA(extras.getBoolean(PREF_SIRF_ENABLE_NMEA, true));
}
+ if (extras.containsKey(PREF_SIRF_ENABLE_SBAS)){
+ enableSBAS(extras.getBoolean(PREF_SIRF_ENABLE_SBAS, true));
+ }
}
private void enableSirfConfig(SharedPreferences extras){
if (extras.contains(PREF_SIRF_ENABLE_GLL)){
@@ -254,15 +255,14 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener if (extras.contains(PREF_SIRF_ENABLE_ZDA)){
enableNmeaZDA(extras.getBoolean(PREF_SIRF_ENABLE_ZDA, false));
}
- if (extras.contains(PREF_SIRF_ENABLE_SBAS)){
- enableSBAS(extras.getBoolean(PREF_SIRF_ENABLE_SBAS, true));
- }
if (extras.contains(PREF_SIRF_ENABLE_STATIC_NAVIGATION)){
enableStaticNavigation(extras.getBoolean(PREF_SIRF_ENABLE_STATIC_NAVIGATION, false));
- }
- if (extras.contains(PREF_SIRF_ENABLE_NMEA)){
+ } else if (extras.contains(PREF_SIRF_ENABLE_NMEA)){
enableNMEA(extras.getBoolean(PREF_SIRF_ENABLE_NMEA, true));
}
+ if (extras.contains(PREF_SIRF_ENABLE_SBAS)){
+ enableSBAS(extras.getBoolean(PREF_SIRF_ENABLE_SBAS, true));
+ }
gpsManager.sendNmeaCommand(this.getString(R.string.sirf_nmea_gga_on));
gpsManager.sendNmeaCommand(this.getString(R.string.sirf_nmea_rmc_on));
}
@@ -354,14 +354,14 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener if (gpsManager != null){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
// SharedPreferences.Editor edit = sharedPreferences.edit();
- boolean isInNmeaMode = sharedPreferences.getBoolean(PREF_SIRF_ENABLE_NMEA, false);
+ boolean isInNmeaMode = sharedPreferences.getBoolean(PREF_SIRF_ENABLE_NMEA, true);
if (isInNmeaMode){
enableNMEA(false);
}
if (enable){
- gpsManager.sendNmeaCommand(getString(R.string.sirf_bin_static_nav_on));
+ gpsManager.sendSirfCommand(getString(R.string.sirf_bin_static_nav_on));
} else {
- gpsManager.sendNmeaCommand(getString(R.string.sirf_bin_static_nav_off));
+ gpsManager.sendSirfCommand(getString(R.string.sirf_bin_static_nav_off));
}
if (isInNmeaMode){
enableNMEA(true);
|