QBluetoothServer Class

The QBluetoothServer class uses the RFCOMM or L2cap protocol to communicate with a Bluetooth device. 更多...

頭: #include <QBluetoothServer>
qmake: QT += bluetooth
Since: Qt 5.2
繼承: QObject

公共類型

enum Error { NoError, UnknownError, PoweredOffError, InputOutputError, ServiceAlreadyRegisteredError, UnsupportedProtocolError }

公共函數

QBluetoothServer (QBluetoothServiceInfo::Protocol serverType , QObject * parent = nullptr)
virtual ~QBluetoothServer ()
void close ()
QBluetoothServer::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 )

信號

void error (QBluetoothServer::Error error )
void newConnection ()

靜態公共成員

const QMetaObject staticMetaObject

保護變量

QBluetoothServerPrivate * d_ptr

額外繼承成員

詳細描述

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 ()。調用 serverPort () to get the channel number that is being used.

QBluetoothServiceInfo::Protocol is not supported by a platform, listen () 會返迴 false . Android and WinRT only support RFCOMM for example.

On iOS, this class cannot be used because the platform does not expose an API which may permit access to QBluetoothServer related features.

另請參閱 QBluetoothServiceInfo and QBluetoothSocket .

成員類型文檔編製

enum QBluetoothServer:: Error

This enum describes Bluetooth server error types.

常量 描述
QBluetoothServer::NoError 0 沒有錯誤。
QBluetoothServer::UnknownError 1 齣現未知錯誤。
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.

成員函數文檔編製

QBluetoothServer:: QBluetoothServer ( QBluetoothServiceInfo::Protocol serverType , QObject * parent = nullptr)

Constructs a bluetooth server with parent and serverType .

[虛擬] QBluetoothServer:: ~QBluetoothServer ()

Destroys the bluetooth server.

void QBluetoothServer:: close ()

Closes and resets the listening socket. Any already established QBluetoothSocket continues to operate and must be separately closed .

QBluetoothServer::Error QBluetoothServer:: error () const

Returns the last error of the QBluetoothServer .

[signal] void QBluetoothServer:: error ( QBluetoothServer::Error error )

此信號發射,當 error 齣現。

注意: 信號 error 在此類中被重載。通過使用函數指針句法連接到此信號,Qt 提供用於獲得如此範例展示的函數指針的方便幫助程序:

connect(bluetoothServer, QOverload<QBluetoothServer::Error>::of(&QBluetoothServer::error),
    [=](QBluetoothServer::Error error){ /* ... */ });
					

另請參閱 error () 和 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 . address must be a local Bluetooth adapter address and port must be larger than zero and not be taken already by another Bluetooth server object. It is recommended to avoid setting a port number to enable the system to automatically choose a port.

返迴 true if the operation succeeded and the server is listening for incoming connections, otherwise returns false .

If the server object is already listening for incoming connections this function always returns false . close () should be called before calling this function.

另請參閱 isListening () 和 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. To shutdown the server later on it is required to call QBluetoothServiceInfo::unregisterService () 和 close () on this server object.

Returns a registered QBluetoothServiceInfo instance if successful 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 QBluetoothServiceInfo .

For an RFCOMM server this function is equivalent to following code snippet.

    QBluetoothServiceInfo serviceInfo;
    serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, serviceName);
    QBluetoothServiceInfo::Sequence browseSequence;
    browseSequence << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
    serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
                             browseSequence);
    QBluetoothServiceInfo::Sequence profileSequence;
    QBluetoothServiceInfo::Sequence classId;
    classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
    classId << QVariant::fromValue(quint16(0x100));
    profileSequence.append(QVariant::fromValue(classId));
    serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,
                             profileSequence);
    classId.clear();
    //Android requires custom uuid to be set as service class
    classId << QVariant::fromValue(uuid);
    classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
    serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
    serviceInfo.setServiceUuid(uuid);
    QBluetoothServiceInfo::Sequence protocolDescriptorList;
    QBluetoothServiceInfo::Sequence protocol;
    protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap));
    if (d->serverType == QBluetoothServiceInfo::L2capProtocol)
        protocol << QVariant::fromValue(serverPort());
    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();
					

另請參閱 isListening (), newConnection (),和 listen ().

int QBluetoothServer:: maxPendingConnections () const

Returns the maximum number of pending connections.

另請參閱 setMaxPendingConnections ().

[signal] void QBluetoothServer:: newConnection ()

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.

另請參閱 nextPendingConnection () 和 hasPendingConnections ().

QBluetoothSocket *QBluetoothServer:: nextPendingConnection ()

返迴指針指嚮 QBluetoothSocket for the next pending connection. It is the callers responsibility to delete the pointer.

QBluetooth::SecurityFlags QBluetoothServer:: securityFlags () const

Returns the Bluetooth security flags.

另請參閱 setSecurityFlags ().

QBluetoothAddress QBluetoothServer:: serverAddress () const

Returns the server address.

quint16 QBluetoothServer:: serverPort () const

Returns the server port number.

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 . If the number of pending sockets exceeds this limit new sockets will be rejected.

另請參閱 maxPendingConnections ().

void QBluetoothServer:: setSecurityFlags ( QBluetooth::SecurityFlags security )

Sets the Bluetooth security flags to security . This function must be called before calling listen (). The Bluetooth link will always be encrypted when using Bluetooth 2.1 devices as encryption is mandatory.

Android only supports two levels of security (secure and non-secure). If this flag is set to QBluetooth::NoSecurity the server object will not employ any authentication or encryption. Any other security flag combination will trigger a secure Bluetooth connection.

On macOS , security flags are not supported and will be ignored.

另請參閱 securityFlags ().