首页 文章

使用蓝牙打印机从Android应用程序打印

提问于
浏览
4

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

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

enter image description here

2 回答

  • 1

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

    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);
    
                    }
    
  • 1

    此应用程序将文本打印到蓝牙打印机 . 我正在使用Android设备和蓝牙打印机 . 您可以轻松地将此库集成到项目中,以便打印支持蓝牙打印机 . 更多示例Click Here

    private void print_bt() {
        try {
        try {
        Thread.sleep(1000);
        } catch (InterruptedException e) {
        e.printStackTrace();
        }
        btoutputstream = btsocket.getOutputStream();
        byte[] printformat = { 0x1B, 0x21, FONT_TYPE };
        btoutputstream.write(printformat);
        String msg = message.getText().toString();
        btoutputstream.write(msg.getBytes());
        btoutputstream.write(0x0D);
        btoutputstream.write(0x0D);
        btoutputstream.write(0x0D);
        btoutputstream.flush();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
    

相关问题