Open msandwidi opened 3 years ago
So I was able to add a .disconnect()
methode that disconnects the currently connected device. It works as I want but I don't know what I am doing. I never wrote a single line of kotlin code before. If you look at the example, every time I setConnect
, I disconnect the connected device. That way I can print the receipt, disconnect and connect to other printer, then print the order.
Another way of doing it also is to pass a parameter to .connect(mac, {bool dropPreviousConnection : false})
and drop the previous connection base on the dropPreviousConnection
value.
I believe the following is what prevents the newly selected device to connect if there is already another one connect:
//.... more code
if (call.method == "connectPrinter") {
var printerMAC = call.arguments.toString();
if(printerMAC.length>0){
mac = printerMAC;
}else{
result.success("false")
}
GlobalScope.launch(Dispatchers.Main) {
if(outputStream == null) { //<= This may be preventing new device to connect
outputStream = connect()?.also {
//result.success("true")
//Toast.makeText(this@MainActivity, "Connected to printer", Toast.LENGTH_SHORT).show()
}.apply {
result.success(state)
//Log.d(TAG, "finished: Connection state:$state")
}
}
}
}else if (call.method == "disconnectPrinter") {
//... more code
}
This is what I added in the kotlin file
if (call.method == "disconnectPrinter") {
GlobalScope.launch(Dispatchers.Main) {
if(outputStream != null) {
outputStream = disconnect()?.also {
//result.success("true")
//Toast.makeText(this@MainActivity, "Connected to printer", Toast.LENGTH_SHORT).show()
}.apply {
result.success("true")
//Log.d(TAG, "finished: Connection state:$state")
}
}
}
} else if (call.method == "writeBytes") {
//...more code
}
Then in dart
//...more code
/// Disconnect from device
static Future<String?> disconnect() async {
String? result = "false";
try {
result = await _channel.invokeMethod('disconnectPrinter');
} on PlatformException catch (e) {
print("Failed to disconnect: '${e.message}'.");
}
return result;
}
//... more code
I hope this information is helpful to adding the feature.
Please checkout the disconnect branch
branch: https://github.com/msandwidi/BluetoothThermalPrinter/tree/disconnect
Thank you all
@msandwidi thanks
Thank you a lot @msandwidi !! 🎉, you are amazing!, it works like a charm! 🔥
Hello @Rajath4 Thank you for maintaining this package. I was glad that it is the one that fits my receipt printing needs. I have an issue when I tried disconnecting from the currently connected printer to connect to another one.
Let me give you more details about my use case
I have a restaurant point of sale app. After a sale is made, I would like to print a receipt for the customer with the printer at the cashier and print the order with the printer located in the kitchen so the cook can make the meal.
So I need to be able to print on both printer simultaneously. When I use this package, and tried the scenario, both the receipt and the order are getting printed from the same printer.. If I try one printer and switch to the other and print, the printing still goes to the previous printer.
Also I have no way of knowing what printer I is currently connected. Is there a way to know what printer is connected?
Is there a way to disconnect and reconnect to another printer on the fly?
like
Thank you all