CBPeripheral-“ didWriteValueFor”得到错误“准备队列已满。”

问题描述 投票:0回答:1

我正在使用ZPL打印机来打印base64解码的字符串。

对于小的解码字符串,它正在打印收据,对于多行base64解码的字符串,我得到了错误。

代理方法:

 func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
          if error != nil {
            print(error?.localizedDescription)
        }
    }

[print(error?.localizedDescription)时出现以下错误

错误:准备队列已满

提前感谢。

swift core-bluetooth ios13 zebra-printers cbperipheral
1个回答
0
投票

如果有人在寻找答案。

当打​​印数据很大时,应先将其分成多个块,然后分别传递每个块进行打印。

下面是创建数据块的代码

 func createChunks(data: Data) -> ([Data]) {
          var chunks : [Data] = []
          let length = data.count
            let chunkSize = 80
            var offset = 0
            repeat {
              let thisChunkSize = ((length - offset) > chunkSize) ? chunkSize : (length - offset);
              let chunk = data.subdata(in: offset..<offset + thisChunkSize )
              chunks.append(chunk)
              offset += thisChunkSize;
            } while (offset < length);
            return chunks
        }

在将数据发送到打印之前调用此方法。

下面是方法,当用户点击打印选项时调用。

func printReciept(text: String) {
        let zpl =  text +  ("\r\n")
        if let payload = zpl.data(using: .utf8) {
            chunksToBeSent = chunks.count
            chunksIsAlreadySent = 0
            chunks = createChunks(data: payload)
            chunks.forEach { (chunk) in
                 writeChucks(chunk: chunk)
            }
        }
    }

下面的话题也来自他们的官方演示Github问题。

Issue Link

© www.soinside.com 2019 - 2024. All rights reserved.