我正在开发一个iOS应用程序,我需要向蓝牙LE中心发送超过20个字节的数据 . 我已经设置了一个外设管理器,我依次发送每个"packet"的20个字节 . 我只在 peripheralManager.updatevalue 返回true时发送下一个数据包(如果 updateValue 返回false,则在 peripheralManagerIsReadyToUpdateSubscribers 被调用后重试) . 大多数情况下这是有效的,但是大约20%的时间发送的数据不正确 .

我有三个包 . 大部分时间中央接收A然后接收B然后接收C,但是有时中央接收B然后再接收B然后再接收C或A然后C然后C接收C.

它始终发送三个通知,但值不正确 .

如果重要的话:

特征的值存储在 BLECharacteristic 对象的实例中

@objc public class BLECharacteristic: CBMutableCharacteristic{
    public var dynamicValue: Data?
    public override init(type UUID: CBUUID, properties: CBCharacteristicProperties, value: Data?, permissions: CBAttributePermissions){
        super.init(type: UUID, properties: properties, value: nil, permissions: permissions)
        self.dynamicValue = value
    }
    public convenience init(characteristic: CBCharacteristic){
        self.init(type: characteristic.uuid, properties: characteristic.properties, value: characteristic.value, permissions: CBAttributePermissions.readable)
    }
}

并且当在 peripheralManagerIsReadyToUpdateSubscribers 被调用之后发送通知"buffered"时,信息存储在 DelayedNotification 对象中 .

@objc public class DelayedNotification: NSObject{
    private(set) var valueToNotify: Data
    private(set) var characteristic: BLECharacteristic
    private(set) var devicesToNotify: [CBCentral]?

    @objc public init(_ data: Data, _ char: BLECharacteristic, _ devsNotify: [CBCentral]?){
        valueToNotify = data
        characteristic = char
        devicesToNotify = devsNotify
    }
}

创建对象时:

var valueToSend: Data
if(characteristic.dynamicValue == nil){
    valueToSend = Data()
}else{
    valueToSend = characteristic.dynamicValue!
}

buffer.append(DelayedNotification(valueToSend, characteristic, devicesToNotify))

编辑:更多代码

private func notifyDevices(_ characteristic: BLECharacteristic){
    DispatchQueue.main.async {
        var valueToSend: Data
        if(characteristic.dynamicValue == nil){
            valueToSend = Data()
        }else{
            valueToSend = Data(characteristic.dynamicValue!)
        }

        self.notificationLock.wait()
        self.notBuffer.append(DelayedNotification(valueToSend, characteristic, nil))
        self.notificationLock.signal()

        self.processNotificationBuffer()
    }
}

private func processNotificationBuffer(){
    DispatchQueue.main.async{
        self.notificationLock.wait()

        for notification in self.notBuffer{
            let res = self.peripheralManager.updateValue(Data(notification.valueToNotify), for: notification.characteristic, onSubscribedCentrals: notification.devicesToNotify)
            if(res){
                NSLog("Sent: " + String(data: notification.valueToNotify, encoding: .utf8)!) // This is always printed in the right order
                notificationSent()
                self.notBuffer.remove(at: self.notBuffer.index(of: notification)!)
            }
        }

        self.notificationLock.signal()
    }
}

@objc public func peripheralManagerIsReady(toUpdateSubscribers peripheral: CBPeripheralManager) {
    processNotificationBuffer()
}