Reputation: 589
I am developing a POS application for android. What i need to do is print the bill when you click on a button. The print needs to happen silently to a predetermined printer without any interaction with the app. The printer will be found with its IP address set up in code. The connection should happen true wifi or bluetooth (probably wifi).
What i have tried so far is use the android print framework (In Android 4.4 (API level 19) ), but it seems to be stuck to the gui (Again i dont want any gui for the print, it just needs to happen). I couldn't find the option to set the printer IP true code.
I have followed a simple tutorial to create the code that i have right now. Here is a link to it: http://www.techotopia.com/index.php/An_Android_Custom_Document_Printing_Tutorial
So to recap, i need my custom Android POS app to print bills(or just anything for starters) to a predetermined printer that i set up in code.
It seems that Rongta pos printers has a android sdk. I guess other manufacturers have to have one as well for their printers.
Upvotes: 4
Views: 4101
Reputation: 3200
It is possible for printint to PDF by calling print adapter lifecycle methods. However as callbacks are non public abstract classes and system throws segfault if null is supplied, you need to use DexMaker to implement them. I have implemented for webView adapter like this:
@Override
protected void onPreExecute() {
super.onPreExecute();
printAdapter = webView.createPrintDocumentAdapter();
}
@Override
protected Void doInBackground(Void... voids) {
File file = new File(pdfPath);
if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
// get file descriptor
descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
// create print attributes
PrintAttributes attributes = new PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(new PrintAttributes.Resolution("id", PRINT_SERVICE, 300, 300))
.setColorMode(PrintAttributes.COLOR_MODE_COLOR)
.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0))
.build();
ranges = new PageRange[]{new PageRange(1, numberPages)};
// dexmaker cache folder
cacheFolder = new File(context.getFilesDir() +"/etemp/");
printAdapter.onStart();
printAdapter.onLayout(attributes, attributes, new CancellationSignal(), getLayoutResultCallback(new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
if (method.getName().equals("onLayoutFinished")) {
onLayoutSuccess();
} else {
Log.e(TAG, "Layout failed");
pdfCallback.onPdfFailed();
}
return null;
}
}, cacheFolder), new Bundle());
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e != null ? e.getMessage() : "PrintPdfTask unknown error");
}
return null;
}
private void onLayoutSuccess() throws IOException {
PrintDocumentAdapter.WriteResultCallback callback = getWriteResultCallback(new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
if (method.getName().equals("onWriteFinished")) {
pdfCallback.onPdfCreated();
} else {
Log.e(TAG, "Layout failed");
pdfCallback.onPdfFailed();
}
return null;
}
}, cacheFolder);
printAdapter.onWrite(ranges, descriptor, new CancellationSignal(), callback);
}
/**
* Implementation of non public abstract class LayoutResultCallback obtained via DexMaker
* @param invocationHandler
* @param dexCacheDir
* @return LayoutResultCallback
* @throws IOException
*/
public static PrintDocumentAdapter.LayoutResultCallback getLayoutResultCallback(InvocationHandler invocationHandler,
File dexCacheDir) throws IOException {
return ProxyBuilder.forClass(PrintDocumentAdapter.LayoutResultCallback.class)
.dexCache(dexCacheDir)
.handler(invocationHandler)
.build();
}
/**
* Implementation of non public abstract class WriteResultCallback obtained via DexMaker
* @param invocationHandler
* @param dexCacheDir
* @return LayoutResultCallback
* @throws IOException
*/
public static PrintDocumentAdapter.WriteResultCallback getWriteResultCallback(InvocationHandler invocationHandler,
File dexCacheDir) throws IOException {
return ProxyBuilder.forClass(PrintDocumentAdapter.WriteResultCallback.class)
.dexCache(dexCacheDir)
.handler(invocationHandler)
.build();
}
Upvotes: 0
Reputation: 91
AFAIK you cannot silent print the document in Android. There is an open issue on AOSP issue tracker. https://code.google.com/p/android/issues/detail?id=160908.
Upvotes: 6