diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java index 3f06bfcbee..4a7f7fd56f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java @@ -2,6 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge.devices.supercars; import android.content.Intent; import android.os.Bundle; +import android.os.CountDownTimer; import android.widget.CheckBox; import android.widget.CompoundButton; @@ -19,6 +20,12 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView. LocalBroadcastManager localBroadcastManager; boolean lights = false; boolean turbo = false; + CountDownTimer periodicDataSenderRunner; + + SuperCarsConstants.Direction direction = SuperCarsConstants.Direction.CENTER; + SuperCarsConstants.Movement movement = SuperCarsConstants.Movement.IDLE; + SuperCarsConstants.Speed speed = SuperCarsConstants.Speed.NORMAL; + SuperCarsConstants.Light light = SuperCarsConstants.Light.OFF; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,52 +48,84 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView. lights = isChecked; } }); + + //when this activity is open, data is sent continuously every 200ms + periodicDataSender(); + periodicDataSenderRunner.start(); } private void sendLocalBroadcast(Intent intent) { localBroadcastManager.sendBroadcast(intent); } + public void periodicDataSender() { + periodicDataSenderRunner = new CountDownTimer(Long.MAX_VALUE, 200) { + + public void onTick(long millisUntilFinished) { + create_intent_with_data(); + } + + public void onFinish() { + start(); + } + }; + } + + private void create_intent_with_data() { + Intent intent = new Intent(SuperCarsSupport.COMMAND_DRIVE_CONTROL); + intent.putExtra(SuperCarsSupport.EXTRA_DIRECTION, direction); + intent.putExtra(SuperCarsSupport.EXTRA_MOVEMENT, movement); + intent.putExtra(SuperCarsSupport.EXTRA_SPEED, speed); + intent.putExtra(SuperCarsSupport.EXTRA_LIGHT, light); + sendLocalBroadcast(intent); + } + @Override - public void onJoystickMoved(float xPercent, float yPercent, int id) { - if (yPercent != 0 && yPercent != 0) { - SuperCarsConstants.Directions command; - SuperCarsConstants.SpeedModes mode; - if (yPercent < 0) { - command = SuperCarsConstants.Directions.UP; - if (xPercent < -0.5) { - command = SuperCarsConstants.Directions.UP_LEFT; - } else if (xPercent > 0.5) { - command = SuperCarsConstants.Directions.UP_RIGHT; - } - } else { - command = SuperCarsConstants.Directions.DOWN; - if (xPercent < -0.5) { - command = SuperCarsConstants.Directions.DOWN_LEFT; - } else if (xPercent > 0.5) { - command = SuperCarsConstants.Directions.DOWN_RIGHT; - } - } - - mode = SuperCarsConstants.SpeedModes.NORMAL; - if (lights) { - mode = SuperCarsConstants.SpeedModes.LIGHTS; - if (turbo) { - mode = SuperCarsConstants.SpeedModes.TURBO_LIGHTS; - } - } else { - if (turbo) { - mode = SuperCarsConstants.SpeedModes.TURBO; - } - } - - Intent intent = new Intent(SuperCarsSupport.COMMAND_DRIVE_CONTROL); - intent.putExtra(SuperCarsSupport.EXTRA_DIRECTION, command); - intent.putExtra(SuperCarsSupport.EXTRA_MODE, mode); - sendLocalBroadcast(intent); + if (yPercent < 0) { + movement = SuperCarsConstants.Movement.UP; + } else if (yPercent > 0) { + movement = SuperCarsConstants.Movement.DOWN; + } else { + movement = SuperCarsConstants.Movement.IDLE; } + if (xPercent < -0.5) { + direction = SuperCarsConstants.Direction.LEFT; + } else if (xPercent > 0.5) { + direction = SuperCarsConstants.Direction.RIGHT; + } else { + direction = SuperCarsConstants.Direction.CENTER; + } + + if (lights) { + light = SuperCarsConstants.Light.ON; + } else { + light = SuperCarsConstants.Light.OFF; + } + if (turbo) { + speed = SuperCarsConstants.Speed.TURBO; + } else { + speed = SuperCarsConstants.Speed.NORMAL; + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + periodicDataSenderRunner.cancel(); + } + + @Override + protected void onPostResume() { + super.onPostResume(); + periodicDataSenderRunner.start(); + } + + @Override + protected void onPause() { + super.onPause(); + periodicDataSenderRunner.cancel(); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/JoystickView.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/JoystickView.java index a94676283c..44999e0b65 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/JoystickView.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/JoystickView.java @@ -1,6 +1,7 @@ -//GPL-3.0 license -//https://github.com/efficientisoceles/JoystickView - +/* Original code taken from https://github.com/efficientisoceles/JoystickView + Licenced under the terms of the GNU General Public License version 3 + Copyright (C) Daniel Su + */ package nodomain.freeyourgadget.gadgetbridge.devices.supercars; import android.content.Context; @@ -109,9 +110,12 @@ public class JoystickView extends SurfaceView implements SurfaceHolder.Callback, drawJoystick(constrainedX, constrainedY); joystickCallback.onJoystickMoved((constrainedX - centerX) / baseRadius, (constrainedY - centerY) / baseRadius, getId()); } + } else if (e.getAction() == MotionEvent.ACTION_UP) { + drawJoystick(centerX, centerY); + joystickCallback.onJoystickMoved(0, 0, getId()); } else drawJoystick(centerX, centerY); - joystickCallback.onJoystickMoved(0, 0, getId()); + //joystickCallback.onJoystickMoved(0, 0, getId()); } return true; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java index efff672a3a..a15cffdced 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java @@ -4,6 +4,7 @@ import java.util.UUID; public class SuperCarsConstants { + //https://gist.github.com/scrool/e79d6a4cb50c26499746f4fe473b3768#encryption public static final byte[] aes_key = new byte[]{(byte) 0x34, (byte) 0x52, (byte) 0x2A, (byte) 0x5B, (byte) 0x7A, (byte) 0x6E, (byte) 0x49, (byte) 0x2C, (byte) 0x08, (byte) 0x09, (byte) 0x0A, (byte) 0x9D, (byte) 0x8D, (byte) 0x2A, (byte) 0x23, (byte) 0xF8}; public static final UUID SERVICE_UUID_FFF = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb"); @@ -16,118 +17,20 @@ public class SuperCarsConstants { public static final UUID CHARACTERISTIC_UUID_FD1 = UUID.fromString("0000fd01-0000-1000-8000-00805f9b34fb"); public static final UUID CHARACTERISTIC_UUID_FD2 = UUID.fromString("0000fd02-0000-1000-8000-00805f9b34fb"); - public static final byte[] idle_normal = new byte[]{0x02, 0x5e, 0x69, 0x5a, 0x48, (byte) 0xff, 0x2a, 0x43, (byte) 0x8c, (byte) 0xa6, (byte) 0x80, (byte) 0xf8, 0x3e, 0x04, (byte) 0xe4, 0x5d}; - public static final byte[] up_normal = new byte[]{0x29, 0x60, (byte) 0x9c, 0x66, 0x48, 0x52, (byte) 0xcf, (byte) 0xf1, (byte) 0xb0, (byte) 0xf0, (byte) 0xcb, (byte) 0xb9, (byte) 0x80, 0x14, (byte) 0xbd, 0x2c}; - public static final byte[] down_normal = new byte[]{0x03, 0x20, (byte) 0x99, 0x09, (byte) 0xba, (byte) 0x9d, (byte) 0xa1, (byte) 0xc8, (byte) 0xb9, (byte) 0x86, 0x16, 0x3c, 0x6d, 0x48, 0x46, 0x55}; - public static final byte[] up_left_normal = new byte[]{(byte) 0x99, 0x28, (byte) 0xe5, (byte) 0x90, (byte) 0xdf, (byte) 0xe8, 0x21, 0x48, 0x5f, 0x41, 0x4f, (byte) 0xbb, 0x63, 0x3d, 0x5c, 0x4e}; - public static final byte[] up_right_normal = new byte[]{0x0f, 0x2c, (byte) 0xe5, 0x66, 0x62, (byte) 0xd4, (byte) 0xfd, (byte) 0x9d, 0x32, (byte) 0xa4, 0x4f, 0x10, 0x2b, (byte) 0xf2, 0x0a, (byte) 0xa7}; - public static final byte[] down_left_normal = new byte[]{(byte) 0x98, (byte) 0xce, (byte) 0x98, 0x1d, 0x58, (byte) 0xd1, 0x15, (byte) 0xaf, (byte) 0xe1, 0x19, 0x60, (byte) 0xbf, 0x46, 0x13, (byte) 0x92, 0x5c}; - public static final byte[] down_right_normal = new byte[]{(byte) 0xf2, 0x52, 0x0f, (byte) 0xba, 0x31, 0x44, (byte) 0xfb, 0x11, 0x46, (byte) 0x8f, (byte) 0xe0, (byte) 0x80, (byte) 0xc6, (byte) 0xc2, (byte) 0xc2, 0x3c}; - - public static final byte[] idle_lights = new byte[]{0x39, (byte) 0xb5, 0x3b, (byte) 0x9b, (byte) 0xb7, (byte) 0xa0, (byte) 0xe0, (byte) 0xd6, 0x52, 0x54, (byte) 0xf9, (byte) 0xea, (byte) 0x84, 0x5d, (byte) 0xde, (byte) 0xee}; - public static final byte[] up_lights = new byte[]{0x2e, (byte) 0x90, (byte) 0xb1, (byte) 0xd3, 0x6b, (byte) 0x8f, (byte) 0xad, 0x5f, (byte) 0x96, 0x7d, (byte) 0xb3, 0x2e, 0x6e, 0x6c, (byte) 0xfd, 0x6e}; - public static final byte[] down_lights = new byte[]{0x2b, 0x41, 0x0a, 0x47, (byte) 0xce, 0x03, 0x59, (byte) 0xe0, 0x4d, 0x75, (byte) 0xfd, 0x0e, (byte) 0xe3, (byte) 0x9f, (byte) 0xe1, (byte) 0xbd}; - public static final byte[] up_right_lights = new byte[]{0x06, 0x4e, 0x40, (byte) 0x8c, 0x32, 0x52, (byte) 0xfb, 0x32, 0x7b, (byte) 0xd9, (byte) 0xfc, 0x54, (byte) 0x9b, 0x53, (byte) 0xee, 0x3e}; - public static final byte[] up_left_lights = new byte[]{0x23, 0x77, 0x1d, (byte) 0xc2, 0x2a, 0x73, (byte) 0x89, (byte) 0x99, 0x2f, 0x53, (byte) 0xac, 0x59, 0x38, (byte) 0xc1, 0x78, (byte) 0x91}; - public static final byte[] down_right_lights = new byte[]{(byte) 0xbb, (byte) 0xd6, 0x2a, (byte) 0xac, 0x32, (byte) 0x8c, (byte) 0x9e, 0x31, 0x65, 0x33, (byte) 0xc8, 0x0e, (byte) 0x9a, (byte) 0xcb, (byte) 0xf6, 0x4b}; - public static final byte[] down_left_lights = new byte[]{0x6e, (byte) 0xaa, (byte) 0xf0, (byte) 0xc0, (byte) 0x85, (byte) 0x8f, 0x14, 0x77, 0x6f, (byte) 0xd8, (byte) 0xf0, 0x71, 0x39, (byte) 0xa0, 0x08, (byte) 0xf2}; - - public static final byte[] idle_turbo = new byte[]{(byte) 0xd9, (byte) 0x94, (byte) 0xb3, (byte) 0x71, (byte) 0xc3, (byte) 0xbe, (byte) 0x2a, (byte) 0x9a, (byte) 0x9d, (byte) 0x04, (byte) 0x88, (byte) 0xa1, (byte) 0x04, (byte) 0x4b, (byte) 0x7f, (byte) 0x67}; - public static final byte[] up_turbo = new byte[]{(byte) 0xe6, (byte) 0x55, (byte) 0x67, (byte) 0xda, (byte) 0x8e, (byte) 0x6c, (byte) 0x56, (byte) 0x0d, (byte) 0x09, (byte) 0xd3, (byte) 0x73, (byte) 0x3a, (byte) 0x7f, (byte) 0x47, (byte) 0xff, (byte) 0x06}; - public static final byte[] down_turbo = new byte[]{(byte) 0xce, (byte) 0xc2, (byte) 0xff, (byte) 0x1d, (byte) 0x7a, (byte) 0xcc, (byte) 0x16, (byte) 0x3c, (byte) 0xd1, (byte) 0x3b, (byte) 0x7e, (byte) 0x61, (byte) 0x53, (byte) 0xad, (byte) 0x5c, (byte) 0x45}; - public static final byte[] up_right_turbo = new byte[]{(byte) 0xfb, (byte) 0x97, (byte) 0x6f, (byte) 0xba, (byte) 0x04, (byte) 0xaf, (byte) 0x87, (byte) 0x02, (byte) 0x22, (byte) 0x26, (byte) 0xec, (byte) 0x50, (byte) 0xae, (byte) 0x82, (byte) 0xf8, (byte) 0xc4}; - public static final byte[] up_left_turbo = new byte[]{0x59, (byte) 0x23, (byte) 0x81, (byte) 0xc9, (byte) 0x43, (byte) 0xa4, (byte) 0x17, (byte) 0xca, (byte) 0x1b, (byte) 0xc3, (byte) 0xb5, (byte) 0x94, (byte) 0x00, (byte) 0xe0, (byte) 0xfc, (byte) 0x12}; - public static final byte[] down_right_turbo = new byte[]{(byte) 0x80, (byte) 0xdf, (byte) 0xb2, (byte) 0x16, (byte) 0x5f, (byte) 0x32, (byte) 0x60, (byte) 0xf1, (byte) 0xd9, (byte) 0x83, (byte) 0x77, (byte) 0x50, (byte) 0xf4, (byte) 0x3a, (byte) 0x43, (byte) 0xda}; - public static final byte[] down_left_turbo = new byte[]{(byte) 0xd5, (byte) 0x4a, (byte) 0xd5, (byte) 0x58, (byte) 0x57, (byte) 0xd3, (byte) 0x27, (byte) 0x74, (byte) 0x5f, (byte) 0x14, (byte) 0x1d, (byte) 0xd0, (byte) 0x0d, (byte) 0x67, (byte) 0x15, (byte) 0x95}; - - public static final byte[] idle_turbo_lights = new byte[]{(byte) 0x1a, (byte) 0x01, (byte) 0x1e, (byte) 0x9e, (byte) 0x6e, (byte) 0xfc, (byte) 0xce, (byte) 0x22, (byte) 0xbe, (byte) 0x8e, (byte) 0xb7, (byte) 0xff, (byte) 0xb6, (byte) 0x29, (byte) 0xfa, (byte) 0x75}; - public static final byte[] up_turbo_lights = new byte[]{(byte) 0xd6, (byte) 0xc7, (byte) 0x63, (byte) 0x8e, (byte) 0x0e, (byte) 0x9a, (byte) 0x80, (byte) 0xbe, (byte) 0x60, (byte) 0x88, (byte) 0xfc, (byte) 0x44, (byte) 0x43, (byte) 0x07, (byte) 0xa1, (byte) 0x78}; - public static final byte[] down_turbo_lights = new byte[]{(byte) 0x0d, (byte) 0x4f, (byte) 0xf8, (byte) 0x23, (byte) 0xac, (byte) 0xf9, (byte) 0xb7, (byte) 0xef, (byte) 0x1c, (byte) 0x26, (byte) 0xd4, (byte) 0xb4, (byte) 0x56, (byte) 0x51, (byte) 0x59, (byte) 0x52}; - public static final byte[] up_right_turbo_lights = new byte[]{(byte) 0x83, (byte) 0xe6, (byte) 0x59, (byte) 0x42, (byte) 0x3d, (byte) 0x4b, (byte) 0x78, (byte) 0x48, (byte) 0x14, (byte) 0x5d, (byte) 0x86, (byte) 0xa1, (byte) 0x7b, (byte) 0x54, (byte) 0x7e, (byte) 0x58}; - public static final byte[] up_left_turbo_lights = new byte[]{(byte) 0xd1, (byte) 0x7f, (byte) 0x6c, (byte) 0x5e, (byte) 0xe6, (byte) 0xba, (byte) 0x81, (byte) 0xe2, (byte) 0xb5, (byte) 0x80, (byte) 0x90, (byte) 0xa3, (byte) 0xcc, (byte) 0x76, (byte) 0x37, (byte) 0x9f}; - public static final byte[] down_right_turbo_lights = new byte[]{(byte) 0xd4, (byte) 0x5d, (byte) 0xc9, (byte) 0x8f, (byte) 0x76, (byte) 0x58, (byte) 0xf9, (byte) 0x02, (byte) 0x0f, (byte) 0x93, (byte) 0xa0, (byte) 0xfd, (byte) 0x80, (byte) 0xe2, (byte) 0x2d, (byte) 0x45}; - public static final byte[] down_left_turbo_lights = new byte[]{(byte) 0x96, (byte) 0xc0, (byte) 0x35, (byte) 0x77, (byte) 0xe9, (byte) 0xcd, (byte) 0xa8, (byte) 0xb9, (byte) 0x70, (byte) 0x21, (byte) 0x5f, (byte) 0xaf, (byte) 0x35, (byte) 0x00, (byte) 0x3b, (byte) 0x74}; - - public static final byte[] left_data = new byte[]{0x51, 0x38, 0x21, 0x12, 0x13, 0x5c, (byte) 0xcc, (byte) 0xdb, (byte) 0x46, (byte) 0xcf, (byte) 0x89, 0x21, (byte) 0xb7, 0x05, 0x49, (byte) 0x9a}; - public static final byte[] right_data = new byte[]{0x1b, 0x57, 0x69, (byte) 0xcd, (byte) 0xf1, 0x3e, (byte) 0x8a, (byte) 0xb6, 0x27, 0x08, 0x0f, (byte) 0xf3, (byte) 0xce, (byte) 0xfc, 0x3b, (byte) 0xc0}; - - public enum SpeedModes { - NORMAL, TURBO, LIGHTS, TURBO_LIGHTS + public enum Speed { + NORMAL, TURBO } - public enum Directions { - UP, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT + public enum Light { + ON, OFF } - public static byte[] get_directions_data(SpeedModes speedModes, Directions directions) { - switch (speedModes) { - case NORMAL: - switch (directions) { - case UP: - return up_normal; - case UP_LEFT: - return up_left_normal; - case UP_RIGHT: - return up_right_normal; - case DOWN: - return down_normal; - case DOWN_LEFT: - return down_left_normal; - case DOWN_RIGHT: - return down_right_normal; - } - return idle_normal; - case TURBO: - switch (directions) { - case UP: - return up_turbo; - case UP_LEFT: - return up_left_turbo; - case UP_RIGHT: - return up_right_turbo; - case DOWN: - return down_turbo; - case DOWN_LEFT: - return down_left_turbo; - case DOWN_RIGHT: - return down_right_turbo; - } - return idle_turbo; - case LIGHTS: - switch (directions) { - case UP: - return up_lights; - case UP_LEFT: - return up_left_lights; - case UP_RIGHT: - return up_right_lights; - case DOWN: - return down_lights; - case DOWN_LEFT: - return down_left_lights; - case DOWN_RIGHT: - return down_right_lights; - } - return idle_lights; - case TURBO_LIGHTS: - switch (directions) { - case UP: - return up_turbo_lights; - case UP_LEFT: - return up_left_turbo_lights; - case UP_RIGHT: - return up_right_turbo_lights; - case DOWN: - return down_turbo_lights; - case DOWN_LEFT: - return down_left_turbo_lights; - case DOWN_RIGHT: - return down_right_turbo_lights; - } - return idle_turbo_lights; - } - return idle_lights; + public enum Movement { + UP, DOWN, IDLE } + public enum Direction { + LEFT, RIGHT, CENTER + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java index 8b0ed8d9a6..7dc5ccc3f6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java @@ -37,7 +37,9 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { private static final Logger LOG = LoggerFactory.getLogger(SuperCarsSupport.class); public static final String COMMAND_DRIVE_CONTROL = "nodomain.freeyourgadget.gadgetbridge.supercars.command.DRIVE_CONTROL"; public static final String EXTRA_DIRECTION = "EXTRA_DIRECTION"; - public static final String EXTRA_MODE = "EXTRA_MODE"; + public static final String EXTRA_MOVEMENT = "EXTRA_MOVEMENT"; + public static final String EXTRA_SPEED = "EXTRA_SPEED"; + public static final String EXTRA_LIGHT = "EXTRA_LIGHT"; public SuperCarsSupport() { super(LOG); @@ -48,7 +50,7 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { protected TransactionBuilder initializeDevice(TransactionBuilder builder) { builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext())); builder.notify(getCharacteristic(SuperCarsConstants.CHARACTERISTIC_UUID_FFF4), true); //for battery - + builder.setGattCallback(this); LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getContext()); IntentFilter filter = new IntentFilter(); @@ -56,10 +58,8 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { broadcastManager.registerReceiver(commandReceiver, filter); getDevice().setFirmwareVersion("N/A"); getDevice().setFirmwareVersion2("N/A"); - builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext())); - LOG.debug("name " + gbDevice.getName()); - + LOG.debug("Connected to: " + gbDevice.getName()); return builder; } @@ -67,10 +67,11 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(COMMAND_DRIVE_CONTROL)) { - SuperCarsSupport.this.setDirection( - (SuperCarsConstants.SpeedModes) intent.getSerializableExtra(EXTRA_MODE), - (SuperCarsConstants.Directions) intent.getSerializableExtra(EXTRA_DIRECTION) - + send_data( + (SuperCarsConstants.Speed) intent.getSerializableExtra(EXTRA_SPEED), + (SuperCarsConstants.Movement) intent.getSerializableExtra(EXTRA_MOVEMENT), + (SuperCarsConstants.Light) intent.getSerializableExtra(EXTRA_LIGHT), + (SuperCarsConstants.Direction) intent.getSerializableExtra(EXTRA_DIRECTION) ); } } @@ -79,24 +80,66 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { @Override public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { - super.onCharacteristicChanged(gatt, characteristic); - byte[] data = characteristic.getValue(); - byte[] decodedData = new byte[0]; - try { - decodedData = CryptoUtils.decryptAES(data, SuperCarsConstants.aes_key); - } catch (Exception e) { - LOG.error("Error while decoding received data"); + if (super.onCharacteristicChanged(gatt, characteristic)) { + return true; } - if (decodedData.length == 16) { - GBDeviceEventBatteryInfo batteryEvent = new GBDeviceEventBatteryInfo(); - batteryEvent.state = BatteryState.BATTERY_NORMAL; - batteryEvent.level = decodedData[4]; - evaluateGBDeviceEvent(batteryEvent); + UUID characteristicUUID = characteristic.getUuid(); + byte[] byte_data = characteristic.getValue(); + LOG.debug("got characteristics: " + characteristicUUID); + /* + //keep here for now. This requires descriptor reading + if (getDevice().getFirmwareVersion() == "N/A") { + // this is probably not the best way to retrieve UUID descriptor + // but i have not found a better way at this moment + BluetoothGattCharacteristic characteristic1 = getCharacteristic(SuperCarsConstants.CHARACTERISTIC_UUID_FFF1); + BluetoothGattDescriptor descriptor1 = characteristic1.getDescriptor(GattDescriptor.UUID_DESCRIPTOR_GATT_CHARACTERISTIC_USER_DESCRIPTION); + gatt.readDescriptor(descriptor1); + getDevice().setFirmwareVersion(new String(descriptor1.getValue())); + getDevice().sendDeviceUpdateIntent(getContext()); + } else if (getDevice().getFirmwareVersion2() == "N/A") { + BluetoothGattCharacteristic characteristic2 = getCharacteristic(SuperCarsConstants.CHARACTERISTIC_UUID_FFF2); + BluetoothGattDescriptor descriptor2 = characteristic2.getDescriptor(GattDescriptor.UUID_DESCRIPTOR_GATT_CHARACTERISTIC_USER_DESCRIPTION); + gatt.readDescriptor(descriptor2); + //getDevice().setFirmwareVersion2(new String(descriptor2.getValue())); + //getDevice().sendDeviceUpdateIntent(getContext()); + } + */ + + if (SuperCarsConstants.CHARACTERISTIC_UUID_FFF4.equals(characteristicUUID)) { + byte[] decoded_data = decryptData(byte_data); + + if (decoded_data.length == 16) { + GBDeviceEventBatteryInfo batteryEvent = new GBDeviceEventBatteryInfo(); + batteryEvent.state = BatteryState.BATTERY_NORMAL; + batteryEvent.level = decoded_data[4]; + evaluateGBDeviceEvent(batteryEvent); + } } return true; } + private byte[] decryptData(byte[] data) { + byte[] decodedResult = new byte[0]; + try { + decodedResult = CryptoUtils.decryptAES(data, SuperCarsConstants.aes_key); + } catch (Exception e) { + LOG.error("Error while decoding received data: " + e); + } + return decodedResult; + } + + private byte[] encryptData(byte[] data) { + byte[] encodedResult = new byte[0]; + try { + encodedResult = CryptoUtils.encryptAES(data, SuperCarsConstants.aes_key); + } catch (Exception e) { + LOG.error("Error while encoding data to be sent: " + e); + } + return encodedResult; + } + + @Override public void onNotification(NotificationSpec notificationSpec) { @@ -251,15 +294,60 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { return false; } - private void setDirection(SuperCarsConstants.SpeedModes speedModes, SuperCarsConstants.Directions direction) { + private void send_data(SuperCarsConstants.Speed speed, + SuperCarsConstants.Movement movement, + SuperCarsConstants.Light light, + SuperCarsConstants.Direction direction) { - byte[] command = SuperCarsConstants.get_directions_data(speedModes, direction); - TransactionBuilder builder = new TransactionBuilder("setDirections"); + byte[] command = craft_packet(speed, direction, movement, light); + TransactionBuilder builder = new TransactionBuilder("send data"); BluetoothGattCharacteristic writeCharacteristic = getCharacteristic(SuperCarsConstants.CHARACTERISTIC_UUID_FFF1); - builder.write(writeCharacteristic, command); + builder.write(writeCharacteristic, encryptData(command)); builder.queue(getQueue()); } + private byte[] craft_packet(SuperCarsConstants.Speed speed, + SuperCarsConstants.Direction direction, + SuperCarsConstants.Movement movement, + SuperCarsConstants.Light light) { + + byte[] packet = new byte[]{0x0, 0x43, 0x54, 0x4c, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; + + switch (movement) { + case UP: + packet[4] = 0x1; + break; + case DOWN: + packet[5] = 0x1; + break; + } + switch (direction) { + case LEFT: + packet[6] = 0x1; + break; + case RIGHT: + packet[7] = 0x1; + break; + } + switch (light) { + case ON: + packet[8] = 0x0; + break; + case OFF: + packet[8] = 0x1; + break; + } + switch (speed) { + case NORMAL: + packet[9] = 0x50; + break; + case TURBO: + packet[9] = 0x64; + break; + } + return packet; + } + @Override public void dispose() { super.dispose(); diff --git a/app/src/main/res/layout/activity_supercars_control.xml b/app/src/main/res/layout/activity_supercars_control.xml index 4e090ee802..71cac18017 100644 --- a/app/src/main/res/layout/activity_supercars_control.xml +++ b/app/src/main/res/layout/activity_supercars_control.xml @@ -26,14 +26,14 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" - android:text="TurboMode" /> + android:text="@string/supercars_turbo_speed_label" /> + android:text="@string/supercars_lights_label" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d64be7f0cc..6223dac4c4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1845,7 +1845,9 @@ Hourly chime The watch will beep once an hour Flipper zero - Shell Racing Bluetooth Intent API Allow controlling Bluetooth connection via Intent API + Shell Racing + Turbo Speed + Lights