From 64b407da92b886a447bc96fd28fb866185156f68 Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Thu, 14 Nov 2019 04:53:19 +0100 Subject: [PATCH] added mtu transactions --- .../btle/AbstractBTLEDeviceSupport.java | 5 +++ .../service/btle/AbstractBTLEOperation.java | 5 +++ .../service/btle/AbstractGattCallback.java | 4 +++ .../gadgetbridge/service/btle/BtLEQueue.java | 13 ++++++++ .../service/btle/GattCallback.java | 2 ++ .../service/btle/TransactionBuilder.java | 11 +++++++ .../btle/actions/RequestMtuAction.java | 31 +++++++++++++++++++ 7 files changed, 71 insertions(+) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/actions/RequestMtuAction.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEDeviceSupport.java index 04f8d1cd1..1b76d03fe 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEDeviceSupport.java @@ -351,6 +351,11 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im } } + @Override + public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { + + } + @Override public void onSetFmFrequency(float frequency) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java index 2ef43fda7..af5f759e2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java @@ -209,4 +209,9 @@ public abstract class AbstractBTLEOperation public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { mSupport.onReadRemoteRssi(gatt, rssi, status); } + + @Override + public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { + mSupport.onMtuChanged(gatt, mtu, status); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractGattCallback.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractGattCallback.java index 4e7a07dd1..0144fd7a2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractGattCallback.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractGattCallback.java @@ -60,4 +60,8 @@ public abstract class AbstractGattCallback implements GattCallback { @Override public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { } + + @Override + public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BtLEQueue.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BtLEQueue.java index 92b56bb33..41af77b3a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BtLEQueue.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BtLEQueue.java @@ -533,6 +533,19 @@ public final class BtLEQueue { checkWaitingCharacteristic(characteristic, status); } + + + @Override + public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { + super.onMtuChanged(gatt, mtu, status); + + if(getCallbackToUse() != null){ + getCallbackToUse().onMtuChanged(gatt, mtu, status); + } + } + + + @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/GattCallback.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/GattCallback.java index 107af38da..16ec0f4f8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/GattCallback.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/GattCallback.java @@ -104,6 +104,8 @@ public interface GattCallback { */ void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status); + void onMtuChanged(BluetoothGatt gatt, int mtu, int status); + // /** // * @see BluetoothGattCallback#onMtuChanged(BluetoothGatt, int, int) // * @param gatt diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/TransactionBuilder.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/TransactionBuilder.java index 5f6974a07..366b80400 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/TransactionBuilder.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/TransactionBuilder.java @@ -18,13 +18,17 @@ package nodomain.freeyourgadget.gadgetbridge.service.btle; import android.bluetooth.BluetoothGattCharacteristic; +import android.os.Build; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; + import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.NotifyAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.ReadAction; +import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.RequestMtuAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WaitAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WriteAction; @@ -56,6 +60,13 @@ public class TransactionBuilder { return add(action); } + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + public TransactionBuilder requestMtu(int mtu){ + return add( + new RequestMtuAction(mtu) + ); + } + public TransactionBuilder notify(BluetoothGattCharacteristic characteristic, boolean enable) { if (characteristic == null) { LOG.warn("Unable to notify characteristic: null"); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/actions/RequestMtuAction.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/actions/RequestMtuAction.java new file mode 100644 index 000000000..7ac30444c --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/actions/RequestMtuAction.java @@ -0,0 +1,31 @@ +package nodomain.freeyourgadget.gadgetbridge.service.btle.actions; + +import android.bluetooth.BluetoothGatt; +import android.bluetooth.BluetoothGattCharacteristic; +import android.os.Build; + +import androidx.annotation.RequiresApi; + +import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction; + +public class RequestMtuAction extends BtLEAction { + private int mtu; + + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + public RequestMtuAction(int mtu) { + super(null); + this.mtu = mtu; + } + + + @Override + public boolean expectsResult() { + return false; + } + + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + @Override + public boolean run(BluetoothGatt gatt) { + return gatt.requestMtu(this.mtu); + } +}