将 iOS BLE MTU 大小更改为 512 (SWIFT)

Change iOS BLE MTU size to 512 (SWIFT)

有什么方法可以将 iOS 上的 MTU 值从默认值更改为任意值?

很像 Android 可以执行 requestMTU(512)。

谢谢

不,iOS 上的 MTU 是自动设置的,最大值为 185。这是对您问题的一个很好的回答:

注意:您可以通过这种方式获取 MTU 值(它将比 ATT MTU 少 3 个字节):

connectedPeripheral.maximumWriteValueLength(for: .withoutResponse)

使用参数.withoutResponse很重要,因为.withResponse总是return512(write with response时,iOS自动选择方式:single write if data比 MTU 短,如果数据大于 MTU,则写入排队。

这不是答案,但它仍然很有趣。这是苹果工程师对我发送的问题的回复。

Hi,

Yes, the issue you are seeing is indeed due to the timing problem/race condition I was suspecting. The reason you are getting DPLE and MTU almost simultanously is, iOS has received the MTU request too early, and as it has not been able to determine if the connection supports EDL, it responds with the minimum.

In iOS 15 this race condition is being handled differently, and the MTU response is held off until the link is ready. Technically the peripheral is not doing something wrong, as the spec allows the MTU request to be sent at any time. Just that on iOS 14, on different devices, this issue may pop-up.

I can’t tell you to not send the MTU request at that time, because spec allows it. And iOS 15 is handling it in a way to bypass these issues.

Delaying the MTU request until PHY update may make the problem less likely, but there is no guarantee that it will not happen ever. The root issue here is that HCI events like PHY Update or EDL change are issued by each controller locally and there is no way to guarantee that just because your peripheral reports EDL that the iPhone controller has also done so. And unfortunately this timing/race issue will be different from controller to controller, therefore from phone to phone.

希望对您有所帮助。 我们正在开发的 BLE 外围设备几乎同时发送所有用于连接协商的命令。这就是导致我们无法获得数据长度扩展的原因。