Bluetooth print fails half way and gets disconnected



  • Hi,

    I am connecting to a bluetooth printer from an android device and printing. If the data is too big it only prints half of it. :( I am not sure why is it so...

    The below are the logs
    D/BluetoothUtils(13396): isSocketAllowedBySecurityPolicy start : device null
    W/BluetoothAdapter(13396): getBluetoothService() called with no BluetoothManagerCallback
    D/BluetoothSocket(13396): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[86]}
    E/File (13396): fail readDirectory() errno=2
    D/AcePOS (13396): (null):0 ((null)): void SyncObjAPI::postBillDetails() Exits
    D/AcePOS (13396): (null):0 ((null)): void SyncObjAPI::parseBillPostNwResponse(QNetworkReply*) Invoked
    D/AcePOS (13396): (null):0 ((null)): void SyncObjAPI::parseBillPostNwResponse(QNetworkReply*) network get error happened "Protocol "" is unknown"
    W/System.err(13396): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    W/System.err(13396): at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:550)
    W/System.err(13396): at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:527)
    W/System.err(13396): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:357)
    W/System.err(13396): at dalvik.system.NativeStart.run(Native Method)
    W/AcePOS (13396): (null):0 ((null)): qt.bluetooth.android: Falling back to workaround.
    W/AcePOS (13396): (null):0 ((null)): qt.bluetooth.android: Cannot determine RFCOMM service channel.
    W/BluetoothAdapter(13396): getBluetoothService() called with no BluetoothManagerCallback
    D/BluetoothSocket(13396): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[88]}
    W/System.err(13396): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    W/System.err(13396): at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:550)
    W/System.err(13396): at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:527)
    W/System.err(13396): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:357)
    W/System.err(13396): at dalvik.system.NativeStart.run(Native Method)
    W/AcePOS (13396): (null):0 ((null)): qt.bluetooth.android: Socket connect via workaround failed.
    D/AcePOS (13396): (null):0 ((null)): void BluetoothPrint::handleErrorOccurance(QBluetoothSocket::SocketError) Invoked QBluetoothSocket::ServiceNotFoundError
    D/AcePOS (13396): (null):0 ((null)): Connection to service failed
    D/AcePOS (13396): (null):0 ((null)): void ErrorDialog::setTitle(QString) Invoked "ERROR IN BLUETOOTH PRINT"
    D/AcePOS (13396): (null):0 ((null)): void ErrorDialog::setTitle(QString) Exits
    D/AcePOS (13396): (null):0 ((null)): void ErrorDialog::setMessage(QString) Invoked "Connection to service failed"
    D/AcePOS (13396): (null):0 ((null)): void ErrorDialog::setIcon(QIcon) Invoked
    D/AcePOS (13396): (null):0 ((null)): void BluetoothPrint::handleErrorOccurance(QBluetoothSocket::SocketError) Exit
    D/AcePOS (13396): (null):0 ((null)): void BluetoothPrint::connectedDev() Invoked
    D/AcePOS (13396): (null):0 ((null)): void BluetoothPrint::connectedDev() to send data
    W/System.err(13396): java.io.IOException: socket closed
    W/System.err(13396): at android.net.LocalSocketImpl$SocketInputStream.read(LocalSocketImpl.java:92)
    W/System.err(13396): at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:472)
    W/System.err(13396): at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
    W/System.err(13396): at java.io.InputStream.read(InputStream.java:162)
    W/System.err(13396): at org.qtproject.qt5.android.bluetooth.QtBluetoothInputStreamThread.run(QtBluetoothInputStreamThread.java:77)
    D/AcePOS (13396): (null):0 ((null)): disconnected() Invoked

    I use Qt Creator 3.3.1 (opensource)
    Based on Qt 5.4.1 (MSVC 2010, 32 bit)

    and Android 4.4.2

    I saw a bug reported here , even though it says fixed.. i get this error :(

    Any fix is there to do this?

    Code is below

    #include "bluetoothprint.h"
    #include "commondefenitions.h"
    #include <QIcon>
    #include <QPrinter>
    #include <QTimer>
    BluetoothPrint::BluetoothPrint(QObject *parent) :
        QObject(parent), m_pBtSocket(NULL)
    {
        m_pErrorDlg = new ErrorDialog();
    
        m_pBtSocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
        mCommand    = false;
    
        connect(m_pBtSocket, SIGNAL(connected()), this, SLOT(connectedDev()));
        connect(m_pBtSocket, SIGNAL(disconnected()), this, SLOT(disconnectedDev()));
        connect(m_pBtSocket, SIGNAL(error(QBluetoothSocket::SocketError)),this,
                         SLOT(handleErrorOccurance(QBluetoothSocket::SocketError)));
    
    }
    
    void BluetoothPrint::write(QByteArray data, QString ipAddress, bool logo)
    {
        qDebug() << Q_FUNC_INFO << "Invoked";
    
        if (NULL == m_pBtSocket)
        {
            return;
        }
    
        mCommand = false;
        mLogo = logo;
        m_data = data;
        m_ipAddress = ipAddress;
    
        static const QLatin1String serviceUuid("00001101-0000-1000-8000-00805F9B34FB");
    
        qDebug() << Q_FUNC_INFO << "Create socket";
    
        if(NULL != ipAddress)
        {
            QBluetoothAddress address = QBluetoothAddress(ipAddress);
            m_pBtSocket->connectToService(address,QBluetoothUuid(serviceUuid),QIODevice::ReadWrite);
            qDebug() << Q_FUNC_INFO << "ConnectToService done";
        }
        else
        {
            qDebug() << Q_FUNC_INFO << "IP Address is empty";
        }
    
        qDebug() << Q_FUNC_INFO << "Exit";
    }
    
    void BluetoothPrint::writeCommand(QByteArray data, QString ipAddress)
    {
        qDebug() << Q_FUNC_INFO << "Invoked";
        mCommand = true;
        m_cashDrawerData = data;
        m_ipAddress = ipAddress;
    
        static const QLatin1String serviceUuid("00001101-0000-1000-8000-00805F9B34FB");
    
        qDebug() << Q_FUNC_INFO << "Create socket";
    
        if(NULL != ipAddress)
        {
            QBluetoothAddress address = QBluetoothAddress(ipAddress);
            m_pBtSocket->connectToService(address,QBluetoothUuid(serviceUuid),QIODevice::ReadWrite);
            qDebug() << Q_FUNC_INFO << "ConnectToService done";
        }
        else
        {
            qDebug() << Q_FUNC_INFO << "IP Address is empty";
        }
    
        qDebug() << Q_FUNC_INFO << "Exit";
    }
    
    void BluetoothPrint::printLogo()
    {
        qDebug() << Q_FUNC_INFO << "Invoked";
    
        QByteArray align;
        align[0] = 27;
        align[1] = 97;
        align[2] = 1;
    
        QByteArray res;
        res[0] = 28;
        res[1] = 112;
        res[2] = 1;
        res[3] = 0;
    
        m_pBtSocket->write(align);
        m_pBtSocket->write(res);
    
        qDebug() << Q_FUNC_INFO << "Exit";
    }
    
    void BluetoothPrint::connectedDev()
    {
        qDebug() << Q_FUNC_INFO << "Invoked";
    
        if (mCommand)
        {
            qDebug() << Q_FUNC_INFO << "to send command";
            m_pBtSocket->write(m_cashDrawerData);
        }
        else
        {
            qDebug() << Q_FUNC_INFO << "to send data";
    
            if (mLogo)
            {
                printLogo();
            }
    
            m_pBtSocket->write(m_data);
            m_pBtSocket->write("\n\n\n\n\n\r");
            m_pBtSocket->write(PARTIAL_PAPER_CUT);
        }
    
        m_pBtSocket->disconnectFromService();
    }
    
    void BluetoothPrint::disconnectedDev()
    {
        qDebug("disconnected() Invoked");
        qDebug(qPrintable("Disconnected from Bluetooth Printer"));
    }
    
    void BluetoothPrint::handleErrorOccurance(QBluetoothSocket::SocketError error)
    {
        qDebug() << Q_FUNC_INFO << "Invoked" << error;
    
        qDebug(qPrintable(m_pBtSocket->errorString()));
        m_pErrorDlg->setTitle("ERROR IN BLUETOOTH PRINT");
        m_pErrorDlg->setMessage(m_pBtSocket->errorString());
        m_pErrorDlg->setIcon(QIcon(":/Images/images/salesscreen/icons/error.png"));
        m_pErrorDlg->show();
    
        qDebug() << Q_FUNC_INFO << "Exit";
    }
    
    BluetoothPrint::~BluetoothPrint()
    {
        if (NULL != m_pBtSocket)
        {
            delete m_pBtSocket;
            m_pBtSocket = NULL;
        }
    }

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.