|
| 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); |
| 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 | +} |
0 commit comments