0
我正在接收蓝牙数据的压缩结构分裂了3个数据包,其中每个数据包的第一个字节是这个数据包的数字,其余的是数据。我使用下面的代码来组装stuct:memcpy与偏移量结构在swift
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
guard let value = characteristic.value as NSData? else { return }
var packetNumber = UInt8()
let maxPacketSize = 17
value.getBytes(&packetNumber, length: 1)
memcpy(&settings + maxPacketSize * (Int(packetNumber) - 1), value.bytes + 1, value.length - 1)
}
它与第一个数据包的效果很好,但第二个和第三个数据包不会被复制到结构,但它仍然由零填补。 objc中的相同方法适用于所有3个数据包。哪里不对?
谢谢!