From 5f69f9bcf089066eda0ced02bde060240bd59881 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Wed, 11 Apr 2018 11:36:40 +0200 Subject: [PATCH] MTU value available in the BleManager --- .../android/nrftoolbox/ble/BleManager.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/wear/src/main/java/no/nordicsemi/android/nrftoolbox/ble/BleManager.java b/wear/src/main/java/no/nordicsemi/android/nrftoolbox/ble/BleManager.java index 696d35d6..cb3c0f26 100644 --- a/wear/src/main/java/no/nordicsemi/android/nrftoolbox/ble/BleManager.java +++ b/wear/src/main/java/no/nordicsemi/android/nrftoolbox/ble/BleManager.java @@ -101,6 +101,10 @@ public class BleManager implements BleProfileApi { private int mConnectionState = BluetoothGatt.STATE_DISCONNECTED; /** Last received battery value or -1 if value wasn't received. */ private int mBatteryValue = -1; + /** + * The current MTU (Maximum Transfer Unit). The maximum number of bytes that can be sent in a single packet is MTU-3. + */ + private int mMtu = 23; private final BroadcastReceiver mBluetoothStateBroadcastReceiver = new BroadcastReceiver() { @Override @@ -579,6 +583,31 @@ public class BleManager implements BleProfileApi { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && enqueue(Request.newMtuRequest(mtu)); } + /** + * Returns the current MTU (Maximum Transfer Unit). MTU specifies the maximum number of bytes that can + * be sent in a single write operation. 3 bytes are used for internal purposes, so the maximum size is MTU-3. + * The value will changed only if requested with {@link #requestMtu(int)} and a successful callback is received. + * If the peripheral requests MTU change, the {@link BluetoothGattCallback#onMtuChanged(BluetoothGatt, int, int)} + * callback is not invoked, therefor the returned MTU value will not be correct. + * Use {@link android.bluetooth.BluetoothGattServerCallback#onMtuChanged(BluetoothDevice, int)} to get the + * callback with right value requested from the peripheral side. + * @return the current MTU value. Default to 23. + */ + protected final int getMtu() { + return mMtu; + } + + /** + * This method overrides the MTU value. Use it only when the peripheral has changed MTU and you + * received the {@link android.bluetooth.BluetoothGattServerCallback#onMtuChanged(BluetoothDevice, int)} + * callback. If you want to set MTU as a master, use {@link #requestMtu(int)} instead. + * @param mtu the MTU value set by the peripheral. + */ + protected final void overrideMtu(final int mtu) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) + mMtu = mtu; + } + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private boolean internalRequestMtu(final int mtu) { final BluetoothGatt gatt = mBluetoothGatt;