簡體   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