My android application receive binary data as UDP packet , how should I convert that in android to ASCII ?
Try the following:
byte [] data = (however you extract data from your source);
String result = new String(data, "iso-8859-1");
Related
I getting issue, printing through bluetooth on thermal printer from pdf file become text view.
Print Pdf file via Bluetooth Printer Android I was tried these example but didn't what I expected.
this is my current code
code file source:
String checkout = "checkout";
String fpath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) +"/"+ checkout + ".pdf";
code to printing
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
System.out.println("read " + readNum + " bytes,");
}
} catch (IOException ex) {
System.out.println("ERROR!");
}
byte[] bytesPDF = bos.toByteArray();
byte[] printformat = { 27, 33, 0 }; //try adding this print format
mService.write(printformat);
mService.write(bytesPDF);
I hope able to print pdf file by thermal bluetooth printer. Please help me. Thankyou.
The issue is very clear. As we can see that the printed receipt has formatting syntaxes with it. Which is used to format text and images in a PDF file. So, the printer through which you are trying to print doesn't support printing a PDF file. So, if possible you should provide the file in a compatible format such as a text file.
To know more about formatting text in a Bluetooth printer, you can have a look at this post here. Let me know whether this solves your problem or not.
The way how Thermal printer works is
Open socket connection to printer
Send the encoded data that the printer understands
Close the connection
So, the question here boils down to what's the format of the data to be sent so that the printer is able to understand it and print accordingly. It depends on the manufacturer of the Printer. The encodings are either well documented, packed into an SDK/driver for use or are open source standard encoding for ESC/POS generic printers.
At the end, what you need to do to print a PDF file is -
Convert PDF file to Bitmap[] of pages.
Encode the pages one by one by the command for printing bitmap as provided by the manufacturer.
Pass this encoded string data to the printer.
For Example, do look at the generic ESC/POS implementation in the following GitHub Repo
https://github.com/DantSu/ESCPOS-ThermalPrinter-Android
PrinterTextParserImg.bitmapToHexadecimalString()
I created webapp which sends file by FileRepresentation. Client is an Android app. How can I get File from Restlet Response object on the client side?
The file content will be present within the payload. So you can extract it like any payload with Restlet, as described below:
ClientResource cr = new ClientResource(...);
Representation rep = cr.get();
In fact, the FileRepresentation class is provided in order to fill request / response from a file but can't be used to extract content of a response.
To have access to your response content on the client side, it depends on the file type. If you receive an ascii content, you can do something like that:
Representation representation = cr.get();
String fileContent = representation.getText();
If it's a binary file, you need to work with a stream, as described below:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
cr.get().write(outputStream);
byte[] fileContent = outputStream.toByteArray();
Hope it helps you,
Thierry
My requirement is to print a file from an android device without using any cloud based service.
I have been able to achieve it using "Raw" print protocol i.e by simply sending the file to printer's IP address at Port 9100. Here is the code snippet for that:
client = new Socket(ip,port); //Port is 9100
byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file
outputStream = client.getOutputStream();
outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
The problem with "Raw" printing protocol is that there is no way to get the status back from the printer.
So, I recently read about IPP and LDR using which we can get the status back from printer.
I have tried to find a way to implement them using android but had no success. I have already went through this answer but had no success in finding my solution.
It will be really helpful if someone can guide me on how to implement IPP or LDR in android.
Thanks in advance!
General usage of IPP:
Once a print job has been submitted the printer returns a job-id
Use the Get-Job-Attributes-Operation in order to get the current job-state
Wait until the attribute job-state equals to 9 (means 'completed')
There are other final job-states you should check for: aborted or canceled
For prototyping you could use the ipptool (native for desktop usage):
# ipptool -t -d job=482 ipp://192.168.2.113/ipp job.ipp
{
OPERATION Get-Job-Attributes
GROUP operation-attributes-tag
ATTR charset attributes-charset utf-8
ATTR language attributes-natural-language en
ATTR uri printer-uri $uri
ATTR integer job-id $job
}
Update 5/2020
I have published a kotlin implementation of the ipp protocol.
https://github.com/gmuth/ipp-client-kotlin
Once submitted you can wait for the print job to terminate: job.waitForTermination()
I'm trying to generate filenames in my Android app from a 4 byte byte array. I'm Base64 encoding the byte array with the URL_SAFE option. However, the generated string seems to end with a newline character, which makes it unusable as a filename. Is there anyway to remove the newline?
My code is as follows:
byte[] myByteArray = new byte[4];
myByteArray = generateBytes(myByteArray); // fills the byte array with some data
final String byteString = Base64.encodeToString(myByteArray, Base64.URL_SAFE);
After some googling, I found out that in Android, Base64 encoding automatically inserts a newline after the string, and that using the NO_WRAP flag would solve this. However, is the NO_WRAP flag generated output filename safe?
Thanks.
OK, turns out I can use (Base64.URL_SAFE | Base64.NO_WRAP) to apply both flags.
I have created an android app. It sends a data message on a port for communicating with the same app on some other phone. While sending the message, i have encoded it into binary data using ISO8859_1 encoding.
byte[] b1=payload.getbytes();
I am able to receive the data message at the receiving end. But the problem is that after receving it in binary format , My app needs to decode the message back to string or human read-able format. But i am not able to do the same.
I have tried to convert it into String using 'toString()' but string contains binary character .
pls help.
Try this:
try {
String s = new String(b1, "ISO8859_1");
} catch (UnsupportedEncodingException e) {
// ...
}