【问题标题】:Bluetooth Printing from Android not working as expectedAndroid 的蓝牙打印无法按预期工作
【发布时间】:2020-03-24 08:40:09
【问题描述】:

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

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

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

【问题讨论】:

  • “打印机在打印时速度很慢” - 这似乎是打印机问题,而不是 StackOverflow 上可以解决的问题
  • 我不认为这是问题所在,因为使用旧蓝牙设备和我发布的第一个代码的同一台打印机打印没有问题。这就是为什么我猜测代码可以以任何方式进行优化。
  • 我认为“蓝牙设备”是指打印机,而您现在正在使用新打印机。我的错。

标签: java android printing bluetooth stream


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多