diff options
Diffstat (limited to 'src/bluetooth')
-rw-r--r-- | src/bluetooth/qbluetoothserver.cpp | 18 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothserver_bluez.cpp | 27 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothserver_qnx.cpp | 2 |
3 files changed, 39 insertions, 8 deletions
diff --git a/src/bluetooth/qbluetoothserver.cpp b/src/bluetooth/qbluetoothserver.cpp index 967d4e71..e70ec1db 100644 --- a/src/bluetooth/qbluetoothserver.cpp +++ b/src/bluetooth/qbluetoothserver.cpp @@ -112,8 +112,11 @@ QT_BEGIN_NAMESPACE Start listening for incoming connections to \a address on \a port. - Returns true if the operation succeeded and the server is listening for - incoming connections, otherwise returns false. + Returns \c true if the operation succeeded and the server is listening for + incoming connections, otherwise returns \c false. + + If the server object is already listening for incoming connections this function + always returns \c false. \l close() should be called before calling this function. \sa isListening(), newConnection() */ @@ -172,12 +175,17 @@ QBluetoothServer::~QBluetoothServer() Convenience function for registering an SPP service with \a uuid and \a serviceName. Because this function already registers the service, the QBluetoothServiceInfo object - which is returned can not be changed any more. + which is returned can not be changed any more. To shutdown the server later on it is + required to call \l QBluetoothServiceInfo::unregisterService() and \l close() on this + server object. Returns a registered QBluetoothServiceInfo instance if sucessful otherwise an invalid QBluetoothServiceInfo. This function always assumes that the default Bluetooth adapter should be used. + If the server object is already listening for incoming connections this function + returns an invalid \l QBluetoothServiceInfo. + For an RFCOMM server this function is equivalent to following code snippet. \snippet qbluetoothserver.cpp listen @@ -223,8 +231,10 @@ QBluetoothServiceInfo QBluetoothServer::listen(const QBluetoothUuid &uuid, const protocolDescriptorList); bool result = serviceInfo.registerService(); //! [listen3] - if (!result) + if (!result) { + close(); //close the still listening socket return QBluetoothServiceInfo(); + } return serviceInfo; } diff --git a/src/bluetooth/qbluetoothserver_bluez.cpp b/src/bluetooth/qbluetoothserver_bluez.cpp index 0b504d9a..8acd8e20 100644 --- a/src/bluetooth/qbluetoothserver_bluez.cpp +++ b/src/bluetooth/qbluetoothserver_bluez.cpp @@ -105,11 +105,32 @@ bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) { Q_D(QBluetoothServer); + if (d->socket->state() == QBluetoothSocket::ListeningState) { + qCWarning(QT_BT_BLUEZ) << "Socket already in listen mode, close server first"; + return false; //already listening, nothing to do + } + int sock = d->socket->socketDescriptor(); if (sock < 0) { - d->m_lastError = InputOutputError; - emit error(d->m_lastError); - return false; + /* Negative socket descriptor is not always an error case + * Another cause could be a call to close()/abort() + * Check whether we can recover by re-creating the socket + * we should really call Bluez::QBluetoothSocketPrivate::ensureNativeSocket + * but a re-creation of the socket will do as well. + */ + + delete d->socket; + if (serverType() == QBluetoothServiceInfo::RfcommProtocol) + d->socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); + else + d->socket = new QBluetoothSocket(QBluetoothServiceInfo::L2capProtocol); + + sock = d->socket->socketDescriptor(); + if (sock < 0) { + d->m_lastError = InputOutputError; + emit error(d->m_lastError); + return false; + } } if (d->serverType == QBluetoothServiceInfo::RfcommProtocol) { diff --git a/src/bluetooth/qbluetoothserver_qnx.cpp b/src/bluetooth/qbluetoothserver_qnx.cpp index 9a16694f..d4289368 100644 --- a/src/bluetooth/qbluetoothserver_qnx.cpp +++ b/src/bluetooth/qbluetoothserver_qnx.cpp @@ -156,7 +156,7 @@ bool QBluetoothServer::listen(const QBluetoothAddress &address, quint16 port) // listen has already been called before if (d->socket && d->socket->state() == QBluetoothSocket::ListeningState) - return true; + return false; d->socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); |