1、IOS发送数据有两种模式:
模式1:
var peripheral: CBPeripheral?
func writeDataToPeripheral(data: Data, characteristic: CBCharacteristic) {
peripheral?.writeValue(data, for: characteristic, type:.withResponse)
}
模式2:
var peripheral: CBPeripheral?
func writeDataToPeripheral(data: Data, characteristic: CBCharacteristic) {
peripheral?.writeValue(data, for: characteristic, type:.withoutResponse)
}
2、.withoutResponse 类型写入数据,通过 peripheralIsReady(toSendWriteWithoutResponse:)判断数据是否发送完毕,开始下一段数据发送。
如果使用 .withResponse 类型写入数据,可以在 didWriteValueForCharacteristic 方法中根据 error 参数判断写入是否成功。如果写入失败,可以暂停发送数据,等待一段时间后再尝试发送。
感谢豆包