我正在从 Android 设备连接到蓝牙打印机并进行打印。如果数据太大,它只会打印一半。:(我不知道为什么会这样......
以下是日志
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
我使用基于 Qt 5.4.1(MSVC 2010,32 位)和Android 4.4.2的 Qt Creator 3.3.1(开源)
我看到这里报告了一个错误,即使它说已修复..我收到此错误:(
有什么解决方法可以做到这一点吗?
// 蓝牙打印.cpp
#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;
}
}
并用文本调用 write 函数......