1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 18:15:49 +01:00

Pebble 2/LE: honor reconnect tries

This commit is contained in:
Andreas Shimokawa 2016-12-02 00:38:06 +01:00
parent 44f74270df
commit 3eda2d4b81
3 changed files with 17 additions and 11 deletions

View File

@ -44,11 +44,7 @@ class PebbleGATTClient extends BluetoothGattCallback {
mContext = context; mContext = context;
mBtDevice = btDevice; mBtDevice = btDevice;
mPebbleLESupport = pebbleLESupport; mPebbleLESupport = pebbleLESupport;
}
boolean initialize() {
connectToPebble(mBtDevice); connectToPebble(mBtDevice);
return true;
} }
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
@ -185,7 +181,6 @@ class PebbleGATTClient extends BluetoothGattCallback {
if (mBluetoothGatt != null) { if (mBluetoothGatt != null) {
this.close(); this.close();
} }
//mBtDevice.createBond();
mBluetoothGatt = btDevice.connectGatt(mContext, false, this); mBluetoothGatt = btDevice.connectGatt(mContext, false, this);
} }

View File

@ -103,6 +103,7 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
LOG.warn("unexpected write request"); LOG.warn("unexpected write request");
return; return;
} }
mPebbleLESupport.mIsConnected = true;
LOG.info("write request: offset = " + offset + " value = " + GB.hexdump(value, 0, -1)); LOG.info("write request: offset = " + offset + " value = " + GB.hexdump(value, 0, -1));
int header = value[0] & 0xff; int header = value[0] & 0xff;
int command = header & 7; int command = header & 7;
@ -180,8 +181,10 @@ class PebbleGATTServer extends BluetoothGattServerCallback {
} }
void close() { void close() {
mBluetoothGattServer.cancelConnection(mBtDevice); if (mBluetoothGattServer != null) {
mBluetoothGattServer.clearServices(); mBluetoothGattServer.cancelConnection(mBtDevice);
mBluetoothGattServer.close(); mBluetoothGattServer.clearServices();
mBluetoothGattServer.close();
}
} }
} }

View File

@ -7,7 +7,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.PipedInputStream; import java.io.PipedInputStream;
import java.io.PipedOutputStream; import java.io.PipedOutputStream;
@ -20,8 +19,9 @@ public class PebbleLESupport {
private PipedInputStream mPipedInputStream; private PipedInputStream mPipedInputStream;
private PipedOutputStream mPipedOutputStream; private PipedOutputStream mPipedOutputStream;
private int mMTU = 20; 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; mBtDevice = btDevice;
mPipedInputStream = new PipedInputStream(); mPipedInputStream = new PipedInputStream();
mPipedOutputStream = new PipedOutputStream(); mPipedOutputStream = new PipedOutputStream();
@ -35,8 +35,16 @@ public class PebbleLESupport {
mPebbleGATTServer = new PebbleGATTServer(this, context, mBtDevice); mPebbleGATTServer = new PebbleGATTServer(this, context, mBtDevice);
if (mPebbleGATTServer.initialize()) { if (mPebbleGATTServer.initialize()) {
mPebbleGATTClient = new PebbleGATTClient(this, context, mBtDevice); 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) { void writeToPipedOutputStream(byte[] value, int offset, int count) {