Skip to content

Commit

Permalink
Merge pull request #544 from NordicSemiconductor/improvement/mtu
Browse files Browse the repository at this point in the history
Max usable MTU set to 515 (512 bytes of data)
  • Loading branch information
philips77 authored Feb 16, 2024
2 parents 695fbf4 + 20ed79f commit f924120
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ble/src/main/java/no/nordicsemi/android/ble/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,9 @@ protected void disableBatteryLevelNotifications() {
* <p>
* The returned request must be either enqueued using {@link Request#enqueue()} for
* asynchronous use, or awaited using await() in synchronous execution.
* <p>
* Starting from version 2.7.3 the maximum packet size is 512 bytes, even if the MTU is set to
* 517. See: <a href="https://github.com/NordicSemiconductor/Android-BLE-Library/issues/541">#541</a>.
*
* @return The request.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ abstract class BleManagerHandler extends RequestHandler {
* The current MTU (Maximum Transfer Unit). The maximum number of bytes that can be sent in
* a single packet is MTU-3.
*/
@IntRange(from = 23, to = 515)
private int mtu = 23;
/**
* Current connection parameters. Those values are only available starting from Android Oreo.
Expand Down Expand Up @@ -1732,13 +1733,14 @@ final boolean isReliableWriteInProgress() {
/**
* Returns the current MTU (Maximum Transfer Unit).
*/
@IntRange(from = 23, to = 515)
final int getMtu() {
return mtu;
}

final void overrideMtu(@IntRange(from = 23, to = 517) final int mtu) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
this.mtu = mtu;
this.mtu = Math.min(515, mtu);
}
}

Expand Down Expand Up @@ -2043,7 +2045,7 @@ protected void onCharacteristicIndicated(@NonNull final BluetoothGatt gatt,
*/
@Deprecated
protected void onMtuChanged(@NonNull final BluetoothGatt gatt,
@IntRange(from = 23, to = 517) final int mtu) {
@IntRange(from = 23, to = 515) final int mtu) {
// do nothing
}

Expand Down Expand Up @@ -2696,10 +2698,10 @@ public void onMtuChanged(@NonNull final BluetoothGatt gatt,
final int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
log(Log.INFO, () -> "MTU changed to: " + mtu);
BleManagerHandler.this.mtu = mtu;
BleManagerHandler.this.onMtuChanged(gatt, mtu);
BleManagerHandler.this.mtu = Math.min(515, mtu);
BleManagerHandler.this.onMtuChanged(gatt, BleManagerHandler.this.mtu);
if (request instanceof MtuRequest) {
((MtuRequest) request).notifyMtuChanged(gatt.getDevice(), mtu);
((MtuRequest) request).notifyMtuChanged(gatt.getDevice(), BleManagerHandler.this.mtu);
request.notifySuccess(gatt.getDevice());
}
} else {
Expand Down Expand Up @@ -3184,7 +3186,7 @@ final void onMtuChanged(@NonNull final BluetoothGattServer server,
@NonNull final BluetoothDevice device,
final int mtu) {
log(Log.INFO, () -> "[Server] MTU changed to: " + mtu);
BleManagerHandler.this.mtu = mtu;
BleManagerHandler.this.mtu = Math.min(515, mtu);
nextRequest(checkCondition());
}

Expand Down

0 comments on commit f924120

Please sign in to comment.