Skip to content

Commit 0ed1d06

Browse files
committed
Flysight2 bluetooth gps protocol
1 parent 2b0c8ad commit 0ed1d06

File tree

8 files changed

+102
-10
lines changed

8 files changed

+102
-10
lines changed

app/src/main/java/com/platypii/baseline/lasers/rangefinder/RangefinderService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public void start(@NonNull Activity activity) {
4747
} else {
4848
// Turn on bluetooth
4949
Log.i(TAG, "Requesting to turn on bluetooth");
50-
final Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
5150
Intents.requestEnableBluetooth(activity);
5251
}
5352
} else {

app/src/main/java/com/platypii/baseline/location/LocationStatus.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ public static void updateStatus(@NonNull Context context) {
4545
return;
4646
}
4747
// GPS signal status
48-
if (Services.bluetooth.preferences.preferenceEnabled && Services.bluetooth.getState() != BluetoothState.BT_CONNECTED) {
48+
final int btState = Services.bluetooth.getState();
49+
if (Services.bluetooth.preferences.preferenceEnabled && btState != BluetoothState.BT_CONNECTED) {
4950
// Bluetooth enabled, but not connected
5051
icon = R.drawable.warning;
51-
switch (Services.bluetooth.getState()) {
52+
switch (btState) {
5253
case BluetoothState.BT_STARTING:
5354
message = "GPS bluetooth starting...";
5455
break;
@@ -60,7 +61,7 @@ public static void updateStatus(@NonNull Context context) {
6061
break;
6162
default:
6263
message = "GPS bluetooth not connected";
63-
Log.e(TAG, "Bluetooth inconsistent state: preference enabled, state " + BT_STATES[Services.bluetooth.getState()]);
64+
Log.e(TAG, "Bluetooth inconsistent state: preference enabled, state " + BT_STATES[btState]);
6465
}
6566
} else {
6667
// Internal GPS, or bluetooth connected:

app/src/main/java/com/platypii/baseline/views/bluetooth/BluetoothAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
5656
final BluetoothItem device = devices.get(position - 1);
5757
nameView.setText(device.name);
5858
addressView.setText(device.address);
59-
if (device.name.contains("GPS") || device.name.startsWith("Mohawk")) {
59+
if (device.name.contains("GPS") || device.name.startsWith("FlySight") || device.name.startsWith("Mohawk")) {
6060
nameView.setTextColor(0xffeeeeee);
6161
} else {
6262
nameView.setTextColor(0xffbbbbbb);

common/src/main/java/com/platypii/baseline/bluetooth/BleService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private void scan() {
106106
setState(BT_SCANNING);
107107
// Scan for peripherals with a certain service UUIDs
108108
central.startPairingPopupHack();
109-
Log.i(TAG, "Scanning for laser rangefinders");
109+
Log.i(TAG, "Scanning for ble devices");
110110
// TODO: Check for permissions
111111
central.scanForPeripherals(); // TODO: filter with services
112112
}

common/src/main/java/com/platypii/baseline/bluetooth/BluetoothDeviceComparator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ public int compare(@NonNull BluetoothItem device1, @NonNull BluetoothItem device
1212
return score(device2) - score(device1);
1313
}
1414

15+
/**
16+
* Devices with higher score will appear higher in the sorted list
17+
*/
1518
private int score(@NonNull BluetoothItem device) {
1619
if (device.name.isEmpty()) return 0;
17-
if (device.name.startsWith("Mohawk")) return 2;
20+
if (device.name.startsWith("FlySight")) return 4;
21+
if (device.name.startsWith("Mohawk")) return 3;
22+
if (device.name.contains("GPS")) return 2;
1823
if (device.name.startsWith("RaceBox")) return 1;
19-
if (device.name.contains("GPS")) return 1;
2024
return 0;
2125
}
2226
}

common/src/main/java/com/platypii/baseline/bluetooth/BluetoothService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public class BluetoothService {
3737
public final PubSub<MLocation> locationUpdates = new PubSub<>();
3838

3939
// BLE subsystem
40-
public final BleService ble = new BleService(new MohawkProtocol(locationUpdates));
40+
public final BleService ble = new BleService(
41+
new MohawkProtocol(locationUpdates),
42+
new Flysight2Protocol(locationUpdates)
43+
);
4144

4245
// Android shared preferences for bluetooth
4346
public final BluetoothPreferences preferences = new BluetoothPreferences();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.platypii.baseline.bluetooth;
2+
3+
import com.platypii.baseline.location.LocationCheck;
4+
import com.platypii.baseline.location.NMEAException;
5+
import com.platypii.baseline.measurements.MLocation;
6+
import com.platypii.baseline.util.Exceptions;
7+
import com.platypii.baseline.util.PubSub;
8+
9+
import android.bluetooth.le.ScanRecord;
10+
import android.util.Log;
11+
import androidx.annotation.NonNull;
12+
import androidx.annotation.Nullable;
13+
import com.welie.blessed.BluetoothPeripheral;
14+
import com.welie.blessed.GattStatus;
15+
import java.nio.ByteBuffer;
16+
import java.nio.ByteOrder;
17+
import java.util.UUID;
18+
19+
public class Flysight2Protocol extends BleProtocol {
20+
private static final String TAG = "FlysightProtocol";
21+
private final PubSub<MLocation> locationUpdates;
22+
23+
// Flysight services
24+
private static final UUID flysightService0 = UUID.fromString("00000000-cc7a-482a-984a-7f2ed5b3e58f");
25+
private static final UUID flysightService1 = UUID.fromString("00000001-cc7a-482a-984a-7f2ed5b3e58f");
26+
private static final UUID flysightService2 = UUID.fromString("00000002-cc7a-482a-984a-7f2ed5b3e58f");
27+
// Flysight characteristics
28+
private static final UUID flysightCharacteristicGNSS = UUID.fromString("00000000-8e22-4541-9d4c-21edae82ed19");
29+
private static final UUID flysightCharacteristicTX = UUID.fromString("00000001-8e22-4541-9d4c-21edae82ed19");
30+
private static final UUID flysightCharacteristicRX = UUID.fromString("00000002-8e22-4541-9d4c-21edae82ed19");
31+
32+
public Flysight2Protocol(@NonNull PubSub<MLocation> locationUpdates) {
33+
this.locationUpdates = locationUpdates;
34+
}
35+
36+
@Override
37+
public boolean canParse(@NonNull BluetoothPeripheral peripheral, @Nullable ScanRecord record) {
38+
// TODO: Check address
39+
return peripheral.getName().equals("FlySight");
40+
}
41+
42+
@Override
43+
public void onServicesDiscovered(@NonNull BluetoothPeripheral peripheral) {
44+
Log.i(TAG, "flysight services discovered " + peripheral.getCurrentMtu());
45+
peripheral.requestMtu(256);
46+
}
47+
48+
@Override
49+
public void onMtuChanged(@NonNull BluetoothPeripheral peripheral, int mtu, @NonNull GattStatus status) {
50+
Log.i(TAG, "flysight mtu changed " + mtu);
51+
// Subscribe to flysight service
52+
peripheral.setNotify(flysightService1, flysightCharacteristicGNSS, true);
53+
}
54+
55+
@Override
56+
public void processBytes(@NonNull BluetoothPeripheral peripheral, @NonNull byte[] value) {
57+
try {
58+
final ByteBuffer buf = ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN);
59+
final int iTow = buf.getInt(0); // gps time of week
60+
final double lng = buf.getInt(4) * 1e-7;
61+
final double lat = buf.getInt(8) * 1e-7;
62+
final double alt = buf.getInt(12) * 1e-3;
63+
final double vN = buf.getInt(16) * 1e-3;
64+
final double vE = buf.getInt(20) * 1e-3;
65+
final double climb = buf.getInt(24) * -1e-3;
66+
final long millis = System.currentTimeMillis(); // TODO
67+
68+
final int locationError = LocationCheck.validate(lat, lng);
69+
if (locationError == LocationCheck.VALID) {
70+
final MLocation loc = new MLocation(
71+
millis, lat, lng, alt, climb, vN, vE,
72+
Float.NaN, Float.NaN, Float.NaN, Float.NaN, -1, -1
73+
);
74+
Log.i(TAG, "flysight -> app: gps " + loc);
75+
// Update listeners
76+
locationUpdates.post(loc);
77+
} else {
78+
Log.w(TAG, LocationCheck.message[locationError] + ": " + lat + "," + lng);
79+
Exceptions.report(new NMEAException(LocationCheck.message[locationError] + ": " + lat + "," + lng));
80+
}
81+
} catch (Exception e) {
82+
Exceptions.report(e);
83+
}
84+
}
85+
}

common/src/main/java/com/platypii/baseline/location/LocationProviderBluetooth.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void start(@NonNull Context context) throws SecurityException {
6767
}
6868

6969
private void onLocationUpdate(MLocation loc) {
70-
locationUpdates.post(loc);
70+
updateLocation(loc);
7171
}
7272

7373
@Override

0 commit comments

Comments
 (0)