Android蓝牙打印机 - 打印HTML文本

时间:2015-02-26 11:50:13

标签: java android printing bluetooth

我正在创建一个Android应用程序,其中应用程序在通过蓝牙连接的打印机上打印数据。 我已通过蓝牙成功连接打印机,应用程序也可以打印纯文本和图像。但是我无法打印HTML文本。

要打印纯文本,我使用以下代码

byte[] cmd = new byte[3];
                cmd[0] = 0x1b;
                cmd[1] = 0x21;
                if((lang.compareTo("en")) == 0){    
                    cmd[2] |= 0x10;
                    //mService.write(cmd);           //±¶¿í¡¢±¶¸ßģʽ
                    mService.sendMessage("Testing!\n\n", "GBK"); 
                    cmd[2] &= 0xEF;
                    //mService.write(cmd);           //È¡Ïû±¶¸ß¡¢±¶¿íģʽ
                    msg = " Hi! This is the plain text.\n\n"
                          " Printer is made in China\n\n";
                    mService.sendMessage(msg,"GBK");
                    }

要打印图像,我使用以下代码

byte[] sendData = null;
        PrintPic pg = new PrintPic();
        pg.initCanvas(384);
        pg.initPaint();
        pg.drawImage(0, 0, "/mnt/sdcard/myimage.jpg");
        sendData = pg.printDraw();
        mService.write(sendData);

有人可以帮我编写代码来打印HTML文本吗?

以下是活动的完整代码(如果有人想要的话)

