0

我正在开发一个 iOS 应用程序,我需要将超过 20 个字节的数据发送到蓝牙 LE 中心。我已经设置了一个外设管理器,我一个接一个地发送每个 20 字节的“数据包”。我只在返回 true 时发送下一个数据包(如果返回 false,我会在调用peripheralManager.updatevalue后重试)。大多数情况下这是可行的,但是大约 20% 的时间发送的数据不正确。peripheralManagerIsReadyToUpdateSubscribersupdateValue

我有三个包。大多数情况下,中央接收 A 然后 B 然后 C,但有时中央接收 B 然后 B 然后再 C 或 A 然后 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,信息存储在一个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()
}
4

1 回答 1

0

问题出在客户身上。我的自定义 android 客户端按预期工作。

于 2018-12-12T05:04:25.157 回答