2

I have an Android GATT server with the following code:

// Data length = 24 bytes
void onNewData(GattServerDelegate gattServer, byte[] data) { // data = 24 bytes
    characteristic.setValue(data);

    // MTU = 23
    gattServer.notifyCharacteristicChanged(mNotifyDevice, characteristic, false);
}

Because the data is longer than the MTU size, the client only receives part of the message. Could I:

  • Request a new MTU from the server side? or,
  • Split the data in two packets?
Thijs
  • 714
  • 9
  • 23

1 Answers1

3

The answer is yes to both questions, with a small detail.

MTU can be requested only from a GATT client and not from a GATT server. A GATT server can only respond with its MTU when a MTU exchange is initated by the client.

However, a device can have both the GATT server role and the GATT client role at the same time to the same device. The same MTU is used for both. So simply open a client connection and call https://developer.android.com/reference/android/bluetooth/BluetoothGatt#requestMtu(int). Or just make sure the remote device initiates MTU exchange before you send a notification.

You can also split the data and send multiple notifications. When doing that, make sure you wait for this callback https://developer.android.com/reference/android/bluetooth/BluetoothGattServerCallback#onNotificationSent(android.bluetooth.BluetoothDevice,%20int) before sending the next notification.

Also, note that maximum notification size is MTU - 3, since the ATT header must fit as well.

Emil
  • 16,784
  • 2
  • 41
  • 52
  • That's it, thank you! Just curious, any particular reasons to choose one approach over the other? – Thijs Feb 08 '23 at 12:21
  • 1
    Sending a big packet instead of two small, when possible, usually results in higher throughput (assuming both devices support LE Data Length extension). With multiple small packets you need additional logic to correctly combine these. – Emil Feb 08 '23 at 14:36
  • ATT packets have a 3 bytes header, increasing the MTU replaces overhead bytes with data bytes hence higher throughput if you're able to use less Link Layer packets to send the payload. DLE (BT 4.2/5) is something else that allows you to increase the PDU (Link Layer payload size), – Gal Ben-Haim Feb 15 '23 at 06:54