diff options
Diffstat (limited to 'src/imports/bluetooth/qdeclarativebluetoothsocket.cpp')
-rw-r--r-- | src/imports/bluetooth/qdeclarativebluetoothsocket.cpp | 53 |
1 files changed, 15 insertions, 38 deletions
diff --git a/src/imports/bluetooth/qdeclarativebluetoothsocket.cpp b/src/imports/bluetooth/qdeclarativebluetoothsocket.cpp index 02c6d308..a1329182 100644 --- a/src/imports/bluetooth/qdeclarativebluetoothsocket.cpp +++ b/src/imports/bluetooth/qdeclarativebluetoothsocket.cpp @@ -54,15 +54,14 @@ /*! \qmltype BluetoothSocket \instantiates QDeclarativeBluetoothSocket - \inqmlmodule QtBluetooth 5.0 - \brief Enables you to connect and communicate with a Bluetooth service or + \inqmlmodule QtBluetooth + \since 5.2 + \brief Enables connecting and communicating with a Bluetooth service or device. \sa QBluetoothSocket \sa QDataStream - The BluetoothSocket type was introduced in \b{QtBluetooth 5.0}. - It allows a QML class connect to another Bluetooth device and exchange strings with it. Data is sent and received using a QDataStream object allowing type safe transfers of QStrings. QDataStream is a well known format and can be @@ -118,8 +117,6 @@ public: QObject::connect(m_socket, SIGNAL(error(QBluetoothSocket::SocketError)), m_dbs, SLOT(socket_error(QBluetoothSocket::SocketError))); QObject::connect(m_socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), m_dbs, SLOT(socket_state(QBluetoothSocket::SocketState))); QObject::connect(m_socket, SIGNAL(readyRead()), m_dbs, SLOT(socket_readyRead())); - - m_stream = new QDataStream(m_socket); } QDeclarativeBluetoothSocket *m_dbs; @@ -129,8 +126,6 @@ public: QDeclarativeBluetoothSocket::SocketState m_state; bool m_componentCompleted; bool m_connected; - QDataStream *m_stream; - }; QDeclarativeBluetoothSocket::QDeclarativeBluetoothSocket(QObject *parent) : @@ -160,9 +155,6 @@ QDeclarativeBluetoothSocket::QDeclarativeBluetoothSocket(QBluetoothSocket *socke QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(socket_error(QBluetoothSocket::SocketError))); QObject::connect(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), this, SLOT(socket_state(QBluetoothSocket::SocketState))); QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(socket_readyRead())); - - d->m_stream = new QDataStream(socket); - } @@ -312,15 +304,6 @@ QDeclarativeBluetoothSocket::SocketState QDeclarativeBluetoothSocket::state() co return d->m_state; } -/*! - \qmlsignal BluetoothSocket::dataAvailable() - - This signal indicates the arrival of new data. It is emitted each time the socket has new - data available. The data can be read from the property stringData. - \sa stringData - \sa sendStringData - */ - void QDeclarativeBluetoothSocket::socket_readyRead() { emit dataAvailable(); @@ -329,13 +312,11 @@ void QDeclarativeBluetoothSocket::socket_readyRead() /*! \qmlproperty string BluetoothSocket::stringData - This property receives or sends data to remote Bluetooth device. Arrival of - data is signaled by the dataAvailable signal and can be read by - stringData. Calling sendStringData will transmit the string. + This property receives or sends data to a remote Bluetooth device. Arrival of + data can be detected by connecting to this properties changed signal and can be read via + stringData. Setting stringData will transmit the string. If excessive amounts of data are sent, the function may block sending. Reading will never block. - \sa dataAvailable - \sa sendStringData */ QString QDeclarativeBluetoothSocket::stringData() @@ -344,30 +325,28 @@ QString QDeclarativeBluetoothSocket::stringData() return QString(); QString data; - *d->m_stream >> data; + while (d->m_socket->canReadLine()) { + QByteArray line = d->m_socket->readLine(); + data += QString::fromUtf8(line.constData(), line.length()); + } + return data; } /*! - \qmlmethod BluetoothSocket::sendStringData(data) - This method transmits the string data passed with "data" to the remote device. If excessive amounts of data are sent, the function may block sending. - \sa dataAvailable - \sa stringData */ -void QDeclarativeBluetoothSocket::sendStringData(QString data) +void QDeclarativeBluetoothSocket::sendStringData(const QString &data) { if (!d->m_connected || !d->m_socket){ qWarning() << "Writing data to unconnected socket"; return; } - QByteArray b; - QDataStream s(&b, QIODevice::WriteOnly); - s << data; - d->m_socket->write(b); + QByteArray text = data.toUtf8() + '\n'; + d->m_socket->write(text); } void QDeclarativeBluetoothSocket::newSocket(QBluetoothSocket *socket, QDeclarativeBluetoothService *service) @@ -380,7 +359,7 @@ void QDeclarativeBluetoothSocket::newSocket(QBluetoothSocket *socket, QDeclarati d->m_socket = socket; d->m_connected = true; d->m_componentCompleted = true; - d->m_error = NoSocketerror; + d->m_error = NoError; QObject::connect(socket, SIGNAL(connected()), this, SLOT(socket_connected())); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(socket_disconnected())); @@ -388,8 +367,6 @@ void QDeclarativeBluetoothSocket::newSocket(QBluetoothSocket *socket, QDeclarati QObject::connect(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), this, SLOT(socket_state(QBluetoothSocket::SocketState))); QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(socket_readyRead())); - d->m_stream = new QDataStream(socket); - socket_state(socket->state()); emit connectedChanged(); |