Android print to network printer - android

I want to include feature in my android app to print image from network printer through WiFi. I tried below code, posted on stackoverflow, but I am only able to print text :( What to do if I want to print images.
try
{
Socket sock = new Socket("192.168.1.222", 9100);
PrintWriter oStream = new PrintWriter(sock.getOutputStream());
oStream.println("HI,test from Android Device");
oStream.println("\n\n\n");
oStream.close();
sock.close();
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}

Related

How can i print to thermal bluetooth printer?

I develop a mobile point of sale with flutter, but I don't know how to print the receipt on a Bluetooth thermal printer
I currently develop a plugin for it but it doesn't work yet
protected void printBill() {
if(btsocket == null){
btsocket = DeviceList.getSocket();
Intent BTIntent = new Intent(getApplicationContext(), DeviceList.class);
this.startActivityForResult(BTIntent, DeviceList.REQUEST_CONNECT_BT);
Toast.makeText(activity,"tes",Toast.LENGTH_LONG).show();
}
else{
OutputStream opstream = null;
try {
opstream = btsocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
outputStream = opstream;
//print command
try {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
outputStream = btsocket.getOutputStream();
byte[] printformat = new byte[]{0x1B,0x21,0x03};
outputStream.write(printformat);
printCustom("Fair Group BD",2,1);
printCustom("Pepperoni Foods Ltd.",0,1);
printCustom("H-123, R-123, Dhanmondi, Dhaka-1212",0,1);
printCustom("Hot Line: +88000 000000",0,1);
printCustom("Vat Reg : 0000000000,Mushak : 11",0,1);
String dateTime[] = getDateTime();
printText(leftRightAlign(dateTime[0], dateTime[1]));
printText(leftRightAlign("Qty: Name" , "Price "));
printCustom(new String(new char[32]).replace("\0", "."),0,1);
printText(leftRightAlign("Total" , "2,0000/="));
printNewLine();
printCustom("Thank you for coming & we look",0,1);
printCustom("forward to serve you again",0,1);
printNewLine();
printNewLine();
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The source code can be found here
Please help me develop this plugin, at least it can print text
I finally created my own plugin
https://pub.dev/packages/blue_thermal_printer

Android socket not created

I'm developing an Android app that is going to send camera stream to a Node.js web server by Socket. When the app has to create the socket the app doesn't execute the code inside the try statement but even though launchs any catch exception.
MyThread.java
protected Void doInBackground(Void... unused) {
OutputStream os = null;
try {Log.d("MyCameraApp", "HERE1");
mSocket = new Socket(ip, port);
if (mSocket != null) {Log.d("MyCameraApp", "SOCKET CONNECTED");
try {
os = mSocket.getOutputStream();
while (true) {
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(4);
dos.writeUTF("####");
dos.writeInt(mFrameBuffer.size());
dos.writeUTF("-##-");
dos.flush();
dos.write(mFrameBuffer.toByteArray());
dos.flush();
Thread.sleep(1000 / 15);
}
} catch (Exception e) {
e.printStackTrace();
try {
if (os != null)
os.close();
} catch (Exception e2) {
e.printStackTrace();
}
}
}
else {
Log.d("MyCameraApp", "SOCKET NULL");
}
}
catch(UnknownHostException e) {
Log.d("MyCameraApp", "CATCH SOCKET");
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
CameraActivity.java
try {
mThread = new MyThread(getApplicationContext(), SERVERIP, SERVERPORT);
mThread.execute();Log.d("MyCameraApp", "WELL DONE");
}
catch(Exception e) {
e.printStackTrace();
Log.d("MyCameraApp", "CATCH");
}
The CameraActivity create a mThread object and the app only displays the log 'HERE1' inside the try, before the socket is created and then displays the log 'WELL DONE'.
I have a basic Node.js server that only is listening in the correspondent port, nothing else.
What is wrong?
Thanks in advance!
The Android device was connected by wifi and the pc was connected by Ethernet... differents subnets.

Printing to Zebra QLn320 from Android

I am currently trying to print an image saved on my android device on a bt enabled zebra printer and I've followed the examples from the documentation, but I can't for the life of me figure out why it's not printing. The BT icon flashes on the printer for a while so I know the connection is being made, but nothing happens. When i call the printImage() function I am giving it the location of the image and the desired width and height. I know the file exists because I can see it displayed in an imageview. Here is my code:
private void printImageTest() {
new Thread(new Runnable() {
#Override
public void run() {
try {
Looper.prepare();
Connection connection = new BluetoothConnection("AC:3F:A4:13:C2:24");
connection.open();
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
printer.printImage(signatureFile, 100, 100);
Thread.sleep(2000);
connection.close();
Looper.myLooper().quit();
} catch (ConnectionException e) {
e.printStackTrace();
} catch (ZebraPrinterLanguageUnknownException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Please find the link to ZPL language zpl programming guide in section Image Load
Connect to printer:
BluetoothConnection printerConnection = new BluetoothConnection(printerAddress);
printerConnection.open();
if (!printerConnection.isConnected()) {
throw new Exception("Could not open bluetooth connection");
}
//print
ZebraPrinter printer = ZebraPrinterFactory.getInstance(printerConnection);
PrinterLanguage pl = printer.getPrinterControlLanguage();
if (pl == PrinterLanguage.CPCL) {
} else {
//byte[] configLabel = createZplReceipt().getBytes();
byte[] configLabel = zplContent.getBytes();
printerConnection.write(configLabel);
}
printerConnection.close();
Then send a ZPL string to printer:

Android: how to handle when the internet is turned off while downloading a file from internet?

Android: I am downloading a file from the internet using this method:
InputStream in = null;
try {
in = connection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[1024];
int length = 0;
try {
while ((length = in.read(buffer)) != -1) {
try {
fout.write(buffer, 0, length);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
The problem is that when the internet is turned off, the read inoutstream stops reading on some device, but it continues reading on other devices! How to solve this problem? thanks in advance.
EDIT:
I found the solution, I have to set a connection's timeout.
When you establish a connection, put that into a try-catch to know if establishing a connection fails. Handle your problem in the catch block. I don't exactly know how you are establishing the connection, so won't be able to suggest further.

Android Printing via Bluetooth SPP

I need to print plain text in my android application. As far as I can tell I need to create an socket with SPP UDID to my device.
The sample codes I've seen, people just get an OutputStream from the Socket and the push the string with the text they want to print.
Is is that simple? Just push text through OutputStream?
Try this for print text:
try {
Socket clientSocket = null;
DataOutputStream outToServer;
try {
clientSocket = new Socket("192.168.101.64", 9100);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeUTF("Some string to print");
}
outToServer.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}

Categories

Resources