首页 > 解决方案 > Android 的蓝牙打印无法按预期工作

问题描述

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

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 秒)。我意识到由于空行而变得缓慢。打印机打印它们时速度很慢,但有文本的行没有问题。

所以我正在寻找一种方法来加速该代码,但我被卡住了。我试图使读取缓冲区更大,但似乎什么也没做。

标签: javaandroidprintingbluetoothstream

解决方案


我终于设法让它正常工作。

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


推荐阅读