swiftcore-bluetoothios13zebra-printerscbperipheral

CBPeripheral - "didWriteValueFor" gets error "The prepare queue is full."


I am using ZPL printer to print the base64 decoded string.

For small decoded string it is printing the receipt, for multiline base64 decoded string I am getting error.

Delegate Method :

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

I am getting following error when print(error?.localizedDescription)

Error :
The prepare queue is full

Thanks in advance.


Solution

  • If anybody is looking for an answer.

    When print data is large it is should be first divided into chunks and then pass each chunk separately for printing.

    Below is the code for creating data chunks

     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
            }
    

    Call this method before sending data to printing.

    Below is the method, this is called when a user hit for printing option.

    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)
                }
            }
        }
    

    Also below thread is from their official demo Github question.

    Issue Link