diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTClient.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTClient.java index 3019f385a..e9935e184 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTClient.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/ble/PebbleGATTClient.java @@ -44,11 +44,7 @@ class PebbleGATTClient extends BluetoothGattCallback { mContext = context; mBtDevice = btDevice; mPebbleLESupport = pebbleLESupport; - } - - boolean initialize() { connectToPebble(mBtDevice); - return true; } public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { @@ -185,7 +181,6 @@ class PebbleGATTClient extends BluetoothGattCallback { if (mBluetoothGatt != null) { this.close(); } - //mBtDevice.createBond(); mBluetoothGatt = btDevice.connectGatt(mContext, false, this); } 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 b324f9c30..a0136b4f2 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 @@ -103,6 +103,7 @@ class PebbleGATTServer extends BluetoothGattServerCallback { LOG.warn("unexpected write request"); return; } + mPebbleLESupport.mIsConnected = true; LOG.info("write request: offset = " + offset + " value = " + GB.hexdump(value, 0, -1)); int header = value[0] & 0xff; int command = header & 7; @@ -180,8 +181,10 @@ class PebbleGATTServer extends BluetoothGattServerCallback { } void close() { - mBluetoothGattServer.cancelConnection(mBtDevice); - mBluetoothGattServer.clearServices(); - mBluetoothGattServer.close(); + if (mBluetoothGattServer != null) { + mBluetoothGattServer.cancelConnection(mBtDevice); + mBluetoothGattServer.clearServices(); + mBluetoothGattServer.close(); + } } } 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 0b9ac5317..f896c340b 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 @@ -7,7 +7,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import java.io.InterruptedIOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; @@ -20,8 +19,9 @@ public class PebbleLESupport { private PipedInputStream mPipedInputStream; private PipedOutputStream mPipedOutputStream; private int mMTU = 20; + boolean mIsConnected = false; - public PebbleLESupport(Context context, final BluetoothDevice btDevice, PipedInputStream pipedInputStream, PipedOutputStream pipedOutputStream) { + public PebbleLESupport(Context context, final BluetoothDevice btDevice, PipedInputStream pipedInputStream, PipedOutputStream pipedOutputStream) throws IOException { mBtDevice = btDevice; mPipedInputStream = new PipedInputStream(); mPipedOutputStream = new PipedOutputStream(); @@ -35,8 +35,16 @@ public class PebbleLESupport { mPebbleGATTServer = new PebbleGATTServer(this, context, mBtDevice); if (mPebbleGATTServer.initialize()) { mPebbleGATTClient = new PebbleGATTClient(this, context, mBtDevice); - mPebbleGATTClient.initialize(); + try { + Thread.sleep(6000); + if (mIsConnected) { + return; + } + } catch (InterruptedException ignored) { + } } + this.close(); + throw new IOException("conntection failed"); } void writeToPipedOutputStream(byte[] value, int offset, int count) {