I'm trying to setup communication using ESC/POS via Bluetooth. I am correctly getting the Bluetooth thermal printer, and the socket indicates that it is connected.
However, when I'm passing the following command (just trying to feed) nothing happens. In fact, nothing happens with any command I attempt to send.
private fun getDeviceByAddress(address: String): BluetoothDevice? {
return bluetoothManager.adapter.bondedDevices.find { it.address == address }
}
suspend fun feed(printer: Printer, lines: Int = 1) {
getDeviceByAddress(printer.address)?.let { device ->
device.createRfcommSocketToServiceRecord(SPP_UUID)
.let { socket ->
try {
socket.connect()
socket.outputStream.write(byteArrayOf(0x1B, 0x40))
socket.outputStream.write(byteArrayOf(0x1B, 0x64, lines.toByte()))
socket.outputStream.flush()
} catch (e: Exception) {
e.printStackTrace()
} finally {
withTimeout(100) {
socket.close()
}
}
}
}
}
What could be going wrong? I also don't know how to verify that the bytes have been written to the printer.
(I know there is an SDK for this specific brand, but I'm trying to do this strictly over ESC/POS)
Based on what you wrote in your question, the operation is probably consistent with the source code you provided.
The first ESC@(=0x1B, 0x40) sent after connecting resets the printer, so any data sent before that will be cleared.
The next ESCd(=0x1B, 0x64, lines.toByte()) command then feeds the paper the number of lines specified.
If you want to use the feed() function provided in your question to print the data that was previously sent to the printer and then feed the paper, you should not send ESC@.