BluetoothGatt: negotiating new MTU succeeds but new size cannot be used (3 bytes difference)

主宰稳场 提交于 2020-01-02 01:35:07

问题


I'm working on an app that exchange data between devices using BLE.

In order to get better performance, after connecting two devices I'm negotiating to increase the MTU in order to exchange bigger data packages over BLE.

Once the BluetoothDevice is connected and all services and characteristics are read, I request to increase the MTU using:

private void requestMtu() {
    //gatt is a BluetoothGatt instance and MAX_MTU is 512
    this.gatt.requestMtu(MAX_MTU);
}

After that, on the BluetoothGattCallback implementation I get the MTU request succeeded and the new MTU matches the one I requested:

@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
    super.onMtuChanged(gatt, mtu, status);

    if (status == BluetoothGatt.GATT_SUCCESS) {
        this.supportedMTU = mtu;
    }
}

The problem is, when I try to send a data package of 512 bytes, on the other side (onCharacteristicWriteRequest:) I get 509 bytes.

Any ideas?


回答1:


The MTU size represents maximum amount of bytes which can be utilized in an ATT payload. An ATT write request payload (which is being send for a characteristic write) looks like the following:

1 byte Attribute Opcode 2 byte Attribute Handle N Byte Attribute Value

Since the MTU size is 512 bytes, the maximum size N can be is 512 - 3 = 509 bytes



来源:https://stackoverflow.com/questions/36435575/bluetoothgatt-negotiating-new-mtu-succeeds-but-new-size-cannot-be-used-3-bytes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!