Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix printer is not printed, but callback to printingOrderSentSuccessfully #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void onReceive(Context context, Intent intent) {
}
};

public void send(byte[] msg) {
public void send(byte[] msg) throws IOException {
sendMessage(msg);
}

Expand Down Expand Up @@ -228,13 +228,14 @@ public void disconnect() {
}
}

public void sendMessage(byte[] msg) {
public void sendMessage(byte[] msg) throws IOException {
try {
out.write(msg);
} catch (final IOException e) {
connected = false;
if (deviceCallback != null)
deviceCallback.onDeviceDisconnected(device, e.getMessage());
throw e;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.mazenrashed.printooth.data.DeviceCallback
import com.mazenrashed.printooth.data.PairedPrinter
import com.mazenrashed.printooth.data.printable.Printable
import com.mazenrashed.printooth.data.printer.Printer
import java.io.IOException

class Printing(private var printer: Printer, private var pairedPrinter: PairedPrinter, val context: Context) {
private lateinit var printables: List<Printable>
Expand Down Expand Up @@ -41,7 +42,6 @@ class Printing(private var printer: Printer, private var pairedPrinter: PairedPr
bluetooth.setDeviceCallback(object : DeviceCallback {
override fun onDeviceConnected(device: BluetoothDevice) {
printPrintables()
printingCallback?.printingOrderSentSuccessfully()
}

override fun onDeviceDisconnected(device: BluetoothDevice, message: String) {
Expand All @@ -63,21 +63,29 @@ class Printing(private var printer: Printer, private var pairedPrinter: PairedPr
}

private fun printPrintables() {
bluetooth.send(printer.initPrinterCommand) // init printer
this.printables.forEach {
it.getPrintableByteArray(printer).forEach { ops ->
bluetooth.send(ops)
try {
bluetooth.send(printer.initPrinterCommand) // init printer
this.printables.forEach {
it.getPrintableByteArray(printer).forEach { ops ->
bluetooth.send(ops)
}
}

//Feed 2 lines to cut the paper
if (extraLinesAtEnd > 0) {
bluetooth.send(printer.feedLineCommand.plus(extraLinesAtEnd))
}
}

//Feed 2 lines to cut the paper
if (extraLinesAtEnd > 0) {
bluetooth.send(printer.feedLineCommand.plus(extraLinesAtEnd))
Handler(Looper.getMainLooper()).postDelayed({
bluetooth.disconnect()
}, 2000)

printingCallback?.printingOrderSentSuccessfully()
} catch (e: IOException) {
printingCallback?.onError("Couldn't send data to the other device. Error: " + e.message)
}

Handler(Looper.getMainLooper()).postDelayed({
bluetooth.disconnect()
}, 2000)

}

fun print(printables: ArrayList<Printable>) {
Expand Down