Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

printing - Print from your Android application with Bluetooth Printer

I am developing an android billing application for Android devices.In my application i had to print the receipts in the Bluetooth printer in the specific format as the activity displays ,which i showned below.i have to print the whole screen as i showned below. i want to know how to recognize nearest Bluetooth printers?,How to configure them ?and also how to print data in a Bluetooth printer. the app is Compatible for previous version. I searched lot but not got any positive response.

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I will tell you what i did for my app.may be that won't be suit for you.First of all you have to convert the activity into pdf using itext . then save the pdf into sdcard.from there you have to open the pdf using any application which supports printing.you have to install that application into your device.i used PrinterShare . for me its works fine.it fulfilled my needs. the code is given below.

                Document doc = new Document();
                PdfWriter docWriter = null;

                DecimalFormat df = new DecimalFormat("0.00");
                try {
                    Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12,
                            Font.BOLD, new BaseColor(0, 0, 0));
                    Font bf12 = new Font(FontFamily.TIMES_ROMAN, 12);

                    String path = Environment.getExternalStorageDirectory()
                            .getAbsolutePath() + "/noufalpdfdemo";

                    File dir = new File(path);
                    if (!dir.exists())
                        dir.mkdirs();

                    Log.d("PDFCreator", "PDF Path: " + path);
                    File file = new File(dir, "sample.pdf");

                    docWriter = PdfWriter.getInstance(doc,
                            new FileOutputStream(file));

                    // document header attributes
                    doc.addAuthor("betterThanZero");
                    doc.addCreationDate();
                    doc.addProducer();
                    doc.addCreator("MySampleCode.com");
                    doc.addTitle("Report with Column Headings");
                    doc.setPageSize(PageSize.LETTER);
                    // open document
                    doc.open();

                    // create a paragraph
                    PdfContentByte cb = docWriter.getDirectContent();
                    Paragraph paragraph = new Paragraph("", FontFactory
                            .getFont(FontFactory.TIMES_BOLD, 24, BaseColor.RED));
                    paragraph.setAlignment(Element.ALIGN_CENTER);
                    Paragraph para = new Paragraph("INVOICE", FontFactory
                            .getFont(FontFactory.TIMES_BOLD, 24, BaseColor.RED));
                    para.setAlignment(Element.ALIGN_CENTER);
                    doc.add(para);

                    cr = db.getcustomerinvoices(getIntent().getStringExtra("i"));
                    String invoffitemdiscounttotal, invoffinvoicediscounttotal, invdiscount, invnettamount;

                    String date, time, cus_id, c_name, invoiceitem, granttotal;
                    cr.moveToFirst();
                    date = cr.getString(cr.getColumnIndex("date"));
                    time = cr.getString(cr.getColumnIndex("time"));
                    cus_id = cr.getString(cr.getColumnIndex("cus_id"));
                    c_name = cr.getString(cr.getColumnIndex("c_name"));
                    invoiceitem = cr.getString(cr.getColumnIndex("invoiceitem"));
                    granttotal = cr.getString(cr.getColumnIndex("granttotal"));
                    invoffitemdiscounttotal = cr.getString(cr
                            .getColumnIndex("invoffitemdiscounttotal"));
                    invoffinvoicediscounttotal = cr.getString(cr
                            .getColumnIndex("invoffinvoicediscounttotal"));

                    invdiscount = cr.getString(cr.getColumnIndex("Discount"));
                    invnettamount = cr.getString(cr.getColumnIndex("NetAmount"));

                    // add some detail information about the country
                    doc.add(new Paragraph("CUS NAME: " + c_name.trim(), bf12));
                    doc.add(new Paragraph("CUS ID: " + cus_id.trim(), bf12));
                    doc.add(new Paragraph("DATE: " + date.trim(), bf12));
                    doc.add(new Paragraph("INVOICE NUMBER: "
                            + invoicenumber.trim(), bf12));
                    doc.add(Chunk.NEWLINE);
                    doc.add(Chunk.NEWLINE);

                    float[] columnWidths = { 2.5f, 1f, 2f, 2f, 2f, 2f };
                    // create PDF table with the given widths
                    PdfPTable table = new PdfPTable(columnWidths);
                    // set table width a percentage of the page width
                    table.setWidthPercentage(90f);

                    insertCell(table, "Item", Element.ALIGN_RIGHT, 1, bfBold12);
                    insertCell(table, "QTY", Element.ALIGN_LEFT, 1, bfBold12);
                    insertCell(table, "PRICE", Element.ALIGN_LEFT, 1, bfBold12);
                    insertCell(table, "TOTAL", Element.ALIGN_RIGHT, 1, bfBold12);
                    insertCell(table, "DISCOUNT", Element.ALIGN_LEFT, 1,
                            bfBold12);
                    insertCell(table, "TOTAL", Element.ALIGN_RIGHT, 1, bfBold12);

                    table.setHeaderRows(1);
                    // insert an empty row
                    // insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);

                    cr = db.getcustomerinvoicetable(getIntent().getStringExtra(
                            "i"));
                    if (cr.getCount() > 0) {
                        cr.moveToFirst();
                        for (int i = 0; i < cr.getCount(); i++) {

                            String name = cr.getString(cr
                                    .getColumnIndex("itemname"));
                            String price = cr.getString(cr
                                    .getColumnIndex("price"));
                            String qty = cr.getString(cr.getColumnIndex("qty"));
                            String total = cr.getString(cr
                                    .getColumnIndex("total"));
                            String discount = cr.getString(cr
                                    .getColumnIndex("DiscountAmt"));
                            String newtotal = cr.getString(cr
                                    .getColumnIndex("NetAmount"));

                            insertCell(table, name, Element.ALIGN_RIGHT, 1,
                                    bf12);
                            insertCell(table, qty, Element.ALIGN_LEFT, 1, bf12);

                            insertCell(table, price, Element.ALIGN_LEFT, 1,
                                    bf12);
                            insertCell(table, total, Element.ALIGN_RIGHT, 1,
                                    bf12);
                            insertCell(table, discount, Element.ALIGN_LEFT, 1,
                                    bf12);
                            insertCell(table, newtotal, Element.ALIGN_RIGHT, 1,
                                    bf12);

                            cr.moveToNext();

                        }
                        // insertCell(table, "", Element.ALIGN_RIGHT, 1,
                        // bfBold12);
                        // insertCell(table, "", Element.ALIGN_RIGHT, 1,
                        // bfBold12);
                        //
                        // insertCell(table, "", Element.ALIGN_RIGHT, 1,
                        // bfBold12);
                        // insertCell(table, "", Element.ALIGN_RIGHT, 1,
                        // bfBold12);
                        // insertCell(table, "", Element.ALIGN_RIGHT, 1,
                        // bfBold12);
                        // insertCell(table, "", Element.ALIGN_RIGHT, 1,
                        // bfBold12);
                        insertCell(table, "", Element.ALIGN_RIGHT, 6, bfBold12);
                        insertCell(table, "GrandTotal:", Element.ALIGN_RIGHT,
                                2, bfBold12);
                        insertCell(table, granttotal, Element.ALIGN_LEFT, 4,
                                bfBold12);
                        insertCell(table, "OFF LINE ITEM PROMO TOTAL DISCOUNT:", Element.ALIGN_RIGHT,
                                2, bfBold12);
                        insertCell(table, invoffitemdiscounttotal, Element.ALIGN_LEFT, 4,
                                bfBold12);
                        insertCell(table, "OFF INVOICE PROMO TOTAL DISCOUNT:", Element.ALIGN_RIGHT,
                                2, bfBold12);
                        insertCell(table, invoffinvoicediscounttotal, Element.ALIGN_LEFT, 4,
                                bfBold12);
                        insertCell(table, "TOTAL DISCOUNT:", Element.ALIGN_RIGHT,
                                2, bfBold12);
                        insertCell(table, invdiscount, Element.ALIGN_LEFT, 4,
                                bfBold12);
                        insertCell(table, "NET AMOUNT:", Element.ALIGN_RIGHT,
                                2, bfBold12);
                        insertCell(table, invnettamount, Element.ALIGN_LEFT, 4,
                                bfBold12);
                    }
                    // repeat the same as above to display another location
                    insertCell(table, "", Element.ALIGN_LEFT, 6, bfBold12);
                    insertCell(table, "FREE GOODS REVIEW ...",
                            Element.ALIGN_CENTER, 6, bfBold12);

                    insertCell(table, "Item", Element.ALIGN_RIGHT, 1, bfBold12);
                    insertCell(table, "QTY", Element.ALIGN_LEFT, 1, bfBold12);
                    insertCell(table, "", Element.ALIGN_CENTER, 6, bfBold12);

                    table.setHeaderRows(1);
                    // insert an empty row
                    // insertCell(table, "GrandTotal:", Element.ALIGN_RIGHT, 3,
                    // bfBold12);
                    // insertCell(table, granttotal, Element.ALIGN_RIGHT, 1,
                    // bfBold12);

                    Cursor cr2 = db.getinvoiceextras(invoicenumber);

                    if (cr2.getCount() > 0) {
                        cr2.moveToFirst();

                        String fockey = cr2.getString(cr2
                                .getColumnIndex("fockey"));
                        String accrevkey = cr2.getString(cr2
                                .getColumnIndex("accrevkey"));
                        Cursor cr3 = db.getcusinvfocdatas(fockey);
                        if (cr3.getCount() > 0) {
                            cr3.moveToFirst();
                            for (int i = 0; i < cr3.getCount(); i++) {
                                String name = cr3.getString(cr3
                                        .getColumnIndex("focitemname"));
                                String qty = cr3.getString(cr3
                                        .getColumnIndex("focqty"));
                                insertCell(table, name, Element.ALIGN_RIGHT, 1,
                                        bf12);
                                insertCell(table, qty, Element.ALIGN_LEFT, 1,
                                        bf12);

                                cr3.moveToNext();

                            }
                        }

                        else {
                            Log.d("nzm",
                                    "else for cr3:db.getcusinvfocdatas(fockey) for "
                                            + fockey);

                        }
                        insertCell(table, "", Element.ALIGN_LEFT, 6, bfBold12);

//                      insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);
                        insertCell(table, "ACCOUNTS REVIEW ...",
                                Element.ALIGN_CENTER, 6, bfBold12);

                        Cursor cr4 = db.getcusinvaccrevdatas(accrevkey)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...