What does [peripheral maximumWriteValueLengthForTy

2019-07-25 12:58发布

问题:

I am running the following code:

[peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse];

In my case (iPhone 7, iOS 10.0.2) it returns 512. What is the meaning of this number?

From documentation:

method maximumWriteValueLengthForType:

The maximum amount of data, in bytes, that can be sent to a characteristic in a single write type.

But if I try to send 500 or 600 bytes it works absolutely the same. The target device receives all data. And in both cases response callback is not called.

回答1:

As I understand, in theory it should return the ATT MTU size, as it was requested from the central to the peripheral. From the peripheral side, similar value is central.maximumUpdateValueLength and it is suggested to avoid sending notifications with characteristic values larger than central.maximumUpdateValueLength. But unfortunately this is not clearly documented and also Apple's own example (BTETransfer app) is using default ATT MTU size of 20 to be safe. For notifications, I assume, bad things might happen if you attempt to change more than central.maximumUpdateValueLength bytes.

But from my experiments on Android, I found out that from the other side - central side - some kind of caching is going on and it is possible to send more bytes than the current ATT MTU size (on Android, it is available through onMtuChanged and seems to also be negotiated to 512 immediately after connection). So, we might assume that on iOS the same kind of caching is going on, but I'm not sure how large it can go. On Android, we at least have write callbacks to know when it is safe to write more data. On iOS, not sure what would happen if we try to write, let's say 1MB on a writable characteristic from the central side. But I will try it today.

Also, the behavior might differ among iOS versions because central.maximumUpdateValueLength is available since iOS 7 but peripheral maximumWriteValueLengthForType only since iOS 9.