title: Android Blutooth Printer - Printing HTML Text
body: <p>I am making an android app in which app prints data on printer connected via Bluetooth.
  I have successfully connected printer via Bluetooth and app is able to print plain text and image also. But I am unable to print HTML text.</p>
  
  <p>To print plain text, I use the following code</p>
  
  <pre><code>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] &amp;= 0xEF;
                      //mService.write(cmd);           //È¡Ïû±¶¸ß¡¢±¶¿íÄ£Ê½
                      msg = " Hi! This is the plain text.\n\n"
                            " Printer is made in China\n\n";
                      mService.sendMessage(msg,"GBK");
                      }
  </code></pre>
  
  <p>To print image, I use the following code</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>Can anybody help me to write code to print HTML text ?</p>
  
  <p>Following is the complete code for activity if anybody wants</p>
  
  <pre><code>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() &gt; 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] &amp;= 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] &amp;= 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:     //ÇëÇóÁ¬½ÓÄ³Ò»À¶ÑÀÉè±¸
              if (resultCode == Activity.RESULT_OK) {   //ÒÑµã»÷ËÑË÷ÁÐ±íÖÐµÄÄ³¸öÉè±¸Ïî
                  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Á÷Êý¾Ý
      }
  }
  </code></pre>