public class PrintDemo extends Activity {
    Button btnSearch;
    Button btnSendDraw;
    Button btnSend;
    Button btnClose;
    EditText edtContext;
    EditText edtPrint;
    private static final int REQUEST_ENABLE_BT = 2;
    BluetoothService mService = null;
    BluetoothDevice con_dev = null;
    private static final int REQUEST_CONNECT_DEVICE = 1;  //»ñÈ¡É豸ÏûÏ¢



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mService = new BluetoothService(this, mHandler);
        //À¶ÑÀ²»¿ÉÓÃÍ˳ö³ÌÐò
        if( mService.isAvailable() == false ){
            Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
            finish();
        }       
    }

    @Override
    public void onStart() {
        super.onStart();
        //À¶ÑÀδ´ò¿ª£¬´ò¿ªÀ¶ÑÀ
        if( mService.isBTopen() == false)
        {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }
        try {
            btnSendDraw = (Button) this.findViewById(R.id.btn_test);
            btnSendDraw.setOnClickListener(new ClickEvent());
            btnSearch = (Button) this.findViewById(R.id.btnSearch);
            btnSearch.setOnClickListener(new ClickEvent());
            btnSend = (Button) this.findViewById(R.id.btnSend);
            btnSend.setOnClickListener(new ClickEvent());
            btnClose = (Button) this.findViewById(R.id.btnClose);
            btnClose.setOnClickListener(new ClickEvent());
            edtContext = (EditText) findViewById(R.id.txt_content);
            btnClose.setEnabled(false);
            btnSend.setEnabled(false);
            btnSendDraw.setEnabled(false);
        } catch (Exception ex) {
            Log.e("³ö´íÐÅÏ¢",ex.getMessage());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mService != null) 
            mService.stop();
        mService = null;
    }

    class ClickEvent implements View.OnClickListener {
        public void onClick(View v) {
            if (v == btnSearch) {           
                Intent serverIntent = new Intent(PrintDemo.this,DeviceListActivity.class);      //ÔËÐÐÁíÍâÒ»¸öÀàµÄ»î¶¯
                startActivityForResult(serverIntent,REQUEST_CONNECT_DEVICE);
            } else if (v == btnSend) {
                String msg = edtContext.getText().toString();
                if( msg.length() > 0 ){
                    mService.sendMessage(msg+"\n", "GBK");
                }
            } else if (v == btnClose) {
                mService.stop();
            } else if (v == btnSendDraw) {
                String msg = "";
                String lang = getString(R.string.strLang);
                //printImage();

                byte[] cmd = new byte[3];
                cmd[0] = 0x1b;
                cmd[1] = 0x21;
                if((lang.compareTo("en")) == 0){    
                    cmd[2] |= 0x10;
                    //mService.write(cmd);           //±¶¿í¡¢±¶¸ßģʽ
                    mService.sendMessage("Congratulations!\n\n", "GBK"); 
                    cmd[2] &= 0xEF;
                    //mService.write(cmd);           //È¡Ïû±¶¸ß¡¢±¶¿íģʽ
                    msg = "  If you can read this information, you have correctly installed your printer.\n\n"
                          +"  Product Info.\n\n" +
                          " Model:X330\n" +
                          " System Version: 2.1\n" +
                          " Bluetooth Specification: 2.0\n" +
                          " Physical Connection: USB 2.0\n" +
                          " Made in China\n\n";


                    mService.sendMessage(msg,"GBK");

                    }else if((lang.compareTo("ch")) == 0){
                    cmd[2] |= 0x10;
                    //mService.write(cmd);           //±¶¿í¡¢±¶¸ßģʽ
                    mService.sendMessage("¹§Ï²£¡\n\n", "GBK"); 
                    cmd[2] &= 0xEF;
                    //mService.write(cmd);           //È¡Ïû±¶¸ß¡¢±¶¿íģʽ
                    msg = "  Èç¹ûÄú¿ÉÒÔ¶ÁÈ¡Õâ¸öÐÅÏ¢,\n"

                    "  ÖйúÖÆÔì\n\n";

                    mService.sendMessage(msg,"GBK");    
                }
            }
        }
    }

    /**
     * ´´½¨Ò»¸öHandlerʵÀý£¬ÓÃÓÚ½ÓÊÕBluetoothServiceÀà·µ»Ø»ØÀ´µÄÏûÏ¢
     */
    private final  Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case BluetoothService.MESSAGE_STATE_CHANGE:
                switch (msg.arg1) {
                case BluetoothService.STATE_CONNECTED:   //ÒÑÁ¬½Ó
                    Toast.makeText(getApplicationContext(), "Connect successful",
                            Toast.LENGTH_SHORT).show();
                    btnClose.setEnabled(true);
                    btnSend.setEnabled(true);
                    btnSendDraw.setEnabled(true);
                    break;
                case BluetoothService.STATE_CONNECTING:  //ÕýÔÚÁ¬½Ó
                    Log.d("À¶ÑÀµ÷ÊÔ","ÕýÔÚÁ¬½Ó.....");
                    break;
                case BluetoothService.STATE_LISTEN:     //¼àÌýÁ¬½ÓµÄµ½À´
                case BluetoothService.STATE_NONE:
                    Log.d("À¶ÑÀµ÷ÊÔ","µÈ´ýÁ¬½Ó.....");
                    break;
                }
                break;
            case BluetoothService.MESSAGE_CONNECTION_LOST:    //À¶ÑÀÒѶϿªÁ¬½Ó
                Toast.makeText(getApplicationContext(), "Device connection was lost",
                               Toast.LENGTH_SHORT).show();
                btnClose.setEnabled(false);
                btnSend.setEnabled(false);
                btnSendDraw.setEnabled(false);
                break;
            case BluetoothService.MESSAGE_UNABLE_CONNECT:     //ÎÞ·¨Á¬½ÓÉ豸
                Toast.makeText(getApplicationContext(), "Unable to connect device",
                        Toast.LENGTH_SHORT).show();
                break;
            }
        }

    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {    
        switch (requestCode) {
        case REQUEST_ENABLE_BT:      //ÇëÇó´ò¿ªÀ¶ÑÀ
            if (resultCode == Activity.RESULT_OK) {   //À¶ÑÀÒѾ­´ò¿ª
                Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
            } else {                 //Óû§²»ÔÊÐí´ò¿ªÀ¶ÑÀ
                finish();
            }
            break;
        case  REQUEST_CONNECT_DEVICE:     //ÇëÇóÁ¬½ÓijһÀ¶ÑÀÉ豸
            if (resultCode == Activity.RESULT_OK) {   //Òѵã»÷ËÑË÷ÁбíÖеÄij¸öÉ豸Ïî
                String address = data.getExtras()
                                     .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);  //»ñÈ¡ÁбíÏîÖÐÉ豸µÄmacµØÖ·
                con_dev = mService.getDevByMac(address);   

                mService.connect(con_dev);
            }
            break;
        }
    } 

    //´òӡͼÐÎ
    @SuppressLint("SdCardPath")
    private void printImage() {
        byte[] sendData = null;
        PrintPic pg = new PrintPic();
        pg.initCanvas(384);
        pg.initPaint();
        pg.drawImage(0, 0, "/mnt/sdcard/icon.jpg");
        sendData = pg.printDraw();
        mService.write(sendData);   //´òÓ¡byteÁ÷Êý¾Ý
    }
}

0 个答案:

没有答案