QBluetoothServer Class
The QBluetoothServer class uses the RFCOMM or L2cap protocol to communicate with a Bluetooth device. More...
Header: | #include <QBluetoothServer> |
qmake: | QT += bluetooth |
Inherits: | QObject. |
Public Types
enum | Error { NoError, UnknownError, PoweredOffError, InputOutputError, ServiceAlreadyRegisteredError, UnsupportedProtocolError } |
Public Functions
QBluetoothServer(QBluetoothServiceInfo::Protocol serverType, QObject * parent = 0) | |
~QBluetoothServer() | |
void | close() |
Error | error() const |
bool | hasPendingConnections() const |
bool | isListening() const |
bool | listen(const QBluetoothAddress & address = QBluetoothAddress(), quint16 port = 0) |
QBluetoothServiceInfo | listen(const QBluetoothUuid & uuid, const QString & serviceName = QString()) |
int | maxPendingConnections() const |
QBluetoothSocket * | nextPendingConnection() |
QBluetooth::SecurityFlags | securityFlags() const |
QBluetoothAddress | serverAddress() const |
quint16 | serverPort() const |
QBluetoothServiceInfo::Protocol | serverType() const |
void | setMaxPendingConnections(int numConnections) |
void | setSecurityFlags(QBluetooth::SecurityFlags security) |
- 31 public functions inherited from QObject
Signals
void | error(Error error) |
void | newConnection() |
- 2 signals inherited from QObject
Additional Inherited Members
- 1 property inherited from QObject
- 1 public slot inherited from QObject
- 1 public variable inherited from QObject
- 10 static public members inherited from QObject
- 9 protected functions inherited from QObject
- 2 protected variables inherited from QObject
Detailed Description
The QBluetoothServer class uses the RFCOMM or L2cap protocol to communicate with a Bluetooth device.
QBluetoothServer is used to implement Bluetooth services over RFCOMM or L2cap.
Start listening for incoming connections with listen(). Wait till the newConnection() signal is emitted when a new connection is established, and call nextPendingConnection() to get a QBluetoothSocket for the new connection.
To enable other devices to find your service, create a QBluetoothServiceInfo with the applicable attributes for your service and register it using QBluetoothServiceInfo::registerService(). Call serverPort() to get the channel number that is being used.
If the QBluetoothServiceInfo::Protocol is not supported by a platform, listen() will return false.
See also QBluetoothServiceInfo and QBluetoothSocket.
Member Type Documentation
enum QBluetoothServer::Error
This enum describes Bluetooth server error types.
Constant | Value | Description |
---|---|---|
QBluetoothServer::NoError | 0 | No error. |
QBluetoothServer::UnknownError | 1 | An unknown error occurred. |
QBluetoothServer::PoweredOffError | 2 | The Bluetooth adapter is powered off. |
QBluetoothServer::InputOutputError | 3 | An input output error occurred. |
QBluetoothServer::ServiceAlreadyRegisteredError | 4 | The service or port was already registered |
QBluetoothServer::UnsupportedProtocolError | 5 | The Protocol is not supported on this platform. |
Member Function Documentation
QBluetoothServer::QBluetoothServer(QBluetoothServiceInfo::Protocol serverType, QObject * parent = 0)
Constructs a bluetooth server with parent and serverType.
QBluetoothServer::~QBluetoothServer()
Destroys the bluetooth server.
void QBluetoothServer::close()
Closes and resets the listening socket.
Error QBluetoothServer::error() const
Returns the last error of the QBluetoothServer.
void QBluetoothServer::error(Error error) [signal]
This signal is emitted when an error occurs.
See also error() and QBluetoothServer::Error.
bool QBluetoothServer::hasPendingConnections() const
Returns true if a connection is pending, otherwise false.
bool QBluetoothServer::isListening() const
Returns true if the server is listening for incoming connections, otherwise false.
bool QBluetoothServer::listen(const QBluetoothAddress & address = QBluetoothAddress(), quint16 port = 0)
Start listening for incoming connections to address on port.
Returns true if the operation succeeded and the server is listening for incoming connections, otherwise returns false.
See also isListening() and newConnection().
QBluetoothServiceInfo QBluetoothServer::listen(const QBluetoothUuid & uuid, const QString & serviceName = QString())
Convenience function for registering an SPP service with uuid and serviceName. Because this function already registers the service, the QBluetoothServiceInfo object which is returned can not be changed any more.
Returns a registered QBluetoothServiceInfo instance if sucessful otherwise an invalid QBluetoothServiceInfo. This function always assumes that the default Bluetooth adapter should be used.
For an RFCOMM server this function is equivalent to following code snippet.
QBluetoothServiceInfo serviceInfo; serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, serviceName); serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); QBluetoothServiceInfo::Sequence classId; classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, classId); serviceInfo.setServiceUuid(uuid); QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)); protocolDescriptorList.append(QVariant::fromValue(protocol)); protocol.clear(); protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) << QVariant::fromValue(quint8(serverPort())); protocolDescriptorList.append(QVariant::fromValue(protocol)); serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); bool result = serviceInfo.registerService();
See also isListening(), newConnection(), and listen().
int QBluetoothServer::maxPendingConnections() const
Returns the maximum number of pending connections.
See also setMaxPendingConnections().
void QBluetoothServer::newConnection() [signal]
This signal is emitted when a new connection is available.
The connected slot should call nextPendingConnection() to get a QBluetoothSocket object to send and receive data over the connection.
See also nextPendingConnection() and hasPendingConnections().
QBluetoothSocket * QBluetoothServer::nextPendingConnection()
Returns a pointer to aQBluetoothSocket for the next pending connection. It is the callers responsibility to delete the pointer.
QBluetooth::SecurityFlags QBluetoothServer::securityFlags() const
Returns the Bluetooth security flags.
See also setSecurityFlags().
QBluetoothAddress QBluetoothServer::serverAddress() const
Returns the server address.
quint16 QBluetoothServer::serverPort() const
Returns the server port number. On BlackBerry, this port might not correspond to the real RFCOMM port.
QBluetoothServiceInfo::Protocol QBluetoothServer::serverType() const
Returns the type of the QBluetoothServer.
void QBluetoothServer::setMaxPendingConnections(int numConnections)
Sets the maximum number of pending connections to numConnections.
See also maxPendingConnections().
void QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security)
Sets the Bluetooth security flags to security. This function must be called before calling listen(). On BlackBerry, security flags are not supported and will be ignored.
See also securityFlags().