【问题标题】:Android - Bluetooth Printer not finished printing but throws IOException: Socket ClosedAndroid - 蓝牙打印机未完成打印但抛出 IOException: Socket Closed
【发布时间】:2013-09-02 17:10:01
【问题描述】:

我有一个将数据(文本和图像)打印到热敏打印机的应用程序。

我的问题是,每当我打印冗长的数据时,打印输出都会被剪切,并且我会在打印的 try catch 语句中抛出一个IOException: Socket Closed

这是我打印数据的部分:

protected void simpleComm(Integer port){
    //InputStream tmpIn = null;
    byte[] buffer = new byte[3]; //{65,65,53,53,49,52,65,66,67,68};

    buffer[0] = (byte) 0x08;
    buffer[1] = (byte) 0x99;
    buffer[2] = (byte) 0x04;
    OutputStream tmpOut;// = null;    

    bluetoothAdapter.cancelDiscovery();

    Log.d(this.toString(), "Port = " + port);
    try {


        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
        Method m = blueToothDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
        socket = (BluetoothSocket) m.invoke(blueToothDevice, port);

        //socket = mmDevice.createRfcommSocketToServiceRecord(uuid);

        // assert (socket != null) : "Socket is Null";
        socket.connect();

        try {
            Log.d(this.toString(),"************ CONNECTION SUCCEES! *************");
            try{
                //tmpIn=socket.getInputStream();
                tmpOut = socket.getOutputStream();

                //mmInStream = tmpIn;
                mmOutStream = tmpOut;

            }
            catch(Exception ex){
                Log.e(this.toString(), "Exception " + ex.getMessage());
            }

            //TODO print sequence starts here

            image = "logo";
            print_image(Environment.getExternalStorageDirectory().toString() + "/LOGO.png");

            byte[] arrayOfByte1 = { 27, 33, 0 };
            byte[] format = { 27, 33, 0 };

            mmOutStream.write((newline).getBytes("US-ASCII"));


            //bold
            format[2] = ((byte)(0x0 | arrayOfByte1[2]));  
            //width
            format[2] = ((byte)(0x12 | arrayOfByte1[2]));
            //height
            format[2] = ((byte)(0x8 | arrayOfByte1[2]));
            mmOutStream.write(format);


            for(int i = 0 ; i < orderListForPrinting.size(); i++ ){
                mmOutStream.write(((List.get(i)).getBytes("US-ASCII")));
            }


            //bold
            format[2] = ((byte)(0x8 | arrayOfByte1[2]));  
            //width
            format[2] = ((byte)(0x8 | arrayOfByte1[2]));
            //height
            format[2] = ((byte)(0x8 | arrayOfByte1[2]));
            mmOutStream.write(format);

            mmOutStream.write((newline).getBytes("US-ASCII"));


            printSignature();

            //bold
            format[2] = ((byte)(0x8 | arrayOfByte1[2]));  
            //width
            format[2] = ((byte)(0x8 | arrayOfByte1[2]));
            //height
            format[2] = ((byte)(0x8 | arrayOfByte1[2]));
            mmOutStream.write(format);

            mmOutStream.write(( name + "                  " + newline + newline).getBytes("US-ASCII"));                

        }
        finally{

            orderList = new ArrayList<TotalOrderClass>();
            orderListForPrinting = new ArrayList<String>();
            socket.close();
            mmOutStream.flush();
        }
    } 
    catch (IOException ex){
        Log.e(this.toString(), "IOException: " + ex.getMessage());
    } 
    catch (NoSuchMethodException ex){
        Log.e(this.toString(), "NoSuchMethodException: " + ex.getMessage());
    } 
    catch (IllegalAccessException ex){
        Log.e(this.toString(), "IllegalAccessException: " + ex.getMessage());
    } 
    catch (InvocationTargetException ex){
        Log.e(this.toString(),"InvocationTargetException: " + ex.getMessage());
    }

}

