diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java index d3f54ee7..fd8637bb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTServer.java @@ -111,6 +111,11 @@ class PebbleGATTServer extends BluetoothGattServerCallback { int serial = header >> 3; if (command == 0x01) { LOG.info("got ACK for serial = " + serial); + if (mPebbleLESupport.mPPAck != null) { + mPebbleLESupport.mPPAck.countDown(); + } else { + LOG.warn("mPPAck countdownlatch is not present but it probably should"); + } } if (command == 0x02) { // some request? LOG.info("got command 0x02"); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java index 883cc13d..e13552d9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleLESupport.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; +import java.util.concurrent.CountDownLatch; import nodomain.freeyourgadget.gadgetbridge.GBApplication; @@ -23,6 +24,7 @@ public class PebbleLESupport { private int mMTU = 20; private int mMTULimit = Integer.MAX_VALUE; boolean mIsConnected = false; + public CountDownLatch mPPAck; public PebbleLESupport(Context context, final BluetoothDevice btDevice, PipedInputStream pipedInputStream, PipedOutputStream pipedOutputStream) throws IOException { mBtDevice = btDevice; @@ -135,6 +137,7 @@ public class PebbleLESupport { int payloadToSend = bytesRead + 4; int srcPos = 0; + mPPAck = new CountDownLatch(1); while (payloadToSend > 0) { int chunkSize = (payloadToSend < (mMTU - 4)) ? payloadToSend : mMTU - 4; byte[] outBuf = new byte[chunkSize + 1]; @@ -145,7 +148,9 @@ public class PebbleLESupport { payloadToSend -= chunkSize; } - Thread.sleep(500); // FIXME ugly wait 0.5s after each pebble package send to the pebble (we do not wait for the GATT chunks) + mPPAck.await(); + mPPAck = null; + } catch (IOException | InterruptedException e) { LOG.info(e.getMessage()); Thread.currentThread().interrupt();