使用Xon和Xoff从打印机获取响应

时间:2011-09-28 05:57:42

标签: android

how to retrieve the printer response from the Bluetooth printer in android by using X-On and X-Off. I have tried but It is not working. I have tried with the following code but it is not working:

CommPortIdentifier portId  = CommPortIdentifier.getPortIdentifier("COM");
SerialPort sPort = (SerialPort)portId .open(this.getClass().getName(), 30000);
sPort.setSerialPortParams(9600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                                            sPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT);

 OutputStream os = sPort.getOutputStream();
 os=  sPort.getOutputStream();
 InputStream is = sPort.getInputStream(); 
os.write(baos.toByteArray(), 0, baos.toByteArray().length);
 StringBuffer inputBuffer = new StringBuffer();

do{
     for(int newData = is.read();newData != -1; newData = is.read())
      {
        inputBuffer.append((char)newData);
      }

      try
       {
         Thread.sleep(300);
       }
      catch
       {
       }
  }while(inputBuffer.length() <= 0);

String sDataIn = inputBuffer.toString();

在上面的程序中,当流程出现在以下行中时,它会出错: CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(“COM”);

错误是:: java.lang.ExceptionInInitializerError

1 个答案:

答案 0 :(得分:0)

咦? Android中不存在CommPortIdentifier类。你是怎么得到这个来编译的?

此外,Android不是Windows,因此串口(如果存在)不会被称为“COM”。

如果您想与蓝牙打印机通话,请首先确保它支持RFCOMM,因为这是Android API中唯一可用的蓝牙协议,其次请参阅蓝牙教程:http://developer.android.com/guide/topics/wireless/bluetooth.html