From 724611684cca34c24700d8113d34a8805bb53667 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Fri, 28 Sep 2012 21:02:27 +0200 Subject: [PATCH 01/15] add tempo (usefull when using plain file instead of tty dev) --- .../android/gps/bluetooth/provider/BlueetoothGpsManager.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index 29163f01..cfb08a46 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -147,6 +147,7 @@ public void run() { SystemClock.sleep(500); } now = SystemClock.uptimeMillis(); + SystemClock.sleep(10); } } catch (IOException e) { Log.e(LOG_TAG, "error while getting data", e); From 431b472e6b27e3d7b35ecf70a6d8593ca39f9482 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Tue, 16 Oct 2012 00:10:29 +0200 Subject: [PATCH 02/15] UsbGps4Droid dev version implemented using USB host API This version works using Android SDK USB Host API. It should work with Android version greater or equal than Honeycomb MR1 (Android SDK version 12) and needs and Android device with USB host support. Using Globalsat BU-353 it will automatically launch UsbGps4Droid. --- AndroidManifest.xml | 7 +- project.properties | 2 +- res/values/constants.xml | 2 +- res/xml/device_filter.xml | 4 + res/xml/pref.xml | 4 +- .../provider/BlueetoothGpsManager.java | 545 +++++++++++++----- .../provider/BluetoothGpsActivity.java | 99 +++- .../provider/BluetoothGpsProviderService.java | 2 +- 8 files changed, 493 insertions(+), 172 deletions(-) create mode 100644 res/xml/device_filter.xml diff --git a/AndroidManifest.xml b/AndroidManifest.xml index bec86833..8f2b221c 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -26,7 +26,8 @@ android:versionCode="126" android:versionName="@string/versionName"> - + + @@ -40,6 +41,10 @@ + + + + 1200 2400 4800 9600 19200 38400 + 38400 + 57600 + 115200 diff --git a/res/xml/pref.xml b/res/xml/pref.xml index f77073ff..d1bd83f7 100644 --- a/res/xml/pref.xml +++ b/res/xml/pref.xml @@ -30,7 +30,7 @@ android:key="@string/pref_gps_device_key" android:summary="@string/pref_gps_device_summary" /> 0)){ +// speed = speedList[k-1]; +// } if (BuildConfig.DEBUG || debug) Log.w(LOG_TAG, "data read: nb: " + n + " " + Arrays.toString(usbBuffer)); if (n > 0){ if (n > bufferWrite.remaining()){ @@ -373,10 +398,18 @@ public void run() { // } // s = sentence.toString(); if (s != null){ - Log.v(LOG_TAG, "data: "+System.currentTimeMillis()+" "+s); - notifyNmeaSentence(s+"\r\n"); - ready = true; - lastRead = SystemClock.uptimeMillis(); + Log.v(LOG_TAG, "data: "+System.currentTimeMillis()+" "+s); + if (notifyNmeaSentence(s+"\r\n")){ + ready = true; + lastRead = SystemClock.uptimeMillis(); + if (nbRetriesRemaining < maxConnectionRetries){ + // reset eventual disabling cause + // setDisableReason(0); + // connection is good so reseting the number of connection try + nbRetriesRemaining = maxConnectionRetries ; + notificationManager.cancel(R.string.connection_problem_notification_title); + } + } } else { Log.d(LOG_TAG, "data: not ready "+System.currentTimeMillis()); SystemClock.sleep(500); @@ -481,6 +514,7 @@ public void close(){ private boolean connected = false; private boolean setDeviceSpeed = false; private String deviceSpeed = "auto"; + private String defaultDeviceSpeed = "4800"; /** * @param callingService @@ -496,7 +530,8 @@ public BlueetoothGpsManager(Service callingService, String deviceAddress, int ma locationManager = (LocationManager)callingService.getSystemService(Context.LOCATION_SERVICE); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(callingService); deviceSpeed = sharedPreferences.getString(BluetoothGpsProviderService.PREF_GPS_DEVICE_SPEED, callingService.getString(R.string.defaultGpsDeviceSpeed)); - setDeviceSpeed = !deviceSpeed.equals(callingService.getString(R.string.defaultGpsDeviceSpeed)); + defaultDeviceSpeed = callingService.getString(R.string.defaultGpsDeviceSpeed); + setDeviceSpeed = !deviceSpeed.equals(callingService.getString(R.string.autoGpsDeviceSpeed)); notificationManager = (NotificationManager)callingService.getSystemService(Context.NOTIFICATION_SERVICE); parser.setLocationManager(locationManager); @@ -605,13 +640,19 @@ public void run() { if (device != null && usbManager.hasPermission(device)){ Log.d(LOG_TAG, "We have permession, good!"); connected = true; - // reset eventual disabling cause - setDisableReason(0); - // connection obtained so reset the number of connection try - nbRetriesRemaining = 1+maxConnectionRetries ; - notificationManager.cancel(R.string.connection_problem_notification_title); + if (setDeviceSpeed){ + Log.v(LOG_TAG, "will set devive speed: " + deviceSpeed); + } else { + Log.v(LOG_TAG, "will use default device speed: " + defaultDeviceSpeed); + deviceSpeed = defaultDeviceSpeed; + } +// // reset eventual disabling cause +// // setDisableReason(0); +// // connection obtained so reset the number of connection try +// nbRetriesRemaining = 1+maxConnectionRetries ; +// notificationManager.cancel(R.string.connection_problem_notification_title); Log.v(LOG_TAG, "starting socket reading task"); - connectedGps = new ConnectedGps(device); + connectedGps = new ConnectedGps(device, deviceSpeed); connectionAndReadingPool.execute(connectedGps); Log.v(LOG_TAG, "socket reading thread started"); } else if (device != null) { @@ -1039,8 +1080,10 @@ public void removeNmeaListener(NmeaListener listener){ * Notifies the reception of a NMEA sentence from the bluetooth GPS to registered NMEA listeners. * * @param nmeaSentence the complete NMEA sentence received from the bluetooth GPS (i.e. $....*XY where XY is the checksum) + * @return true if the input string is a valid NMEA sentence, false otherwise. */ - private void notifyNmeaSentence(final String nmeaSentence){ + private boolean notifyNmeaSentence(final String nmeaSentence){ + boolean res = false; if (enabled){ Log.v(LOG_TAG, "parsing and notifying NMEA sentence: "+nmeaSentence); String sentence = null; @@ -1055,6 +1098,7 @@ private void notifyNmeaSentence(final String nmeaSentence){ final String recognizedSentence = sentence; final long timestamp = System.currentTimeMillis(); if (recognizedSentence != null){ + res = true; Log.v(LOG_TAG, "notifying NMEA sentence: "+recognizedSentence); synchronized(nmeaListeners) { for(final NmeaListener listener : nmeaListeners){ @@ -1068,6 +1112,7 @@ public void run() { } } } + return res; } /** From f15ddeae92266fdbbc0953121ff5fd5110a43435 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sun, 4 Nov 2012 19:48:48 +0100 Subject: [PATCH 04/15] Correction in NMEA checksum calcul --- .../android/gps/bluetooth/provider/BlueetoothGpsManager.java | 2 +- src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index 75643c0b..b390f6d9 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -1171,7 +1171,7 @@ public void sendPackagedSirfCommand(final String commandHexa){ * @param sentence the NMEA sentence without the first "$", the last "*" and the checksum. */ public void sendNmeaCommand(String sentence){ - String command = String.format((Locale)null,"$%s*%X\r\n", sentence, parser.computeChecksum(sentence)); + String command = String.format((Locale)null,"$%s*%02X\r\n", sentence, parser.computeChecksum(sentence)); sendPackagedNmeaCommand(command); } diff --git a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java index cadc5993..d226a0a2 100644 --- a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java +++ b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java @@ -221,7 +221,7 @@ public String parseNmeaSentence(String gpsSentence) throws SecurityException { nmeaSentence = m.group(0); String sentence = m.group(1); String checkSum = m.group(2); - Log.v(LOG_TAG, "data: "+System.currentTimeMillis()+" "+sentence+" cheksum; "+checkSum +" control: "+String.format("%X",computeChecksum(sentence))); + Log.v(LOG_TAG, "data: "+System.currentTimeMillis()+" "+sentence+" cheksum; "+checkSum +" control: "+String.format("%02X",computeChecksum(sentence))); SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(','); splitter.setString(sentence); String command = splitter.next(); From c0c063a4db1f69b0da33c333f32fda68127bfdac Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sun, 4 Nov 2012 20:45:14 +0100 Subject: [PATCH 05/15] bug correction in NMEA checksum management When NMEA sentences had no checksum the paser failed to match the sentences because it was expecting a "*" at the end of the sentences which was wrong. --- src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java index d226a0a2..2c5db10c 100644 --- a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java +++ b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java @@ -215,13 +215,13 @@ private void notifyStatusChanged(int status, Bundle extras, long updateTime){ public String parseNmeaSentence(String gpsSentence) throws SecurityException { String nmeaSentence = null; Log.v(LOG_TAG, "data: "+System.currentTimeMillis()+" "+gpsSentence); - Pattern xx = Pattern.compile("\\$([^*$]*)\\*([0-9A-F][0-9A-F])?\r\n"); + 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.v(LOG_TAG, "data: "+System.currentTimeMillis()+" "+sentence+" cheksum; "+checkSum +" control: "+String.format("%02X",computeChecksum(sentence))); + Log.v(LOG_TAG, "data: "+System.currentTimeMillis()+" "+sentence+" cheksum: "+checkSum +" control: "+String.format("%02X",computeChecksum(sentence))); SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(','); splitter.setString(sentence); String command = splitter.next(); From eb96ce7bffe98f4f3510353d5970a9c51425bb56 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sun, 4 Nov 2012 20:55:17 +0100 Subject: [PATCH 06/15] add new constants to use gps baud rate speed in SiFR/NMEA messages --- res/values/constants.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/res/values/constants.xml b/res/values/constants.xml index 19bae87d..e551c37f 100644 --- a/res/values/constants.xml +++ b/res/values/constants.xml @@ -74,6 +74,7 @@ PSRF103,08,00,00,01 PSRF100,0,38400,8,1,0 + PSRF100,0,%d,8,1,0 PSRF151,01 PSRF151,00 @@ -83,6 +84,7 @@ 8702 810201010001050105010001000100010001000100019600 8102%02X01%02X01%02X01%02X01%02X01%02X01%02X01%02X01%02X0100019600 + 8102%02X01%02X01%02X01%02X01%02X01%02X01%02X01%02X01%02X010001%04X 85020000000000 85010000000000 8400 From c1b27fa44a3dd49cc1499c2467da1a4a536c5d7d Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sun, 4 Nov 2012 20:57:08 +0100 Subject: [PATCH 07/15] remove duplicated entry in gps baud rate list --- res/values/strings.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index fc35c796..92fd7942 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -137,7 +137,6 @@ 9600 19200 38400 - 38400 57600 115200 From 9bc2ad816171b87ccaf2f8dd1084c7a2cf7428fb Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sun, 4 Nov 2012 20:59:44 +0100 Subject: [PATCH 08/15] cosmetic changed --- .../android/gps/bluetooth/provider/BluetoothGpsActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java index 68ac40e6..7e66d4f7 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java @@ -119,7 +119,7 @@ private void updateDevicePreferenceSummary(){ String deviceDisplayedName = ""; if (! usbManager.getDeviceList().isEmpty() && usbManager.getDeviceList().get(deviceName) != null){ deviceDisplayedName = usbManager.getDeviceList().get(deviceName).getDeviceName(); - } else if ((usbManager.getDeviceList().size() == 1) && usbManager.getDeviceList().get(defaultDeviceName) != null){ + } else if ((usbManager.getDeviceList().size() == 1) && (usbManager.getDeviceList().get(defaultDeviceName) != null)){ deviceDisplayedName = usbManager.getDeviceList().get(defaultDeviceName).getDeviceName(); deviceName = defaultDeviceName; prefDevices.setValue(defaultDeviceName); From 3f45af4580c521ef22e91e0c7d4f8e9b471706dc Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sun, 4 Nov 2012 21:46:49 +0100 Subject: [PATCH 09/15] SiRF options reactivation --- res/xml/pref.xml | 2 +- .../provider/BlueetoothGpsManager.java | 334 ++++++++++-------- .../provider/BluetoothGpsProviderService.java | 31 +- 3 files changed, 216 insertions(+), 151 deletions(-) diff --git a/res/xml/pref.xml b/res/xml/pref.xml index d1bd83f7..f3674afd 100644 --- a/res/xml/pref.xml +++ b/res/xml/pref.xml @@ -83,7 +83,7 @@ android:summary="@string/pref_connection_retries_summary" /> 1){ + UsbEndpoint curEndpoint = intf.getEndpoint(--i); + if ((curEndpoint.getDirection() == UsbConstants.USB_DIR_IN) && (curEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)){ + endpointIn = curEndpoint; + } + if ((curEndpoint.getDirection() == UsbConstants.USB_DIR_OUT) && (curEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)){ + endpointOut = curEndpoint; + } + if ((endpointIn != null) && (endpointOut != null)){ + i = 0; + } + } +// endpointIn = intf.getEndpoint(2); final int TIMEOUT = 1000; // final int TIMEOUT = 0; connection = usbManager.openDevice(device); @@ -202,25 +196,12 @@ public ConnectedGps(UsbDevice device, String deviceSpeed) { Log.d(LOG_TAG, "claiming interface"); resclaim = connection.claimInterface(intf, true); Log.d(LOG_TAG, "data claim"+ resclaim); - // Connection initialization: 4800 baud and 8N1 (0 bits no parity 1 stop bit) - Log.d(LOG_TAG, "initializing connection: 4800 baud and 8N1 (0 bits no parity 1 stop bit"); - final byte[] data = { (byte) 0xC0, 0x12, 0x00, 0x00, 0x00, 0x00, 0x08 }; - final ByteBuffer connectionSpeedBuffer = ByteBuffer.wrap(data, 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN); - connectionSpeedBuffer.putInt(Integer.parseInt(deviceSpeed)); - int res1 = connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0); - int res2 = connection.controlTransfer(0x21, 32, 0, 0, data, 7, 0); - Log.e(LOG_TAG, "data init "+ res1 + " " + res2); -// connection.controlTransfer(0x40, 0, 0, 0, null, 0, 0); //reset -// connection.controlTransfer(0x40, 0, 1, 0, null, 0, 0); //clear Rx -// connection.controlTransfer(0x40, 0, 2, 0, null, 0, 0); //clear Tx -// connection.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0); //flow control none -// connection.controlTransfer(0x40, 0x03, 0x0271, 0, null, 0, 0); //baudrate 4800 see http://stackoverflow.com/questions/8546099/setting-parity-with-controltransfer-method -// connection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0); //data bit 8, parity none, stop bit 1, tx off + InputStream tmpIn = null; + OutputStream tmpOut = null; + PrintStream tmpOut2 = null; - InputStream tmpIn = new InputStream() { - int speed = 0; - final int[] speedList = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200}; + tmpIn = new InputStream() { private byte[] buffer = new byte[128]; private byte[] usbBuffer = new byte[64]; private byte[] oneByteBuffer = new byte[1]; @@ -278,8 +259,6 @@ public boolean markSupported() { return super.markSupported(); } - - private Integer i = 0; /* (non-Javadoc) * @see java.io.InputStream#read(byte[], int, int) */ @@ -291,17 +270,7 @@ public int read(byte[] buffer, int offset, int length) ByteBuffer out = ByteBuffer.wrap(buffer, offset, length); if (! bufferRead.hasRemaining()){ if (BuildConfig.DEBUG || debug) Log.i(LOG_TAG, "data read buffer empty " + Arrays.toString(usbBuffer)); - int n = connection.bulkTransfer(endpoint, usbBuffer, 64, TIMEOUT); -// int n = -1, k = 0; -// while ( (n = connection.bulkTransfer(endpoint, usbBuffer, 64, TIMEOUT)) == -1 && speed == 0 && k < 8){ -// connectionSpeedBuffer.putInt(0,speedList[k++]); -// int res1 = connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0); -// int res2 = connection.controlTransfer(0x21, 32, 0, 0, data, 7, 0); -// Log.e(LOG_TAG, "data init "+ res1 + " " + res2 + "speed: "+speedList[k-1]); -// } -// if ((n != -1) && (speed == 0) && (k > 0)){ -// speed = speedList[k-1]; -// } + int n = connection.bulkTransfer(endpointIn, usbBuffer, 64, TIMEOUT); if (BuildConfig.DEBUG || debug) Log.w(LOG_TAG, "data read: nb: " + n + " " + Arrays.toString(usbBuffer)); if (n > 0){ if (n > bufferWrite.remaining()){ @@ -368,11 +337,104 @@ public void close() throws IOException { } }; - //OutputStream tmpOut = null; - //PrintStream tmpOut2 = null; + tmpOut = new OutputStream() { + int speed = 0; + final int[] speedList = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200}; + private byte[] buffer = new byte[128]; + private byte[] usbBuffer = new byte[64]; + private byte[] oneByteBuffer = new byte[1]; + private ByteBuffer bufferWrite = ByteBuffer.wrap(buffer); + private ByteBuffer bufferRead = (ByteBuffer)ByteBuffer.wrap(buffer).limit(0); + + @Override + public void write(int oneByte) throws IOException { + if (BuildConfig.DEBUG || debug) Log.d(LOG_TAG, "trying to write data (one byte): " + oneByte + " char: " + (char)oneByte); + oneByteBuffer[0] = (byte)oneByte; + this.write(oneByteBuffer,0,1); + if (BuildConfig.DEBUG || debug) Log.d(LOG_TAG, "writen data (one byte): " + oneByte + " char: " + (char)oneByte); + } + + /* (non-Javadoc) + * @see java.io.OutputStream#write(byte[], int, int) + */ + @Override + public void write(byte[] buffer, int offset, int count) + throws IOException { + if (BuildConfig.DEBUG || debug) Log.d(LOG_TAG, "trying to write data : " + Arrays.toString(buffer) + " offset " + offset + " count: " + count); + bufferWrite.clear(); + bufferWrite.put(buffer, offset, count); + if (BuildConfig.DEBUG || debug) Log.d(LOG_TAG, "trying to write data : " + Arrays.toString(this.buffer)); + int n = connection.bulkTransfer(endpointOut, this.buffer, count, TIMEOUT); + if (n != count){ + Log.e(LOG_TAG, "error while trying to write data : " + Arrays.toString(this.buffer)); + Log.e(LOG_TAG, "error while trying to write data : " + n + " bytes writen when expecting " + count); + throw new IOException("error while trying to write data : " + Arrays.toString(this.buffer)); + } + if (BuildConfig.DEBUG || debug) Log.d(LOG_TAG, "writen data (one byte): " + Arrays.toString(this.buffer)); + } + /* (non-Javadoc) + * @see java.io.OutputStream#close() + */ + @Override + public void close() throws IOException { + // TODO Auto-generated method stub + super.close(); + } + + /* (non-Javadoc) + * @see java.io.OutputStream#flush() + */ + @Override + public void flush() throws IOException { + // TODO Auto-generated method stub + super.flush(); + } + + /* (non-Javadoc) + * @see java.io.OutputStream#write(byte[]) + */ + @Override + public void write(byte[] buffer) throws IOException { + // TODO Auto-generated method stub + super.write(buffer); + } + + }; + + try { + if (tmpOut != null){ + tmpOut2 = new PrintStream(tmpOut, false, "US-ASCII"); + } + } catch (UnsupportedEncodingException e) { + Log.e(LOG_TAG, "error while getting usb output streams", e); + } + in = tmpIn; - //out = tmpOut; - //out2 = tmpOut2; + out = tmpOut; + out2 = tmpOut2; + final int res1 = connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0); + ready = false; +// connection.controlTransfer(0x40, 0, 0, 0, null, 0, 0); //reset +// connection.controlTransfer(0x40, 0, 1, 0, null, 0, 0); //clear Rx +// connection.controlTransfer(0x40, 0, 2, 0, null, 0, 0); //clear Tx +// connection.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0); //flow control none +// connection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0); //baudrate 9600 +// connection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0); //data bit 8, parity none, stop bit 1, tx off + +// final byte[] datax = new byte[7]; +// final ByteBuffer connectionSpeedBufferx = ByteBuffer.wrap(datax,0,7).order(java.nio.ByteOrder.LITTLE_ENDIAN); +// int res0 = connection.controlTransfer(0xA1, 33, 0, 0, datax, 7, 0); +// Log.e(LOG_TAG, "info connection: " + Arrays.toString(datax)); + // Connection initialization: 4800 baud and 8N1 (0 bits no parity 1 stop bit) + Log.d(LOG_TAG, "initializing connection: 4800 baud and 8N1 (0 bits no parity 1 stop bit"); + final byte[] data = { (byte) 0xC0, 0x12, 0x00, 0x00, 0x00, 0x00, 0x08 }; + final ByteBuffer connectionSpeedBuffer = ByteBuffer.wrap(data, 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN); + connectionSpeedBuffer.putInt(Integer.parseInt(deviceSpeed)); +// int res1 = connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0); + int res2 = connection.controlTransfer(0x21, 32, 0, 0, data, 7, 0); + Log.e(LOG_TAG, "data init "+ res1 + " " + res2); + + ready = ((endpointOut != null) && (out != null) && (out2 != null)); } public boolean isReady(){ @@ -390,13 +452,6 @@ public void run() { // if (true || reader.ready()) { s = reader.readLine(); -// StringBuilder sentence = new StringBuilder(50); -// for (char c = (char)in.read() ; c != '\r' && c != '\n'; c = (char)in.read()){ -//// for (char c = (char)reader.read() ; c != '\r' && c != '\n'; c = (char)reader.read()){ -// Log.v(LOG_TAG, "data: "+System.currentTimeMillis()+" "+sentence+ " "+c); -// sentence.append(c); -// } -// s = sentence.toString(); if (s != null){ Log.v(LOG_TAG, "data: "+System.currentTimeMillis()+" "+s); if (notifyNmeaSentence(s+"\r\n")){ @@ -406,6 +461,7 @@ public void run() { // reset eventual disabling cause // setDisableReason(0); // connection is good so reseting the number of connection try + Log.v(LOG_TAG, "connection is good so reseting the number of connection try"); nbRetriesRemaining = maxConnectionRetries ; notificationManager.cancel(R.string.connection_problem_notification_title); } @@ -415,8 +471,8 @@ public void run() { SystemClock.sleep(500); } } - now = SystemClock.uptimeMillis(); // SystemClock.sleep(10); + now = SystemClock.uptimeMillis(); } } catch (IOException e) { Log.e(LOG_TAG, "error while getting data", e); @@ -432,38 +488,38 @@ public void run() { * Write to the connected OutStream. * @param buffer The bytes to write */ -// public void write(byte[] buffer) { -// try { -// do { -// Thread.sleep(100); -// } while ((enabled) && (! ready)); -// if ((enabled) && (ready)){ -// out.write(buffer); -// out.flush(); -// } -// } catch (IOException e) { -// Log.e(LOG_TAG, "Exception during write", e); -// } catch (InterruptedException e) { -// Log.e(LOG_TAG, "Exception during write", e); -// } -// } + public void write(byte[] buffer) { + try { + do { + Thread.sleep(100); + } while ((enabled) && (! ready)); + if ((enabled) && (ready)){ + out.write(buffer); + out.flush(); + } + } catch (IOException e) { + Log.e(LOG_TAG, "Exception during write", e); + } catch (InterruptedException e) { + Log.e(LOG_TAG, "Exception during write", e); + } + } /** * Write to the connected OutStream. * @param buffer The data to write */ -// public void write(String buffer) { -// try { -// do { -// Thread.sleep(100); -// } while ((enabled) && (! ready)); -// if ((enabled) && (ready)){ -// out2.print(buffer); -// out2.flush(); -// } -// } catch (InterruptedException e) { -// Log.e(LOG_TAG, "Exception during write", e); -// } -// } + public void write(String buffer) { + try { + do { + Thread.sleep(100); + } while ((enabled) && (! ready)); + if ((enabled) && (ready)){ + out2.print(buffer); + out2.flush(); + } + } catch (InterruptedException e) { + Log.e(LOG_TAG, "Exception during write", e); + } + } public void close(){ ready = false; @@ -473,20 +529,20 @@ public void 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 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); // } -// } + } } } } @@ -1121,22 +1177,23 @@ public void run() { * @param command the complete NMEA sentence (i.e. $....*XY where XY is the checksum). */ public void sendPackagedNmeaCommand(final String command){ - Log.e(LOG_TAG, "Disabled .... sending NMEA sentence: "+command); -// if (isEnabled()){ -// notificationPool.execute( new Runnable() { -// @Override -// public void run() { -// while ((enabled) && ((!connected) || (connectedGps == null) || (!connectedGps.isReady()))){ -// Log.v(LOG_TAG, "writing thread is not ready"); -// SystemClock.sleep(500); -// } -// if (isEnabled() && (connectedGps != null)){ -// connectedGps.write(command); -// Log.d(LOG_TAG, "sent NMEA sentence: "+command); -// } -// } -// }); -// } + Log.e(LOG_TAG, "spooling NMEA sentence: "+command); + if (isEnabled()){ + notificationPool.execute( new Runnable() { + @Override + public void run() { + while ((enabled) && ((!connected) || (connectedGps == null) || (!connectedGps.isReady()))){ + Log.v(LOG_TAG, "writing thread is not ready"); + SystemClock.sleep(500); + } + if (isEnabled() && (connectedGps != null)){ + Log.e(LOG_TAG, "sending NMEA sentence: "+command); + connectedGps.write(command); + Log.e(LOG_TAG, "sent NMEA sentence: "+command); + } + } + }); + } } /** @@ -1146,23 +1203,24 @@ public void sendPackagedNmeaCommand(final String command){ * (i.e. with the Start Sequence, Payload Length, Payload, Message Checksum and End Sequence). */ public void sendPackagedSirfCommand(final String commandHexa){ - Log.d(LOG_TAG, "Disabled sending SIRF sentence: "+commandHexa); -// if (isEnabled()){ -// final byte[] command = SirfUtils.genSirfCommand(commandHexa); -// notificationPool.execute( new Runnable() { -// @Override -// public void run() { -// while ((enabled) && ((!connected) || (connectedGps == null) || (!connectedGps.isReady()))){ -// Log.v(LOG_TAG, "writing thread is not ready"); -// SystemClock.sleep(500); -// } -// if (isEnabled() && (connectedGps != null)){ -// connectedGps.write(command); -// Log.d(LOG_TAG, "sent SIRF sentence: "+commandHexa); -// } -// } -// }); -// } + Log.e(LOG_TAG, "spooling SIRF sentence: "+commandHexa); + if (isEnabled()){ + final byte[] command = SirfUtils.genSirfCommand(commandHexa); + notificationPool.execute( new Runnable() { + @Override + public void run() { + while ((enabled) && ((!connected) || (connectedGps == null) || (!connectedGps.isReady()))){ + Log.v(LOG_TAG, "writing thread is not ready"); + SystemClock.sleep(500); + } + if (isEnabled() && (connectedGps != null)){ + Log.e(LOG_TAG, "sendind SIRF sentence: "+commandHexa); + connectedGps.write(command); + Log.e(LOG_TAG, "sent SIRF sentence: "+commandHexa); + } + } + }); + } } /** diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java index a68099ff..a5a87b7a 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java @@ -347,21 +347,28 @@ private void enableSBAS(boolean enable){ private void enableNMEA(boolean enable){ if (gpsManager != null){ + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + String deviceSpeed = sharedPreferences.getString(BluetoothGpsProviderService.PREF_GPS_DEVICE_SPEED, this.getString(R.string.defaultGpsDeviceSpeed)); + if (deviceSpeed.equals(this.getString(R.string.autoGpsDeviceSpeed))){ + deviceSpeed = this.getString(R.string.defaultGpsDeviceSpeed); + } if (enable){ - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - int gll = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_GLL, false)) ? 1 : 0 ; - int vtg = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_VTG, false)) ? 1 : 0 ; - int gsa = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_GSA, false)) ? 5 : 0 ; - int gsv = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_GSV, false)) ? 5 : 0 ; - int zda = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_ZDA, false)) ? 1 : 0 ; - int mss = 0; - int epe = 0; - int gga = 1; - int rmc = 1; - String command = getString(R.string.sirf_bin_to_nmea_38400_alt, gga, gll, gsa, gsv, rmc, vtg, mss, epe, zda); +// int gll = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_GLL, false)) ? 1 : 0 ; +// int vtg = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_VTG, false)) ? 1 : 0 ; +// int gsa = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_GSA, false)) ? 5 : 0 ; +// int gsv = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_GSV, false)) ? 5 : 0 ; +// int zda = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_ZDA, false)) ? 1 : 0 ; +// int mss = 0; +// int epe = 0; +// int gga = 1; +// int rmc = 1; +// String command = getString(R.string.sirf_bin_to_nmea_38400_alt, gga, gll, gsa, gsv, rmc, vtg, mss, epe, zda); +// String command = getString(R.string.sirf_bin_to_nmea_alt, gga, gll, gsa, gsv, rmc, vtg, mss, epe, zda, Integer.parseInt(deviceSpeed)); + String command = getString(R.string.sirf_bin_to_nmea); gpsManager.sendSirfCommand(command); } else { - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_to_binary)); +// gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_to_binary)); + gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_to_binary_alt, Integer.parseInt(deviceSpeed))); } } } From 044255a0509c5aa39cf885dd4a60fae58e1e838d Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sun, 4 Nov 2012 22:11:38 +0100 Subject: [PATCH 10/15] add auto-conf option for gps baud rate - preliminary version --- res/values/strings.xml | 2 +- .../provider/BlueetoothGpsManager.java | 59 ++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 92fd7942..aef2f6ed 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -130,7 +130,7 @@ https://github.com/HvB/UsbGps4Droid - + auto 1200 2400 4800 diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index 2b3564cf..56843319 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -200,7 +200,7 @@ public ConnectedGps(UsbDevice device, String deviceSpeed) { InputStream tmpIn = null; OutputStream tmpOut = null; PrintStream tmpOut2 = null; - + tmpIn = new InputStream() { private byte[] buffer = new byte[128]; private byte[] usbBuffer = new byte[64]; @@ -414,6 +414,7 @@ public void write(byte[] buffer) throws IOException { out2 = tmpOut2; final int res1 = connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0); ready = false; + if (setDeviceSpeed && false){ // connection.controlTransfer(0x40, 0, 0, 0, null, 0, 0); //reset // connection.controlTransfer(0x40, 0, 1, 0, null, 0, 0); //clear Rx // connection.controlTransfer(0x40, 0, 2, 0, null, 0, 0); //clear Tx @@ -435,19 +436,71 @@ public void write(byte[] buffer) throws IOException { Log.e(LOG_TAG, "data init "+ res1 + " " + res2); ready = ((endpointOut != null) && (out != null) && (out2 != null)); + } else { + Thread autoConf = new Thread(){ + + /* (non-Javadoc) + * @see java.lang.Thread#run() + */ + @Override + public void run() { + Log.d(LOG_TAG, "initializing connection: 4800 baud and 8N1 (0 bits no parity 1 stop bit"); + final int[] speedList = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200}; + final byte[] data = { (byte) 0xC0, 0x12, 0x00, 0x00, 0x00, 0x00, 0x08 }; + final ByteBuffer connectionSpeedBuffer = ByteBuffer.wrap(data, 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN); + final byte[] sirfBin2Nmea = SirfUtils.genSirfCommandFromPayload(callingService.getString(R.string.sirf_bin_to_nmea)); + try { + Thread.sleep(1000); + for (int speed : speedList){ + if (!ready){ + connectionSpeedBuffer.putInt(0, speed); + Log.e(LOG_TAG, "trying to use speed " + speed); + int res2 = connection.controlTransfer(0x21, 32, 0, 0, data, 7, 0); + if (sirfGps){ + Log.e(LOG_TAG, "trying to switch from SiRF binaray to NMEA"); + connection.bulkTransfer(endpointOut, sirfBin2Nmea, sirfBin2Nmea.length, 0); + } + Log.e(LOG_TAG, "data init "+ res1 + " " + res2); + Thread.sleep(3000); + } + } + final byte[] datax = new byte[7]; + final ByteBuffer connectionSpeedBufferx = ByteBuffer.wrap(datax,0,7).order(java.nio.ByteOrder.LITTLE_ENDIAN); + int res0 = connection.controlTransfer(0xA1, 33, 0, 0, datax, 7, 0); + Log.e(LOG_TAG, "info connection: " + Arrays.toString(datax)); + Log.e(LOG_TAG, "info connection speed: " + connectionSpeedBufferx.getInt(0)); + Thread.sleep(10000); + } catch (InterruptedException e) { + Log.e(LOG_TAG, "autoconf thread interupted", e); + } finally { + if ((!ready ) || (lastRead + 3000 < SystemClock.uptimeMillis())){ + setMockLocationProviderOutOfService(); + // cleanly closing everything... + ConnectedGps.this.close(); + BlueetoothGpsManager.this.disableIfNeeded(); + } + } + } + + }; + Log.e(LOG_TAG, "trying to find speed "); + autoConf.start(); + } } public boolean isReady(){ return ready; } + private long lastRead = 0; public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(in,"US-ASCII"),128); // InputStreamReader reader = new InputStreamReader(in,"US-ASCII"); String s; long now = SystemClock.uptimeMillis(); - long lastRead = now; + // we will wait more at the beginning of the connection + lastRead = now+45000; while((enabled) && (now < lastRead+3000 )){ // if (true || reader.ready()) { @@ -569,6 +622,7 @@ public void close(){ private int nbRetriesRemaining; private boolean connected = false; private boolean setDeviceSpeed = false; + private boolean sirfGps = false; private String deviceSpeed = "auto"; private String defaultDeviceSpeed = "4800"; @@ -588,6 +642,7 @@ public BlueetoothGpsManager(Service callingService, String deviceAddress, int ma deviceSpeed = sharedPreferences.getString(BluetoothGpsProviderService.PREF_GPS_DEVICE_SPEED, callingService.getString(R.string.defaultGpsDeviceSpeed)); defaultDeviceSpeed = callingService.getString(R.string.defaultGpsDeviceSpeed); setDeviceSpeed = !deviceSpeed.equals(callingService.getString(R.string.autoGpsDeviceSpeed)); + sirfGps = sharedPreferences.getBoolean(BluetoothGpsProviderService.PREF_SIRF_GPS, false); notificationManager = (NotificationManager)callingService.getSystemService(Context.NOTIFICATION_SERVICE); parser.setLocationManager(locationManager); From ae1bd3fa4d13a3e8bd2872e1ed924831d6364a07 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Wed, 14 Nov 2012 21:07:17 +0100 Subject: [PATCH 11/15] correction in SiRF options summary description --- res/values/strings.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index aef2f6ed..6eb5ecca 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -56,8 +56,8 @@ SiRF GPS - "Use SiRF Options" - "Don't SiRF Options" + "Use SiRF Options" + "Don't use SiRF Options" Choice of NMEA sentences GPS operation Enable NMEA GGA From 285e15cb4f4f09a9a9686cb2a83fd0412b305166 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sun, 18 Nov 2012 20:53:57 +0100 Subject: [PATCH 12/15] better usb gps speed management The app will try to use user setting and it doesn't work it will try to guess gps connection speed --- .../provider/BlueetoothGpsManager.java | 447 +++++++++++++----- .../provider/BluetoothGpsProviderService.java | 200 +------- 2 files changed, 328 insertions(+), 319 deletions(-) diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index 56843319..51231840 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -73,6 +73,7 @@ import android.location.GpsStatus.NmeaListener; import android.preference.PreferenceManager; import android.provider.Settings; +import android.os.Bundle; import android.os.SystemClock; import android.util.Log; @@ -116,10 +117,10 @@ public void onReceive(Context context, Intent intent) { // // connection obtained so reset the number of connection try // nbRetriesRemaining = 1+maxConnectionRetries ; // notificationManager.cancel(R.string.connection_problem_notification_title); - Log.v(LOG_TAG, "starting socket reading task"); + Log.v(LOG_TAG, "starting usb reading task"); connectedGps = new ConnectedGps(device, deviceSpeed); connectionAndReadingPool.execute(connectedGps); - Log.v(LOG_TAG, "socket reading thread started"); + Log.v(LOG_TAG, "usb reading thread started"); } } } else { @@ -148,6 +149,7 @@ private class ConnectedGps extends Thread { private UsbEndpoint endpointIn = null; private UsbEndpoint endpointOut = null; private final UsbDeviceConnection connection; + private boolean closed = false; /** * GPS InputStream from which we read data. */ @@ -207,19 +209,21 @@ public ConnectedGps(UsbDevice device, String deviceSpeed) { private byte[] oneByteBuffer = new byte[1]; private ByteBuffer bufferWrite = ByteBuffer.wrap(buffer); private ByteBuffer bufferRead = (ByteBuffer)ByteBuffer.wrap(buffer).limit(0); + private boolean closed = false; @Override public int read() throws IOException { int b = 0; if (BuildConfig.DEBUG || debug) Log.d(LOG_TAG, "trying to read data"); int nb = 0; - while (nb == 0){ + while ((nb == 0) && (!closed)){ nb = this.read(oneByteBuffer,0,1); } if (nb > 0){ b = oneByteBuffer[0]; } else { - b = nb; + // TODO : if nb = 0 then we have a pb + b = -1; Log.e(LOG_TAG, "data read() error code: " + nb ); } if (b <= 0){ @@ -268,7 +272,7 @@ public int read(byte[] buffer, int offset, int length) if (BuildConfig.DEBUG || debug) Log.d(LOG_TAG, "data read buffer - offset: " + offset + " length: " + length); int nb = 0; ByteBuffer out = ByteBuffer.wrap(buffer, offset, length); - if (! bufferRead.hasRemaining()){ + if ((! bufferRead.hasRemaining()) && (! closed)){ if (BuildConfig.DEBUG || debug) Log.i(LOG_TAG, "data read buffer empty " + Arrays.toString(usbBuffer)); int n = connection.bulkTransfer(endpointIn, usbBuffer, 64, TIMEOUT); if (BuildConfig.DEBUG || debug) Log.w(LOG_TAG, "data read: nb: " + n + " " + Arrays.toString(usbBuffer)); @@ -331,20 +335,17 @@ public long skip(long byteCount) throws IOException { @Override public void close() throws IOException { super.close(); - if (BuildConfig.DEBUG || debug) Log.d(LOG_TAG, "closing usb connection: " + connection); - connection.releaseInterface(intf); - connection.close(); + closed = true; } }; tmpOut = new OutputStream() { - int speed = 0; - final int[] speedList = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200}; private byte[] buffer = new byte[128]; private byte[] usbBuffer = new byte[64]; private byte[] oneByteBuffer = new byte[1]; private ByteBuffer bufferWrite = ByteBuffer.wrap(buffer); private ByteBuffer bufferRead = (ByteBuffer)ByteBuffer.wrap(buffer).limit(0); + private boolean closed = false; @Override public void write(int oneByte) throws IOException { @@ -364,11 +365,16 @@ public void write(byte[] buffer, int offset, int count) bufferWrite.clear(); bufferWrite.put(buffer, offset, count); if (BuildConfig.DEBUG || debug) Log.d(LOG_TAG, "trying to write data : " + Arrays.toString(this.buffer)); - int n = connection.bulkTransfer(endpointOut, this.buffer, count, TIMEOUT); + int n = 0; + if (! closed){ + n = connection.bulkTransfer(endpointOut, this.buffer, count, TIMEOUT); + } else { + Log.e(LOG_TAG, "error while trying to write data: outputStream closed"); + } if (n != count){ - Log.e(LOG_TAG, "error while trying to write data : " + Arrays.toString(this.buffer)); - Log.e(LOG_TAG, "error while trying to write data : " + n + " bytes writen when expecting " + count); - throw new IOException("error while trying to write data : " + Arrays.toString(this.buffer)); + Log.e(LOG_TAG, "error while trying to write data: " + Arrays.toString(this.buffer)); + Log.e(LOG_TAG, "error while trying to write data: " + n + " bytes writen when expecting " + count); + throw new IOException("error while trying to write data: " + Arrays.toString(this.buffer)); } if (BuildConfig.DEBUG || debug) Log.d(LOG_TAG, "writen data (one byte): " + Arrays.toString(this.buffer)); } @@ -379,6 +385,7 @@ public void write(byte[] buffer, int offset, int count) public void close() throws IOException { // TODO Auto-generated method stub super.close(); + closed = true; } /* (non-Javadoc) @@ -412,80 +419,82 @@ public void write(byte[] buffer) throws IOException { in = tmpIn; out = tmpOut; out2 = tmpOut2; - final int res1 = connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0); - ready = false; - if (setDeviceSpeed && false){ -// connection.controlTransfer(0x40, 0, 0, 0, null, 0, 0); //reset -// connection.controlTransfer(0x40, 0, 1, 0, null, 0, 0); //clear Rx -// connection.controlTransfer(0x40, 0, 2, 0, null, 0, 0); //clear Tx -// connection.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0); //flow control none -// connection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0); //baudrate 9600 -// connection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0); //data bit 8, parity none, stop bit 1, tx off - -// final byte[] datax = new byte[7]; -// final ByteBuffer connectionSpeedBufferx = ByteBuffer.wrap(datax,0,7).order(java.nio.ByteOrder.LITTLE_ENDIAN); -// int res0 = connection.controlTransfer(0xA1, 33, 0, 0, datax, 7, 0); -// Log.e(LOG_TAG, "info connection: " + Arrays.toString(datax)); - // Connection initialization: 4800 baud and 8N1 (0 bits no parity 1 stop bit) - Log.d(LOG_TAG, "initializing connection: 4800 baud and 8N1 (0 bits no parity 1 stop bit"); + final int[] speedList = {Integer.parseInt(deviceSpeed), 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200}; +// final List speedList = Arrays.asList(new String[]{"1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200"}); final byte[] data = { (byte) 0xC0, 0x12, 0x00, 0x00, 0x00, 0x00, 0x08 }; final ByteBuffer connectionSpeedBuffer = ByteBuffer.wrap(data, 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN); - connectionSpeedBuffer.putInt(Integer.parseInt(deviceSpeed)); -// int res1 = connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0); - int res2 = connection.controlTransfer(0x21, 32, 0, 0, data, 7, 0); - Log.e(LOG_TAG, "data init "+ res1 + " " + res2); - - ready = ((endpointOut != null) && (out != null) && (out2 != null)); - } else { - Thread autoConf = new Thread(){ + final byte[] sirfBin2Nmea = SirfUtils.genSirfCommandFromPayload(callingService.getString(R.string.sirf_bin_to_nmea)); + final byte[] datax = new byte[7]; + final ByteBuffer connectionSpeedInfoBuffer = ByteBuffer.wrap(datax,0,7).order(java.nio.ByteOrder.LITTLE_ENDIAN); + final int res1 = connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0); + if (setDeviceSpeed){ +//// connection.controlTransfer(0x40, 0, 0, 0, null, 0, 0); //reset +//// connection.controlTransfer(0x40, 0, 1, 0, null, 0, 0); //clear Rx +//// connection.controlTransfer(0x40, 0, 2, 0, null, 0, 0); //clear Tx +//// connection.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0); //flow control none +//// connection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0); //baudrate 9600 +//// connection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0); //data bit 8, parity none, stop bit 1, tx off + } + if (sirfGps){ + Log.e(LOG_TAG, "trying to switch from SiRF binaray to NMEA"); + connection.bulkTransfer(endpointOut, sirfBin2Nmea, sirfBin2Nmea.length, 0); + } + Thread autoConf = new Thread(){ - /* (non-Javadoc) - * @see java.lang.Thread#run() - */ - @Override - public void run() { - Log.d(LOG_TAG, "initializing connection: 4800 baud and 8N1 (0 bits no parity 1 stop bit"); - final int[] speedList = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200}; - final byte[] data = { (byte) 0xC0, 0x12, 0x00, 0x00, 0x00, 0x00, 0x08 }; - final ByteBuffer connectionSpeedBuffer = ByteBuffer.wrap(data, 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN); - final byte[] sirfBin2Nmea = SirfUtils.genSirfCommandFromPayload(callingService.getString(R.string.sirf_bin_to_nmea)); - try { - Thread.sleep(1000); - for (int speed : speedList){ - if (!ready){ - connectionSpeedBuffer.putInt(0, speed); - Log.e(LOG_TAG, "trying to use speed " + speed); - int res2 = connection.controlTransfer(0x21, 32, 0, 0, data, 7, 0); - if (sirfGps){ - Log.e(LOG_TAG, "trying to switch from SiRF binaray to NMEA"); - connection.bulkTransfer(endpointOut, sirfBin2Nmea, sirfBin2Nmea.length, 0); - } - Log.e(LOG_TAG, "data init "+ res1 + " " + res2); - Thread.sleep(3000); + /* (non-Javadoc) + * @see java.lang.Thread#run() + */ + @Override + public void run() { +// final byte[] data = { (byte) 0xC0, 0x12, 0x00, 0x00, 0x00, 0x00, 0x08 }; +// final ByteBuffer connectionSpeedBuffer = ByteBuffer.wrap(data, 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN); +// final byte[] sirfBin2Nmea = SirfUtils.genSirfCommandFromPayload(callingService.getString(R.string.sirf_bin_to_nmea)); +// final byte[] datax = new byte[7]; +// final ByteBuffer connectionSpeedInfoBuffer = ByteBuffer.wrap(datax,0,7).order(java.nio.ByteOrder.LITTLE_ENDIAN); + try { + int res0 = connection.controlTransfer(0xA1, 33, 0, 0, datax, 7, 0); + BlueetoothGpsManager.this.deviceSpeed = Integer.toString(connectionSpeedInfoBuffer.getInt(0)); + Log.e(LOG_TAG, "info connection: " + Arrays.toString(datax)); + Log.e(LOG_TAG, "info connection speed: " + BlueetoothGpsManager.this.deviceSpeed); + Thread.sleep(4000); + Log.e(LOG_TAG, "trying to use speed in range: " + Arrays.toString(speedList)); + for (int speed : speedList){ + if (!ready && !closed){ + BlueetoothGpsManager.this.deviceSpeed = Integer.toString(speed); + Log.e(LOG_TAG, "trying to use speed " + speed); + Log.d(LOG_TAG, "initializing connection: " + speed + " baud and 8N1 (0 bits no parity 1 stop bit"); + connectionSpeedBuffer.putInt(0, speed); + int res2 = connection.controlTransfer(0x21, 32, 0, 0, data, 7, 0); + if (sirfGps){ + Log.e(LOG_TAG, "trying to switch from SiRF binaray to NMEA"); + connection.bulkTransfer(endpointOut, sirfBin2Nmea, sirfBin2Nmea.length, 0); } + Log.e(LOG_TAG, "data init "+ res1 + " " + res2); + Thread.sleep(4000); } - final byte[] datax = new byte[7]; - final ByteBuffer connectionSpeedBufferx = ByteBuffer.wrap(datax,0,7).order(java.nio.ByteOrder.LITTLE_ENDIAN); - int res0 = connection.controlTransfer(0xA1, 33, 0, 0, datax, 7, 0); - Log.e(LOG_TAG, "info connection: " + Arrays.toString(datax)); - Log.e(LOG_TAG, "info connection speed: " + connectionSpeedBufferx.getInt(0)); + } + res0 = connection.controlTransfer(0xA1, 33, 0, 0, datax, 7, 0); + Log.e(LOG_TAG, "info connection: " + Arrays.toString(datax)); + Log.e(LOG_TAG, "info connection speed: " + connectionSpeedInfoBuffer.getInt(0)); + if (! closed) { Thread.sleep(10000); - } catch (InterruptedException e) { - Log.e(LOG_TAG, "autoconf thread interupted", e); - } finally { - if ((!ready ) || (lastRead + 3000 < SystemClock.uptimeMillis())){ - setMockLocationProviderOutOfService(); - // cleanly closing everything... - ConnectedGps.this.close(); - BlueetoothGpsManager.this.disableIfNeeded(); - } + } + } catch (InterruptedException e) { + Log.e(LOG_TAG, "autoconf thread interupted", e); + } finally { + if ( (! closed) && (!ready ) || (lastRead + 4000 < SystemClock.uptimeMillis())){ + setMockLocationProviderOutOfService(); + // cleanly closing everything... + ConnectedGps.this.close(); + BlueetoothGpsManager.this.disableIfNeeded(); } } + } - }; - Log.e(LOG_TAG, "trying to find speed "); - autoConf.start(); - } + }; + Log.e(LOG_TAG, "trying to find speed "); + ready = false; + autoConf.start(); } public boolean isReady(){ @@ -501,7 +510,7 @@ public void run() { long now = SystemClock.uptimeMillis(); // we will wait more at the beginning of the connection lastRead = now+45000; - while((enabled) && (now < lastRead+3000 )){ + while((enabled) && (now < lastRead+4000 ) && (! closed)){ // if (true || reader.ready()) { s = reader.readLine(); @@ -509,6 +518,7 @@ public void run() { Log.v(LOG_TAG, "data: "+System.currentTimeMillis()+" "+s); if (notifyNmeaSentence(s+"\r\n")){ ready = true; +// lastRead = Math.max(SystemClock.uptimeMillis(), lastRead); lastRead = SystemClock.uptimeMillis(); if (nbRetriesRemaining < maxConnectionRetries){ // reset eventual disabling cause @@ -545,8 +555,8 @@ public void write(byte[] buffer) { try { do { Thread.sleep(100); - } while ((enabled) && (! ready)); - if ((enabled) && (ready)){ + } while ((enabled) && (! ready) && (!closed)); + if ((enabled) && (ready) && (!closed)){ out.write(buffer); out.flush(); } @@ -564,8 +574,8 @@ public void write(String buffer) { try { do { Thread.sleep(100); - } while ((enabled) && (! ready)); - if ((enabled) && (ready)){ + } while ((enabled) && (! ready) && (!closed)); + if ((enabled) && (ready) && (!closed)){ out2.print(buffer); out2.flush(); } @@ -576,19 +586,29 @@ public void write(String buffer) { public void close(){ ready = false; + closed = true; try { - Log.d(LOG_TAG, "closing Bluetooth GPS output sream"); + Log.d(LOG_TAG, "closing USB 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"); + Log.d(LOG_TAG, "closing USB GPS input streams"); out2.close(); out.close(); } catch (IOException e) { Log.e(LOG_TAG, "error while closing GPS input streams", e); } finally { + if (BuildConfig.DEBUG || debug) Log.e(LOG_TAG, "releasing usb interface for connection: " + connection); + boolean released = connection.releaseInterface(intf); + if (released){ + if (BuildConfig.DEBUG || debug) Log.e(LOG_TAG, "usb interface released for connection: " + connection); + } else { + if (BuildConfig.DEBUG || debug) Log.e(LOG_TAG, "unable to release usb interface for connection: " + connection); + } + if (BuildConfig.DEBUG || debug) Log.e(LOG_TAG, "closing usb connection: " + connection); + connection.close(); // try { // Log.d(LOG_TAG, "closing Bluetooth GPS socket"); // socket.close(); @@ -762,10 +782,10 @@ public void run() { // // connection obtained so reset the number of connection try // nbRetriesRemaining = 1+maxConnectionRetries ; // notificationManager.cancel(R.string.connection_problem_notification_title); - Log.v(LOG_TAG, "starting socket reading task"); + Log.v(LOG_TAG, "starting usb reading task"); connectedGps = new ConnectedGps(device, deviceSpeed); connectionAndReadingPool.execute(connectedGps); - Log.v(LOG_TAG, "socket reading thread started"); + Log.v(LOG_TAG, "usb reading thread started"); } else if (device != null) { Log.d(LOG_TAG, "We don't have permession, so resquesting..."); usbManager.requestPermission(device, permissionIntent); @@ -933,6 +953,9 @@ public void run() { connectionAndReadingPool = Executors.newSingleThreadScheduledExecutor(); Log.v(LOG_TAG, "starting connection to socket task"); connectionAndReadingPool.scheduleWithFixedDelay(connectThread, 5000, 60000, TimeUnit.MILLISECONDS); + if (sirfGps){ + enableSirfConfig(sharedPreferences); + } } } } @@ -1232,23 +1255,9 @@ public void run() { * @param command the complete NMEA sentence (i.e. $....*XY where XY is the checksum). */ public void sendPackagedNmeaCommand(final String command){ - Log.e(LOG_TAG, "spooling NMEA sentence: "+command); - if (isEnabled()){ - notificationPool.execute( new Runnable() { - @Override - public void run() { - while ((enabled) && ((!connected) || (connectedGps == null) || (!connectedGps.isReady()))){ - Log.v(LOG_TAG, "writing thread is not ready"); - SystemClock.sleep(500); - } - if (isEnabled() && (connectedGps != null)){ - Log.e(LOG_TAG, "sending NMEA sentence: "+command); - connectedGps.write(command); - Log.e(LOG_TAG, "sent NMEA sentence: "+command); - } - } - }); - } + Log.e(LOG_TAG, "sending NMEA sentence: "+command); + connectedGps.write(command); + Log.e(LOG_TAG, "sent NMEA sentence: "+command); } /** @@ -1258,24 +1267,10 @@ public void run() { * (i.e. with the Start Sequence, Payload Length, Payload, Message Checksum and End Sequence). */ public void sendPackagedSirfCommand(final String commandHexa){ - Log.e(LOG_TAG, "spooling SIRF sentence: "+commandHexa); - if (isEnabled()){ - final byte[] command = SirfUtils.genSirfCommand(commandHexa); - notificationPool.execute( new Runnable() { - @Override - public void run() { - while ((enabled) && ((!connected) || (connectedGps == null) || (!connectedGps.isReady()))){ - Log.v(LOG_TAG, "writing thread is not ready"); - SystemClock.sleep(500); - } - if (isEnabled() && (connectedGps != null)){ - Log.e(LOG_TAG, "sendind SIRF sentence: "+commandHexa); - connectedGps.write(command); - Log.e(LOG_TAG, "sent SIRF sentence: "+commandHexa); - } - } - }); - } + final byte[] command = SirfUtils.genSirfCommand(commandHexa); + Log.e(LOG_TAG, "sendind SIRF sentence: "+commandHexa); + connectedGps.write(command); + Log.e(LOG_TAG, "sent SIRF sentence: "+commandHexa); } /** @@ -1298,4 +1293,210 @@ public void sendSirfCommand(String payload){ String command = SirfUtils.createSirfCommandFromPayload(payload); sendPackagedSirfCommand(command); } + + private void enableNMEA(boolean enable){ +// SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(callingService); +// String deviceSpeed = sharedPreferences.getString(BluetoothGpsProviderService.PREF_GPS_DEVICE_SPEED, callingService.getString(R.string.defaultGpsDeviceSpeed)); + if (deviceSpeed.equals(callingService.getString(R.string.autoGpsDeviceSpeed))){ + deviceSpeed = callingService.getString(R.string.defaultGpsDeviceSpeed); + } + SystemClock.sleep(400); + if (enable){ +// int gll = (sharedPreferences.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GLL, false)) ? 1 : 0 ; +// int vtg = (sharedPreferences.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_VTG, false)) ? 1 : 0 ; +// int gsa = (sharedPreferences.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GSA, false)) ? 5 : 0 ; +// int gsv = (sharedPreferences.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GSV, false)) ? 5 : 0 ; +// int zda = (sharedPreferences.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_ZDA, false)) ? 1 : 0 ; +// int mss = 0; +// int epe = 0; +// int gga = 1; +// int rmc = 1; +// String command = getString(R.string.sirf_bin_to_nmea_38400_alt, gga, gll, gsa, gsv, rmc, vtg, mss, epe, zda); +// String command = getString(R.string.sirf_bin_to_nmea_alt, gga, gll, gsa, gsv, rmc, vtg, mss, epe, zda, Integer.parseInt(deviceSpeed)); + String command = callingService.getString(R.string.sirf_bin_to_nmea); + this.sendSirfCommand(command); + } else { +// this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_to_binary)); + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_to_binary_alt, Integer.parseInt(deviceSpeed))); + } + SystemClock.sleep(400); + } + + private void enableNmeaGGA(boolean enable){ + if (enable){ + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_gga_on)); + } else { + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_gga_off)); + } + } + + private void enableNmeaGLL(boolean enable){ + if (enable){ + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_gll_on)); + } else { + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_gll_off)); + } + } + + private void enableNmeaGSA(boolean enable){ + if (enable){ + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_gsa_on)); + } else { + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_gsa_off)); + } + } + + private void enableNmeaGSV(boolean enable){ + if (enable){ + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_gsv_on)); + } else { + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_gsv_off)); + } + } + + private void enableNmeaRMC(boolean enable){ + if (enable){ + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_rmc_on)); + } else { + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_rmc_off)); + } + } + + private void enableNmeaVTG(boolean enable){ + if (enable){ + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_vtg_on)); + } else { + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_vtg_off)); + } + } + + private void enableNmeaZDA(boolean enable){ + if (enable){ + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_zda_on)); + } else { + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_zda_off)); + } + } + + private void enableSBAS(boolean enable){ + if (enable){ + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_sbas_on)); + } else { + this.sendNmeaCommand(callingService.getString(R.string.sirf_nmea_sbas_off)); + } + } + + public void enableSirfConfig(final Bundle extra){ + Log.e(LOG_TAG, "spooling SiRF config: "+ extra); + if (isEnabled()){ + notificationPool.execute( new Runnable() { + @Override + public void run() { + while ((enabled) && ((!connected) || (connectedGps == null) || (!connectedGps.isReady()))){ + Log.v(LOG_TAG, "writing thread is not ready"); + SystemClock.sleep(500); + } + if (isEnabled() && (connected) && (connectedGps != null) && (connectedGps.isReady())){ + Log.e(LOG_TAG, "init SiRF config: "+extra); + if (extra.containsKey(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GGA)){ + enableNmeaGGA(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GGA, true)); + } + if (extra.containsKey(BluetoothGpsProviderService.PREF_SIRF_ENABLE_RMC)){ + enableNmeaRMC(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_RMC, true)); + } + if (extra.containsKey(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GLL)){ + enableNmeaGLL(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GLL, false)); + } + if (extra.containsKey(BluetoothGpsProviderService.PREF_SIRF_ENABLE_VTG)){ + enableNmeaVTG(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_VTG, false)); + } + if (extra.containsKey(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GSA)){ + enableNmeaGSA(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GSA, false)); + } + if (extra.containsKey(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GSV)){ + enableNmeaGSV(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GSV, false)); + } + if (extra.containsKey(BluetoothGpsProviderService.PREF_SIRF_ENABLE_ZDA)){ + enableNmeaZDA(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_ZDA, false)); + } + if (extra.containsKey(BluetoothGpsProviderService.PREF_SIRF_ENABLE_STATIC_NAVIGATION)){ + enableStaticNavigation(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_STATIC_NAVIGATION, false)); + } else if (extra.containsKey(BluetoothGpsProviderService.PREF_SIRF_ENABLE_NMEA)){ + enableNMEA(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_NMEA, true)); + } + if (extra.containsKey(BluetoothGpsProviderService.PREF_SIRF_ENABLE_SBAS)){ + enableSBAS(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_SBAS, true)); + } + Log.e(LOG_TAG, "initialized SiRF config: "+extra); + } + } + }); + } + } + + public void enableSirfConfig(final SharedPreferences extra){ + Log.e(LOG_TAG, "spooling SiRF config: "+ extra); + if (isEnabled()){ + notificationPool.execute( new Runnable() { + @Override + public void run() { + while ((enabled) && ((!connected) || (connectedGps == null) || (!connectedGps.isReady()))){ + Log.v(LOG_TAG, "writing thread is not ready"); + SystemClock.sleep(500); + } + if (isEnabled() && (connected) && (connectedGps != null) && (connectedGps.isReady())){ + Log.e(LOG_TAG, "init SiRF config: "+extra); + if (extra.contains(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GLL)){ + enableNmeaGLL(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GLL, false)); + } + if (extra.contains(BluetoothGpsProviderService.PREF_SIRF_ENABLE_VTG)){ + enableNmeaVTG(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_VTG, false)); + } + if (extra.contains(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GSA)){ + enableNmeaGSA(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GSA, false)); + } + if (extra.contains(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GSV)){ + enableNmeaGSV(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GSV, false)); + } + if (extra.contains(BluetoothGpsProviderService.PREF_SIRF_ENABLE_ZDA)){ + enableNmeaZDA(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_ZDA, false)); + } + if (extra.contains(BluetoothGpsProviderService.PREF_SIRF_ENABLE_STATIC_NAVIGATION)){ + enableStaticNavigation(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_STATIC_NAVIGATION, false)); + } else if (extra.contains(BluetoothGpsProviderService.PREF_SIRF_ENABLE_NMEA)){ + enableNMEA(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_NMEA, true)); + } + if (extra.contains(BluetoothGpsProviderService.PREF_SIRF_ENABLE_SBAS)){ + enableSBAS(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_SBAS, true)); + } + sendNmeaCommand(callingService.getString(R.string.sirf_nmea_gga_on)); + sendNmeaCommand(callingService.getString(R.string.sirf_nmea_rmc_on)); + if (extra.contains(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GGA)){ + enableNmeaGGA(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_GGA, true)); + } + if (extra.contains(BluetoothGpsProviderService.PREF_SIRF_ENABLE_RMC)){ + enableNmeaRMC(extra.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_RMC, true)); + } + } + } + }); + } + } + + private void enableStaticNavigation(boolean enable){ + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(callingService); + boolean isInNmeaMode = sharedPreferences.getBoolean(BluetoothGpsProviderService.PREF_SIRF_ENABLE_NMEA, true); + if (isInNmeaMode){ + enableNMEA(false); + } + if (enable){ + this.sendSirfCommand(callingService.getString(R.string.sirf_bin_static_nav_on)); + } else { + this.sendSirfCommand(callingService.getString(R.string.sirf_bin_static_nav_off)); + } + if (isInNmeaMode){ + enableNMEA(true); + } + } + } diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java index a5a87b7a..852b318b 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java @@ -137,9 +137,9 @@ public void onStart(Intent intent, int startId) { 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 (sharedPreferences.getBoolean(PREF_SIRF_GPS, false)){ - enableSirfConfig(sharedPreferences); - } +// if (sharedPreferences.getBoolean(PREF_SIRF_GPS, false)){ +// gpsManager.enableSirfConfig(sharedPreferences); +// } toast.setText(this.getString(R.string.msg_gps_provider_started)); toast.show(); } else { @@ -194,199 +194,7 @@ public void onStart(Intent intent, int startId) { } else if (ACTION_CONFIGURE_SIRF_GPS.equals(intent.getAction())){ if (gpsManager != null){ Bundle extras = intent.getExtras(); - enableSirfConfig(extras); - } - } - } - - private void enableSirfConfig(Bundle extras){ - if (extras.containsKey(PREF_SIRF_ENABLE_GGA)){ - enableNmeaGGA(extras.getBoolean(PREF_SIRF_ENABLE_GGA, true)); - } - if (extras.containsKey(PREF_SIRF_ENABLE_RMC)){ - enableNmeaRMC(extras.getBoolean(PREF_SIRF_ENABLE_RMC, true)); - } - if (extras.containsKey(PREF_SIRF_ENABLE_GLL)){ - enableNmeaGLL(extras.getBoolean(PREF_SIRF_ENABLE_GLL, false)); - } - if (extras.containsKey(PREF_SIRF_ENABLE_VTG)){ - enableNmeaVTG(extras.getBoolean(PREF_SIRF_ENABLE_VTG, false)); - } - if (extras.containsKey(PREF_SIRF_ENABLE_GSA)){ - enableNmeaGSA(extras.getBoolean(PREF_SIRF_ENABLE_GSA, false)); - } - if (extras.containsKey(PREF_SIRF_ENABLE_GSV)){ - enableNmeaGSV(extras.getBoolean(PREF_SIRF_ENABLE_GSV, false)); - } - if (extras.containsKey(PREF_SIRF_ENABLE_ZDA)){ - enableNmeaZDA(extras.getBoolean(PREF_SIRF_ENABLE_ZDA, false)); - } - if (extras.containsKey(PREF_SIRF_ENABLE_STATIC_NAVIGATION)){ - enableStaticNavigation(extras.getBoolean(PREF_SIRF_ENABLE_STATIC_NAVIGATION, false)); - } 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)){ - enableNmeaGLL(extras.getBoolean(PREF_SIRF_ENABLE_GLL, false)); - } - if (extras.contains(PREF_SIRF_ENABLE_VTG)){ - enableNmeaVTG(extras.getBoolean(PREF_SIRF_ENABLE_VTG, false)); - } - if (extras.contains(PREF_SIRF_ENABLE_GSA)){ - enableNmeaGSA(extras.getBoolean(PREF_SIRF_ENABLE_GSA, false)); - } - if (extras.contains(PREF_SIRF_ENABLE_GSV)){ - enableNmeaGSV(extras.getBoolean(PREF_SIRF_ENABLE_GSV, false)); - } - if (extras.contains(PREF_SIRF_ENABLE_ZDA)){ - enableNmeaZDA(extras.getBoolean(PREF_SIRF_ENABLE_ZDA, false)); - } - if (extras.contains(PREF_SIRF_ENABLE_STATIC_NAVIGATION)){ - enableStaticNavigation(extras.getBoolean(PREF_SIRF_ENABLE_STATIC_NAVIGATION, false)); - } 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)); - if (extras.contains(PREF_SIRF_ENABLE_GGA)){ - enableNmeaGGA(extras.getBoolean(PREF_SIRF_ENABLE_GGA, true)); - } - if (extras.contains(PREF_SIRF_ENABLE_RMC)){ - enableNmeaRMC(extras.getBoolean(PREF_SIRF_ENABLE_RMC, true)); - } - } - - private void enableNmeaGGA(boolean enable){ - if (gpsManager != null){ - if (enable){ - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_gga_on)); - } else { - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_gga_off)); - } - } - } - - private void enableNmeaRMC(boolean enable){ - if (gpsManager != null){ - if (enable){ - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_rmc_on)); - } else { - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_rmc_off)); - } - } - } - - private void enableNmeaGLL(boolean enable){ - if (gpsManager != null){ - if (enable){ - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_gll_on)); - } else { - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_gll_off)); - } - } - } - - private void enableNmeaVTG(boolean enable){ - if (gpsManager != null){ - if (enable){ - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_vtg_on)); - } else { - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_vtg_off)); - } - } - } - - private void enableNmeaGSA(boolean enable){ - if (gpsManager != null){ - if (enable){ - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_gsa_on)); - } else { - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_gsa_off)); - } - } - } - - private void enableNmeaGSV(boolean enable){ - if (gpsManager != null){ - if (enable){ - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_gsv_on)); - } else { - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_gsv_off)); - } - } - } - - private void enableNmeaZDA(boolean enable){ - if (gpsManager != null){ - if (enable){ - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_zda_on)); - } else { - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_zda_off)); - } - } - } - - private void enableSBAS(boolean enable){ - if (gpsManager != null){ - if (enable){ - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_sbas_on)); - } else { - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_sbas_off)); - } - } - } - - private void enableNMEA(boolean enable){ - if (gpsManager != null){ - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - String deviceSpeed = sharedPreferences.getString(BluetoothGpsProviderService.PREF_GPS_DEVICE_SPEED, this.getString(R.string.defaultGpsDeviceSpeed)); - if (deviceSpeed.equals(this.getString(R.string.autoGpsDeviceSpeed))){ - deviceSpeed = this.getString(R.string.defaultGpsDeviceSpeed); - } - if (enable){ -// int gll = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_GLL, false)) ? 1 : 0 ; -// int vtg = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_VTG, false)) ? 1 : 0 ; -// int gsa = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_GSA, false)) ? 5 : 0 ; -// int gsv = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_GSV, false)) ? 5 : 0 ; -// int zda = (sharedPreferences.getBoolean(PREF_SIRF_ENABLE_ZDA, false)) ? 1 : 0 ; -// int mss = 0; -// int epe = 0; -// int gga = 1; -// int rmc = 1; -// String command = getString(R.string.sirf_bin_to_nmea_38400_alt, gga, gll, gsa, gsv, rmc, vtg, mss, epe, zda); -// String command = getString(R.string.sirf_bin_to_nmea_alt, gga, gll, gsa, gsv, rmc, vtg, mss, epe, zda, Integer.parseInt(deviceSpeed)); - String command = getString(R.string.sirf_bin_to_nmea); - gpsManager.sendSirfCommand(command); - } else { -// gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_to_binary)); - gpsManager.sendNmeaCommand(getString(R.string.sirf_nmea_to_binary_alt, Integer.parseInt(deviceSpeed))); - } - } - } - - private void enableStaticNavigation(boolean enable){ - if (gpsManager != null){ - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - boolean isInNmeaMode = sharedPreferences.getBoolean(PREF_SIRF_ENABLE_NMEA, true); - if (isInNmeaMode){ - enableNMEA(false); - } - if (enable){ - gpsManager.sendSirfCommand(getString(R.string.sirf_bin_static_nav_on)); - } else { - gpsManager.sendSirfCommand(getString(R.string.sirf_bin_static_nav_off)); - } - if (isInNmeaMode){ - enableNMEA(true); + gpsManager.enableSirfConfig(extras); } } } From 4fd0929ccc4bd7f441fb24b6c156f2c2b5bae6ae Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Fri, 8 Mar 2013 18:14:41 +0100 Subject: [PATCH 13/15] update version numbers for Honeycomb version beta 1 --- AndroidManifest.xml | 2 +- res/values/constants.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 8f2b221c..af2b22b3 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -23,7 +23,7 @@ diff --git a/res/values/constants.xml b/res/values/constants.xml index e551c37f..4f93b4c6 100644 --- a/res/values/constants.xml +++ b/res/values/constants.xml @@ -22,7 +22,7 @@ --> - alpha 5 + beta 1 startGps gpsLocationProviderKey From 792f36c4d4da0bf4996f793c78430498bbab68e2 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Fri, 8 Mar 2013 18:16:12 +0100 Subject: [PATCH 14/15] Update copyright info in source and about app message --- AndroidManifest.xml | 6 +++--- res/values/constants.xml | 6 +++--- res/values/strings.xml | 12 ++++++------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index af2b22b3..ea022b99 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,8 +1,8 @@