繁体   English   中英

使用CoreBluetooth提高文件传输速度

[英]Improve file transfer speed using CoreBluetooth

我目前有我的OSX应用程序(中央)通过使用writeValue的可写特性将4Kb图像发送到我的iOS应用程序(外围设备)。

由于图像大小超出限制,我使用writeValue多次使用以下函数:

func sendData() {
    while (self.sendDataIndex < self.dataToSend.length) {
        var amountToSend = self.dataToSend.length - self.sendDataIndex!

        if (amountToSend > NOTIFY_MTU) {
            amountToSend = NOTIFY_MTU
        }

        // Copy out the data we want
        var chunk = self.dataToSend.subdataWithRange(NSMakeRange(self.sendDataIndex!, amountToSend))

        // Send it
        self.discoveredPeripheral?.writeValue(chunk, forCharacteristic: self.setupCharacteristic, type: CBCharacteristicWriteType.WithoutResponse)

        // It did send, so update our index
        self.sendDataIndex = self.sendDataIndex!+amountToSend;

    }
    self.discoveredPeripheral?.writeValue("EOM".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true), forCharacteristic: self.setupCharacteristic, type: CBCharacteristicWriteType.WithResponse)
}

我的问题是它需要的时间比我需要的时间长,大约需要30秒。

奇怪的是,即使我在发送图像时使用CBCharacteristicWriteType.WithoutResponse,我也会得到它们的响应,我想知道这是否会让它变慢。

这就是我在外围设备上的特性设置:

var cbProperties = CBCharacteristicProperties.Read|CBCharacteristicProperties.Write|CBCharacteristicProperties.Notify
var cbPermissions = CBAttributePermissions.Readable|CBAttributePermissions.Writeable
var transferCharacteristic = CBMutableCharacteristic(type: CBUUID.UUIDWithString(SETUP_CHARACTERISTIC_UUID), properties: cbProperties, value: nil, permissions: cbPermissions)

我已经为NOTIFY_MTU尝试了不同的值,范围从20到900,但是越高,writeValue执行的次数越少,但每个包到达的时间越长。

我已经看到转移执行得更快,有关如何改进它的任何建议?

谢谢

通过将CBCharacteristicProperties更改为WriteWithoutResponse,我能够将其降低到3秒。

软件包的传输速度非常快,但是我知道它们何时交付时我就失去了响应,所以我必须实现自己的响应以了解传输何时完成。

除此之外它工作得很好!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM