Skip to content

Commit fe8155c

Browse files
committed
Use desired MTU
1 parent 31a36ae commit fe8155c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

android/app/src/main/java/betaflight/configurator/protocols/ble/BetaflightBlePlugin.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ private static void addDevice(String name, String service, String write, String
101101
private boolean fallbackScan = false;
102102
private List<UUID> requestedServices = new ArrayList<>();
103103

104+
private static final int DESIRED_MTU = 247;
105+
104106
private BleBridgeManager bleManager;
105107
private String connectedAddress;
106108

@@ -555,6 +557,7 @@ private static class BleBridgeManager extends BleManager {
555557
private final UUID serviceUuid;
556558
private final UUID writeUuid;
557559
private final UUID notifyUuid;
560+
private int negotiatedMtu = 23;
558561

559562
private BluetoothGattCharacteristic writeCharacteristic;
560563
private BluetoothGattCharacteristic notifyCharacteristic;
@@ -597,6 +600,11 @@ protected boolean isRequiredServiceSupported(@NonNull BluetoothGatt gatt) {
597600

598601
@Override
599602
protected void initialize() {
603+
requestMtu(DESIRED_MTU)
604+
.with((device, mtu) -> negotiatedMtu = mtu)
605+
.fail((device, status) -> Log.w(TAG, "MTU request failed with status " + status))
606+
.enqueue();
607+
600608
if (notifyCharacteristic != null) {
601609
enableNotifications(notifyCharacteristic).enqueue();
602610
}
@@ -613,7 +621,8 @@ WriteRequest send(byte[] data) {
613621
if (writeCharacteristic == null) {
614622
return null;
615623
}
616-
return writeCharacteristic(writeCharacteristic, data).split();
624+
int chunkSize = Math.max(negotiatedMtu - 3, 20); // 3 bytes ATT overhead
625+
return writeCharacteristic(writeCharacteristic, data).split(chunkSize);
617626
}
618627
}
619628
}

0 commit comments

Comments
 (0)