这是我的打印签名功能,和我用来打印标志的功能类似。

private void printSignature() {
    //TODO print signature here
    convertBitmap(signature);
    try{
        mmOutStream.write(PrinterCommands.SET_LINE_SPACING_24);

        int offset = 0;
        while (offset < signature.getHeight()) {
            mmOutStream.write(PrinterCommands.SELECT_BIT_IMAGE_MODE_SIGNATURE);
            for (int x = 0; x < signature.getWidth(); ++x) {

                for (int k = 0; k < 3; ++k) {

                    byte slice = 0;
                    for (int b = 0; b < 8; ++b) {
                        int y = (((offset / 8) + k) * 8) + b;
                        int i = (y * signature.getWidth()) + x;
                        boolean v = false;
                        if (i < dots.length()) {
                            v = dots.get(i);
                        }
                        slice |= (byte) ((v ? 1 : 0) << (7 - b));
                    }
                    mmOutStream.write(slice);
                }
            }
            offset += 24;
            mmOutStream.write(PrinterCommands.FEED_LINE);
        }

        mmOutStream.write(PrinterCommands.SET_LINE_SPACING_30);
    }
    catch (IOException e) {
        Toast.makeText(getApplicationContext(), "Catch on to print signature stuff", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

}

转换位图函数:

public String convertBitmap(Bitmap inputBitmap) {

    int mWidth = inputBitmap.getWidth();
    int mHeight = inputBitmap.getHeight();

    convertArgbToGrayscale(inputBitmap, mWidth, mHeight);
    String mStatus = "ok";
    return mStatus;

}

convertArgbToGrayScale 函数:

private void convertArgbToGrayscale(Bitmap bmpOriginal, int width,
        int height) {
    int pixel;
    int k = 0;
    int B = 0, G = 0, R = 0;
    dots = new BitSet();
    try {

        for (int x = 0; x < height; x++) {
            for (int y = 0; y < width; y++) {
                // get one pixel color
                pixel = bmpOriginal.getPixel(y, x);

                // retrieve color of all channels
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // take conversion up to one single value by calculating
                // pixel intensity.
                R = G = B = (int) (0.299 * R + 0.587 * G + 0.114 * B);
                // set bit into bitset, by calculating the pixel's luma
                if (R < 55) {                       
                    dots.set(k);//this is the bitset that i'm printing
                }
                k++;

            }


        }


    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Catch on to Grayscale stuff", Toast.LENGTH_LONG).show();
    }
}

我现在的问题是调用 printSignature() 函数后,打印机停止打印并且套接字关闭,这就是我收到 IOException: Socket Closed 错误的原因。

我已经运行了几次这个函数,但我不知道为什么套接字会关闭,因为我只在 try-catch 的 finally 子句中关闭套接字。

有什么想法吗?

【问题讨论】:

    标签: android sockets printing bluetooth ioexception


    【解决方案1】:

    我所做的是,我在每个 .Write() 之后放置一个 Thread.sleep(x),其中 x 是我的输出字符串中的字符数量,这在您的代码中可能会有所不同,但程序等待的数量关闭前必须做的事情,与打印的字符(或行)的数量成正比。

    【讨论】:

      【解决方案2】:

      如果你在socket.close() 前面加上Thread.sleep(2000) 会发生什么?

      我在蓝牙 SPP 上工作,我遇到了同样的问题(数据传输结束前套接字关闭)。我认为socket.close()的广播比数据的传输快,那么发送关闭请求时BT打印机不会分析所有数据

      【讨论】:

      • 有更好或更优雅的解决方案吗?我知道这会奏效,但长时间打印输出可能会在随机位置出现中断,现在只需将其延迟固定间隔即可。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 2011-12-28
      • 2011-07-05
      • 1970-01-01
      • 2012-04-07
      • 2014-07-22
      相关资源
      最近更新 更多