I am building an Android Application that helps users to print a receipt. What I've done so far:
Right now, I can find the device/printer(DATECS DP-25) through Bluetooth and connect it to the printer. The connection flow works perfectly. Problem is that my printer doesn't start printing at all with the commands that I've introduced so far.
Probably commands are wrong, or I don't really know exactly what is the problem. I tried a lot of apps but none of them with success in printing. Just one app from PlayStore it's working but no access to the code of that app.
Code for the printing flow:
Initializing variables:
private suspend fun init(): Boolean {
return withContext(Dispatchers.IO) {
return@withContext try {
uuidSting = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb")
bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuidSting)
bluetoothSocket.connect()
outputStream = bluetoothSocket.outputStream
inputStream = bluetoothSocket.inputStream
printWriter = PrintWriter(outputStream.bufferedWriter(Charsets.ISO_8859_1))
true
} catch (e: Exception) {
println(e.stackTraceToString())
false
}
}
}
Print function:
fun print(text: String){
printerController.apply {
initPrinter()
patchText(text)
nextLine()
nextLine()
}
}
@Throws(IOException::class)
fun initPrinter() {
printWriter.write(0x1B)
printWriter.write(0x40)
printWriter.flush()
}
@Throws(IOException::class)
fun patchText(text: String) {
printWriter.println(text)
printWriter.flush()
}
@Throws(IOException::class)
fun nextLine() {
printWriter.write("\n")
printWriter.flush()
}
I would be more than happy if anybody can help me with some advice or directions.