5

I use BluetoothGatt.requestMtu(512) in my android application. First time it works well But Right after Mtu is changed automatically to 23 (default)

I dont know what is the problem...

this a log

`D/BluetoothGatt: setCharacteristicNotification() - uuid: 0000fff1-0000-1000-8000-00805f9b34fb enable: true
D/BluetoothGatt: configureMTU() - device: 74:F0:7D:C0:CB:68 mtu: 512
D/BluetoothGatt: onConfigureMTU() - Device=74:F0:7D:C0:CB:68 mtu=23 status=0
D/BluetoothGatt: discoverServices() - device: 74:F0:7D:C0:CB:68
D/BluetoothGatt: onSearchComplete() = Device=74:F0:7D:C0:CB:68 Status=0
I/gattSuccess: gattSuccess`

this is in onMtuChanged() so it works infinite loop

hungly
  • 63
  • 1
  • 6
  • 2
    Your log does not show that you successfully set the MTU to 512. onConfigureMTU returned with a MTU of 23, the actual MTU. You can only request a MTU, hence the name requestMtu, and the devices will choose the biggest value that works for both. If your peripheral only allows the MTU to be 23 you can never increase it higher. – Michael Kotzjan Jan 25 '21 at 11:48
  • 1
    Thank you. after your answer. I tried to change my buffer to 23byte and separate it. – hungly Jan 27 '21 at 05:08

2 Answers2

7

As M. Kotzjan indicated, this is related to the MTU supported by the other device. From the Bluetooth Specification v5.2, Vol 3, Part G, Section 4.3.1 (Exchange MTU):-

This sub-procedure is used by the client to set the ATT_MTU to the maximum possible value that can be supported by both devices when the client supports a value greater than the default ATT_MTU for the Attribute protocol. This sub- procedure shall only be initiated once during a connection.

In other words, if you request an MTU of 512 and the remote device replies with 23 (the minimum MTU value), the connection's MTU will be 23. If you request 512 and the remote device replies with 100, the connection's MTU will be 100. If you request 200 and the remote device replies with 512, the connection's MTU will be 200.

Have a look at the following link for more information:-

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72
0

You can simply take byteArray size and reduce 3 it will give you data array size.

if (len < 0) {len = buffer.size - 3}

Bibin Baby
  • 163
  • 1
  • 3