Permalink
Browse files

Basics of talking to a bluetooth vario might work now...

  • Loading branch information...
1 parent e4b8d63 commit ad78a7d0715181360faf561a23d6f0483b12e470 @geeksville geeksville committed Feb 12, 2012
@@ -8,8 +8,8 @@
<body>
Καλώς ήρθατε στο Gaggle. <br><br>Κανονικά θα βλέπατε τις πτήσεις σας εδώ, αλλά δεν έχετε καταγράψει καμιά πτήση ακόμα.
Για να ξεκινήσετε την καταγραφή μιας πτήσης, κάντε κλικ στο &quot;Έναρξη καταγραφής&quot;.<br><br>
-Για το εγχειρίδιο χρήσης του Gaggle, απλά πατήστε Μενού και επιλέξτε "Βοήθεια"<br><br>
-Αν απλός θέλετε να δείτε τα όργανα ή τους χάρτες, κάντε κλικ στις πάνω καρτέλες.<br>
+Για το εγχειρίδιο χρήσης του Gaggle, πατήστε Μενού και επιλέξτε "Βοήθεια"<br><br>
+Αν θέλετε να δείτε τα όργανα ή τους χάρτες, κάντε κλικ στις πάνω καρτέλες.<br>
</body>
</html>
@@ -27,17 +27,23 @@
private static final String TAG = "BarometerClient";
- private static IBarometerClient instance = null;
+ private static IBarometerClient instance = null;
/**
* All users of barometer share the same (expensive) instance
*
* @return null for if not available
*/
- public static IBarometerClient create(Context context) {
+ public static IBarometerClient create(Context context) {
- if (instance == null && AndroidBarometerClient.isAvailable())
- instance = new AndroidBarometerClient(context);
+ SensorClient.initManager(context);
+
+ // Prefer to use external bluetooth device
+ if (instance == null && BluetoothBarometerClient.isAvailable())
+ instance = new BluetoothBarometerClient(context);
+
+ if (instance == null && AndroidBarometerClient.isAvailable())
+ instance = new AndroidBarometerClient(context);
return instance;
}
@@ -10,108 +10,184 @@
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
+import android.content.Context;
import android.location.Location;
import android.util.Log;
+import android.widget.Toast;
/**
* A client for scott@cnes.com bluetooth vario
*
* @author kevinh
*
+ * On Mon, Feb 6, 2012 at 11:47 AM, Scott Jepson <scott@cnes.com> wrote:
+ *
+ * Hi Kevin, So here's the output from the bluetooth barometer:
+ * printf("V%06ld A%07ld P%06ld B%03ld\r\n",Vspd,Alt,Pressure,**Battery)
+ *
+ * Vspd=CM/S and looks like 000005 or -00005 Alt=CM and looks like
+ * 0038002 which would be 380.02 Meters Pressure=Pa and looks like
+ * 096840 Battery=Volts and looks like 032 which would be 3.2 volts
+ *
+ * So the whole output looks like: V000005 A0038002 P096840 B032
+ *
+ * How fast do you want the data samples sent? So far I can do 25
+ * samples a second.
+ *
+ *
+ * -Scott
*/
public class BluetoothBarometerClient extends Observable implements
- IBarometerClient, Runnable {
-
- private static final String TAG = "BluetoothBarometerClient";
-
- private static final int myClass = 0xa01; // See
- // http://developer.android.com/reference/android/bluetooth/BluetoothClass.Device.html,
- // for now I'm guessing at a
- // value
-
- /** A unique ID for our app */
- private UUID uuid = UUID.fromString("b00d0c47-899b-4484-810a-5b27a514e906");
-
- private BluetoothSocket socket;
-
- private BluetoothBarometerClient(BluetoothDevice device) {
- // Get a BluetoothSocket to connect with the given BluetoothDevice
- try {
- // MY_UUID is the app's UUID string, also used by the server code
- socket = device.createRfcommSocketToServiceRecord(uuid);
- } catch (IOException e) {
- // FIXME
- }
- }
-
- static boolean isAvailable() {
- return findDevice() != null;
- }
-
- private static BluetoothDevice findDevice() {
- BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
- if (adapter != null) {
- Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
-
- for (BluetoothDevice device : pairedDevices) {
-
- Log.d(TAG,
- "Considering " + device.getName() + "@"
- + device.getAddress());
-
- int bClass = device.getBluetoothClass().getDeviceClass();
-
- if (bClass == myClass)
- return device;
- }
- }
-
- return null;
- }
-
- @Override
- public void setAltitude(float meters) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public float getAltitude() {
- // TODO Auto-generated method stub
- return 0;
- }
-
- @Override
- public float getVerticalSpeed() {
- // TODO Auto-generated method stub
- return 0;
- }
-
- @Override
- public void improveLocation(Location l) {
- // TODO Auto-generated method stub
-
- }
-
- // / The background thread that talks to device
- @Override
- public void run() {
- try {
- // Connect the device through the socket. This will block
- // until it succeeds or throws an exception
- socket.connect();
-
- // Read messages
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- } catch (IOException connectException) {
- // Unable to connect; close the socket and get out
- try {
- socket.close();
- } catch (IOException closeException) {
- // Ignore errors on close
- }
- }
- }
+ IBarometerClient, Runnable {
+
+ private static final String TAG = "BluetoothBarometerClient";
+
+ // / What device name do we look for?
+ private static final String myName = "BlueBaro";
+
+ // private static final int myClass = 0xa01; // See
+ // http://developer.android.com/reference/android/bluetooth/BluetoothClass.Device.html,
+ // for now I'm guessing at a
+ // value
+
+ /** A unique ID for our app */
+ private UUID uuid = UUID.fromString("b00d0c47-899b-4484-810a-5b27a514e906");
+
+ private BluetoothDevice device;
+ private Thread thread;
+
+ private float altitude, vspd, batVoltage, pressure;
+
+ // / true if we've been set based on the GPS
+ private boolean isCalibrated = false;
+
+ private Context context;
+
+ public BluetoothBarometerClient(Context context) {
+ this.context = context;
+ this.device = findDevice();
+
+ // We do all the real work in a background thread, so we don't stall and can
+ // handle reboots of the bluetooth device
+
+ // FIXME this burns too much power, we should instead only create our reader
+ // thread in addObserver, then
+ // shut it down gracefully when the number of observers drops to zero. This
+ // will have the nice effect of only talking
+ // to the bluetooth baro when we actually need its data.
+ thread = new Thread(this, "BluetoothBaro");
+ thread.setDaemon(true);
+ thread.start();
+ }
+
+ static boolean isAvailable() {
+ return findDevice() != null;
+ }
+
+ private static BluetoothDevice findDevice() {
+ BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
+ if (adapter != null && adapter.isEnabled()) {
+ Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
+
+ for (BluetoothDevice device : pairedDevices) {
+
+ Log.d(TAG,
+ "Considering " + device.getName() + "@" + device.getAddress());
+
+ // if (device.getBluetoothClass().getDeviceClass() == myClass) return
+ // device;
+ if (device.getName().startsWith(myName))
+ return device;
+ }
+ }
+
+ return null;
+ }
+
+ @Override
+ public void setAltitude(float meters) {
+ // FIXME - apply correction from GPS based altitude
+ }
+
+ @Override
+ public float getAltitude() {
+ return altitude;
+ }
+
+ @Override
+ public float getVerticalSpeed() {
+ return vspd;
+ }
+
+ @Override
+ public void improveLocation(Location l) {
+ if (isCalibrated)
+ l.setAltitude(altitude);
+ }
+
+ private void handleMessage(String m) {
+ /**
+ * Hi Kevin, So here's the output from the bluetooth barometer:
+ * printf("V%06ld A%07ld P%06ld B%03ld\r\n",Vspd,Alt,Pressure,**Battery)
+ *
+ * Vspd=CM/S and looks like 000005 or -00005 Alt=CM and looks like 0038002
+ * which would be 380.02 Meters Pressure=Pa and looks like 096840
+ * Battery=Volts and looks like 032 which would be 3.2 volts
+ *
+ * So the whole output looks like: V000005 A0038002 P096840 B032
+ */
+ vspd = Integer.parseInt(m.substring(1, 1 + 6)) / 100.0f; // avoid using
+ // split,
+ // because it generates
+ // lots of allocs
+ altitude = Integer.parseInt(m.substring(9, 9 + 7)) / 100.f;
+
+ // convert pressure from Pa to hPa
+ pressure = Integer.parseInt(m.substring(18, 18 + 6)) / 100.f;
+
+ batVoltage = Integer.parseInt(m.substring(26, 26 + 3)) / 10.0f;
+
+ // Tell the GUI/audio vario we have new state
+ setChanged();
+ notifyObservers(pressure);
+ }
+
+ /** The background thread that talks to device */
+ @Override
+ public void run() {
+ BluetoothSocket socket = null;
+ try {
+ Toast.makeText(context, "Using Bluetooth Vario", Toast.LENGTH_LONG)
+ .show();
+
+ // FIXME, add outer loop to reconnect if bluetooth device is rebooted
+ socket = device.createRfcommSocketToServiceRecord(uuid);
+
+ // Connect the device through the socket. This will block
+ // until it succeeds or throws an exception
+ socket.connect();
+
+ // Read messages
+ BufferedReader reader = new BufferedReader(new InputStreamReader(
+ socket.getInputStream()));
+
+ String line;
+ while ((line = reader.readLine()) != null)
+ handleMessage(line);
+
+ reader.close();
+
+ socket.close();
+ socket = null;
+ } catch (IOException connectException) {
+ // close the socket and get out
+ try {
+ if (socket != null)
+ socket.close();
+ } catch (IOException closeException) {
+ // Ignore errors on close
+ }
+ }
+ }
}

0 comments on commit ad78a7d

Please sign in to comment.