Android的蓝牙打印无法正常工作

问题描述 投票:0回答:1

我一直在使用以下代码将数据发送到蓝牙打印机:

try {
    BluetoothAdapter oBluetoothAdapter  = BluetoothAdapter.getDefaultAdapter();
    BluetoothDevice  oDispositivo       = oBluetoothAdapter.getRemoteDevice(cMAC);

    Method oMethod = oDispositivo.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
    oSocket = (BluetoothSocket) oMethod.invoke(oDispositivo, Integer.valueOf(1));
    oSocket.connect();

    btoutputstream = new BufferedWriter(new OutputStreamWriter(oSocket.getOutputStream(),"ISO_8859_1"));

    // Enviamos el mensaje
    int off = 0;

    while(off < nLength){

        btoutputstream.write(msg,off,nBloque);
        btoutputstream.flush();

        Thread.sleep(nSleep);

        off += nBloque;

        if((off + nBloque) > nLength) nBloque = nLength - off;  
    }

    btoutputstream.flush();

}catch(Exception e){
    return cFail + " || Exception: " + e.toString();
}
finally{
    try{
        if(btoutputstream != null) btoutputstream.close();
        if(oSocket != null) oSocket.close();
    }catch(Exception e2){
        return e2.toString();
    }
}

问题是此代码无法在具有新蓝牙设备的同一打印机上使用。它打印出第一段代码,并且不再打印。

所以我一直在寻找一种使它起作用的方法,但最终我使用了它:

public static String BluetoothPrint()
{
    try{
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mmDevice    = mBluetoothAdapter.getRemoteDevice(cMac);

    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();

    beginListenForData();

    mmOutputStream.write(cText.getBytes());

} catch(Exception e) {
    return "error: " + e.toString();
} finally {
    //      try{
        //          stopWorker = true;
        //          mmOutputStream.close();
        //          mmInputStream.close();
        //          mmSocket.close();
        //      } catch(Exception e) {
        //          return "error: " + e.toString()
        //      }
}

return "ok";
}

public static void beginListenForData(){
    try {
    final Handler handler = new Handler();

    // This is the ASCII code for a newline character
    final byte delimiter = 10;

    stopWorker = false;
    readBufferPosition = 0;
    readBuffer = new byte[1024];

    workerThread = new Thread(new Runnable() {
        public void run() {
            while (!Thread.currentThread().isInterrupted()
            && !stopWorker) {

                try {

                    int bytesAvailable = mmInputStream.available();
                    if (bytesAvailable > 0) {
                        byte[] packetBytes = new byte[bytesAvailable];
                        mmInputStream.read(packetBytes);
                        for (int i = 0; i < bytesAvailable; i++) {
                            byte b = packetBytes[i];
                            if (b == delimiter) {
                                byte[] encodedBytes = new byte[readBufferPosition];
                                System.arraycopy(readBuffer, 0,
                                encodedBytes, 0,
                                encodedBytes.length);
                                final String data = new String(
                                encodedBytes, "US-ASCII");
                                readBufferPosition = 0;

                                handler.post(new Runnable() {
                                    public void run() {
                                        //myLabel.setText(data);
                                    }
                                });
                            } else {
                                readBuffer[readBufferPosition++] = b;
                            }
                        }
                    }

                } catch (Exception ex) {
                    stopWorker = true;
                }

            }
        }
    });

    workerThread.start();
} catch (NullPointerException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

}

此代码工作正确,但是我的主要问题是打印速度非常慢(最多1:30分钟,而之前要在15-20秒内打印)。我意识到由于行空,它的运行速度很慢。打印机在打印它们时速度很慢,但是包含文本的行没有问题。

因此,我正在寻找一种加速该代码的方法,但我遇到了麻烦。我试图使读取缓冲区更大,但似乎什么也没做。

java android printing bluetooth stream
1个回答
0
投票

我终于设法使其正常工作。

在我要发送以打印的文本中,空行只是换行(CRLF)。由于其他打印机都可以正常打印,因此我尝试在空行上添加一个字符(一个空格,因此它可以打印出相同的字符),并且不再缓慢打印。我不知道这是什么原因,但是它的工作速度更快,就像在更换蓝牙之前一样。

© www.soinside.com 2019 - 2024. All rights reserved.