Requesting MTU with Bluetooth Low Energy connectio

2019-01-24 17:39发布

问题:

I have a Bluetooth Low Energy Application which requires an MTU size above the default 23 bytes.

Though Android introduced BluetoothGatt#requestMTU() in API 21, is there any way, including use of private APIs, to accomplish this pre API 21?

回答1:

If you have control over the peripheral device, you can issue an MTU request (ATT_OP_MTU_REQ, opcode 0x02) from the peripheral. Android is capable of larger GATT MTUs (update: 517 bytes is the maximum value), if requested by the peripheral, and will happily send an according ATT_OP_MTU_RESP.

The only tricky thing is that the MTU exchange may not overlap with a fragmented read/write. One workaround might be a client-side sleep at a given time of the handshake, where the peripheral sends an MTU request. Another possible workaround would be a READ or WRITE on a "magic" characteristic that triggers the MTU exchange.

However, with an increased MTU, I'm much more often receiving Status=133 errors, possibly caused by a higher packet error rate with larger packets?