BluetoothGatt:协商新的MTU成功,但不能使用新的大小(3个字节的差异)

Ome*_*mer 9 android bluetooth-lowenergy gatt

我正在开发一个使用BLE在设备之间交换数据的应用程序.

为了获得更好的性能,在连接两个设备后,我正在协商增加MTU,以便通过BLE交换更大的数据包.

连接BluetoothDevice并读取所有服务和特性后,我请求使用以下命令增加MTU:

private void requestMtu() {
    //gatt is a BluetoothGatt instance and MAX_MTU is 512
    this.gatt.requestMtu(MAX_MTU);
}
Run Code Online (Sandbox Code Playgroud)

之后,在BluetoothGattCallback实现中,我获得MTU请求成功,新MTU匹配我请求的MTU:

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

    if (status == BluetoothGatt.GATT_SUCCESS) {
        this.supportedMTU = mtu;
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试发送512字节的数据包时,在另一侧(onCharacteristicWriteRequest:)我得到509字节.

有任何想法吗?

小智 23

MTU大小表示可以在ATT有效载荷中使用的最大字节量.ATT写请求有效负载(正在为特性写入发送)如下所示:

1字节属性操作码2字节属性句柄N字节属性值

由于MTU大小为512字节,因此最大大小N可以是512-3 = 509字节