使用蓝牙打印机从您的 Android 应用程序打印 [英] Print from your Android application with Bluetooth Printer

查看:26
本文介绍了使用蓝牙打印机从您的 Android 应用程序打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Android 设备开发一个 android 计费应用程序.在我的应用程序中,我必须在活动显示时以特定格式在蓝牙打印机中打印收据,如下所示.我必须像我一样打印整个屏幕如下所示.我想知道如何识别最近的蓝牙打印机?如何配置它们?以及如何在蓝牙打印机中打印数据.该应用程序与以前的版本兼容.我搜索了很多,但没有得到任何积极的回应.

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" />

推荐答案

我会告诉你我为我的应用程序做了什么.可能那不适合你.首先你必须将活动转换为 pdf使用 itext .然后将 pdf 保存到 sdcard 中.从那里您必须使用任何支持打印的应用程序打开 pdf.您必须将该应用程序安装到您的设备中.我使用了 PrinterShare .对我来说它的工作正常.它满足了我的需求.代码如下.

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);
                        if (cr4.getCount() > 0) {
                            cr4.moveToFirst();
                            oldbalance = cr4.getString(cr4
                                    .getColumnIndex("oldbalance"));
                            curosbalance = cr4.getString(cr4
                                    .getColumnIndex("curosbalance"));
                            amtcollected = cr4.getString(cr4
                                    .getColumnIndex("amtcollected"));
                            paymode = cr4.getString(cr4
                                    .getColumnIndex("paymode"));
                            cheque = cr4.getString(cr4
                                    .getColumnIndex("ischeque"));
                            bank = cr4.getString(cr4.getColumnIndex("bank"));
                            chequenumber = cr4.getString(cr4
                                    .getColumnIndex("chequenumber"));
                            chequeamount = cr4.getString(cr4
                                    .getColumnIndex("chequeamount"));
                            chequedate = cr4.getString(cr4
                                    .getColumnIndex("chequedate"));
                            pdccheque = cr4.getString(cr4
                                    .getColumnIndex("ispdc"));
                            bolcheque = Boolean.parseBoolean(cheque);
                            bolpdc = Boolean.parseBoolean(pdccheque);

                            insertCell(table, "OLD BALANCE",
                                    Element.ALIGN_RIGHT, 1, bfBold12);
                            insertCell(table, oldbalance, Element.ALIGN_LEFT,
                                    1, bfBold12);
                            // insert an empty row
                            insertCell(table, "", Element.ALIGN_LEFT, 4,
                                    bfBold12);

                            insertCell(table, "AMT COLLECTED",
                                    Element.ALIGN_RIGHT, 1, bfBold12);
                            insertCell(table, amtcollected, Element.ALIGN_LEFT,
                                    1, bfBold12);
                            // insert an empty row
                            insertCell(table, "", Element.ALIGN_LEFT, 4,
                                    bfBold12);
                            insertCell(table, "CURR_OS_BALANCE:",
                                    Element.ALIGN_RIGHT, 1, bfBold12);
                            insertCell(table, curosbalance, Element.ALIGN_LEFT,
                                    1, bfBold12);
                            // insert an empty row
                            insertCell(table, "", Element.ALIGN_LEFT, 4,
                                    bfBold12);

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

                            // tvcuros.setText("" + curosbalance);
                            // tvoldbalance.setText(oldbalance);
                            // tvamtcollected.setText(amtcollected);
                            // tvpaymode.setText(paymode);
                            // tvpdccheque.setText(pdccheque);
                            // tvcheque.setText(cheque);

                            if (bolcheque) {
                                cheque_layout.setVisibility(View.VISIBLE);
                                // chequedate =
                                // getIntent().getExtras().getString("chequedate")
                                // .toString();
                                // tvbank.setText(bank);
                                // tvchequenumber.setText(chequenumber);
                                // tvchequeamount.setText(chequeamount);
                                // tvchequedate.setText(chequedate);
                                insertCell(table, "BANK", Element.ALIGN_RIGHT,
                                        1, bfBold12);
                                insertCell(table, bank, Element.ALIGN_LEFT, 1,
                                        bfBold12);
                                // insert an empty row
                                insertCell(table, "", Element.ALIGN_LEFT, 4,
                                        bfBold12);

                                insertCell(table, "CHEQUE NO:",
                                        Element.ALIGN_RIGHT, 1, bfBold12);
                                insertCell(table, chequenumber,
                                        Element.ALIGN_LEFT, 1, bfBold12);
                                // insert an empty row
                                insertCell(table, "", Element.ALIGN_LEFT, 4,
                                        bfBold12);

                                insertCell(table, "CHEQUE AMOUNT:",
                                        Element.ALIGN_RIGHT, 1, bfBold12);
                                insertCell(table, chequeamount,
                                        Element.ALIGN_LEFT, 1, bfBold12);
                                // insert an empty row
                                insertCell(table, "", Element.ALIGN_LEFT, 4,
                                        bfBold12);

                                insertCell(table, "CHEQUE DATE:",
                                        Element.ALIGN_RIGHT, 1, bfBold12);
                                insertCell(table, chequedate,
                                        Element.ALIGN_LEFT, 1, bfBold12);
                                // insert an empty row
                                insertCell(table, "", Element.ALIGN_LEFT, 4,
                                        bfBold12);

                            }
                            if (bolpdc) {
                                insertCell(table, "PDC CHEQUE",
                                        Element.ALIGN_RIGHT, 1, bfBold12);
                                insertCell(table, pdccheque,
                                        Element.ALIGN_LEFT, 1, bfBold12);
                                // insert an empty row
                                insertCell(table, "", Element.ALIGN_LEFT, 4,
                                        bfBold12);

                            }
                        } else {
                            Log.d("nzm",
                                    "else for cr4=db.getcusinvaccrevdatas(accrevkey) for "
                                            + accrevkey);

                        }

                    }

                    // add the PDF table to the paragraph
                    paragraph.add(table);

                    // add the paragraph to the document
                    doc.add(paragraph);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    doc.close();
                }

                String sdcard = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/noufalpdfdemo";
                File dir = new File(sdcard);

                // Get the text file
                File file = new File(dir, "sample.pdf");
                file.canRead();
                // FileFinalpath = SdCardpath + "/" + Filepath + Filename;
                // File file = new File(FileFinalpath);
                if (file.exists()) {
                    Uri filepath = Uri.fromFile(file);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(filepath, "application/pdf");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    try {
                        startActivity(intent);
                    } catch (Exception e) {
                        // alert.showAlertDialog(PDF_Activity.this,
                        // "File Not Started...","File Not Started From SdCard ",
                        // false);
                        Log.e("error", "" + e);
                    }

                } else {
                    // alert.showAlertDialog(PDF_Activity.this,
                    // "File Not Found...","File Not Found From SdCard ",
                    // false);

                }

这篇关于使用蓝牙打印机从您的 Android 应用程序打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