Methods exist in CoreBluetooth that allow you to check the maximum write size, both with and without a response from the peripheral:
func maximumWriteValueLength(for type: CBCharacteristicWriteType ) -> Int
.
There is also a CBPeripheral function that allows you to check if a peripheral is ready to receive a write (without response): var canSendWriteWithoutResponse: Bool { get }
, and a CBPeripheralDelegate function that is triggered when a CBPeripheral is able to receive another write without a response: optional func peripheralIsReady(toSendWriteWithoutResponse peripheral: CBPeripheral )
.
If you buffer the data you plan to send, and only send the maximum packet size allowed per write (checking if the peripheral is ready to receive another write before hand), this may help solve your disconnection issue. You will want to call your write function when a peripheral is ready to receive another write (CBPeripheralDelegate method above is triggered), to empty any potentially remaining data in your buffer.
Let me know if this helps.