Pebble 2: React to MTU notifications, fixes LE for older pebbles also

It seems that setting the MTU on older pebbles does not work, so just use what we can use.
Maybe old pebbles need setMTU() which only works on Android 5+, we could use that conditionally...
here
Andreas Shimokawa 2016-11-18 23:36:27 +01:00
parent 51fa31aa66
commit 4dc085de57
2 changed files with 19 additions and 7 deletions

View File

@ -15,6 +15,7 @@ import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import static android.bluetooth.BluetoothGattCharacteristic.FORMAT_UINT16;
import static android.bluetooth.BluetoothGattCharacteristic.PROPERTY_WRITE;
@ -32,15 +33,17 @@ class PebbleGATTClient extends BluetoothGattCallback {
private final String mBtDeviceAddress;
private final BluetoothDevice mBtDevice;
private final Context mContext;
private final PebbleLESupport mPebbleLESupport;
private boolean oldPebble = false;
private boolean doPairing = true;
private boolean removeBond = false;
private BluetoothGatt mBluetoothGatt;
PebbleGATTClient(Context context, BluetoothDevice btDevice) {
PebbleGATTClient(PebbleLESupport pebbleLESupport, Context context, BluetoothDevice btDevice) {
mContext = context;
mBtDevice = btDevice;
mPebbleLESupport = pebbleLESupport;
mBtDeviceAddress = btDevice.getAddress();
}
@ -54,7 +57,13 @@ class PebbleGATTClient extends BluetoothGattCallback {
LOG.info("onCharacteristicChanged() unexpected device: " + gatt.getDevice().getAddress() + " , expected: " + mBtDeviceAddress);
return;
}
LOG.info("onCharacteristicChanged()" + characteristic.getUuid().toString() + " " + GB.hexdump(characteristic.getValue(), 0, -1));
if (characteristic.getUuid().equals(MTU_CHARACTERISTIC)) {
int newMTU = characteristic.getIntValue(FORMAT_UINT16, 0);
mPebbleLESupport.setMTU(newMTU);
LOG.info("Pebble requested MTU = " + newMTU);
} else {
LOG.info("onCharacteristicChanged()" + characteristic.getUuid().toString() + " " + GB.hexdump(characteristic.getValue(), 0, -1));
}
}
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
@ -141,8 +150,7 @@ class PebbleGATTClient extends BluetoothGattCallback {
if ((characteristic.getProperties() & PROPERTY_WRITE) != 0) {
characteristic.setValue(new byte[]{1});
gatt.writeCharacteristic(characteristic);
}
else {
} else {
LOG.info("This seems to be some <4.0 FW Pebble, reading pairing trigger");
gatt.readCharacteristic(characteristic);
}

View File

@ -19,6 +19,7 @@ public class PebbleLESupport {
private PebbleGATTClient mPebbleGATTClient;
private PipedInputStream mPipedInputStream;
private PipedOutputStream mPipedOutputStream;
private int mMTU = 20;
public PebbleLESupport(Context context, final String btDeviceAddress, PipedInputStream pipedInputStream, PipedOutputStream pipedOutputStream) {
@ -37,7 +38,7 @@ public class PebbleLESupport {
mPebbleGATTServer = new PebbleGATTServer(this, context, btDevice);
mPebbleGATTServer.initialize();
mPebbleGATTClient = new PebbleGATTClient(context, btDevice);
mPebbleGATTClient = new PebbleGATTClient(this, context, btDevice);
mPebbleGATTClient.initialize();
}
@ -83,13 +84,16 @@ public class PebbleLESupport {
}
}
void setMTU(int mtu) {
mMTU = mtu;
}
private class PipeReader extends Thread {
int mmSequence = 0;
private boolean mQuit = false;
@Override
public void run() {
int MTU = 339 - 3;
byte[] buf = new byte[8192];
int bytesRead;
while (!mQuit) {
@ -113,7 +117,7 @@ public class PebbleLESupport {
int payloadToSend = bytesRead + 4;
int srcPos = 0;
while (payloadToSend > 0) {
int chunkSize = (payloadToSend < (MTU - 1)) ? payloadToSend : MTU - 1;
int chunkSize = (payloadToSend < (mMTU - 4)) ? payloadToSend : mMTU - 4;
byte[] outBuf = new byte[chunkSize + 1];
outBuf[0] = (byte) ((mmSequence++ << 3) & 0xff);
System.arraycopy(buf, srcPos, outBuf, 1, chunkSize);