diff options
Diffstat (limited to 'examples/bluetooth/btchat')
-rw-r--r-- | examples/bluetooth/btchat/chat.cpp | 50 | ||||
-rw-r--r-- | examples/bluetooth/btchat/chat.h | 6 | ||||
-rw-r--r-- | examples/bluetooth/btchat/chat.ui | 26 | ||||
-rw-r--r-- | examples/bluetooth/btchat/chatclient.cpp | 2 | ||||
-rw-r--r-- | examples/bluetooth/btchat/chatserver.cpp | 14 | ||||
-rw-r--r-- | examples/bluetooth/btchat/chatserver.h | 3 | ||||
-rw-r--r-- | examples/bluetooth/btchat/remoteselector.cpp | 17 | ||||
-rw-r--r-- | examples/bluetooth/btchat/remoteselector.h | 2 |
8 files changed, 95 insertions, 25 deletions
diff --git a/examples/bluetooth/btchat/chat.cpp b/examples/bluetooth/btchat/chat.cpp index 37bfb85c..f3df7aa1 100644 --- a/examples/bluetooth/btchat/chat.cpp +++ b/examples/bluetooth/btchat/chat.cpp @@ -56,7 +56,7 @@ static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8"); Chat::Chat(QWidget *parent) -: QDialog(parent), ui(new Ui_Chat) + : QDialog(parent), currentAdapterIndex(0), ui(new Ui_Chat) { //! [Construct UI] ui->setupUi(this); @@ -66,6 +66,22 @@ Chat::Chat(QWidget *parent) connect(ui->sendButton, SIGNAL(clicked()), this, SLOT(sendClicked())); //! [Construct UI] + localAdapters = QBluetoothLocalDevice::allDevices(); + if (localAdapters.count() < 2) { + ui->localAdapterBox->setVisible(false); + } else { + //we ignore more than two adapters + ui->localAdapterBox->setVisible(true); + ui->firstAdapter->setText(tr("Default (%1)", "%1 = Bluetooth address"). + arg(localAdapters.at(0).address().toString())); + ui->secondAdapter->setText(localAdapters.at(1).address().toString()); + ui->firstAdapter->setChecked(true); + connect(ui->firstAdapter, SIGNAL(clicked()), this, SLOT(newAdapterSelected())); + connect(ui->secondAdapter, SIGNAL(clicked()), this, SLOT(newAdapterSelected())); + QBluetoothLocalDevice adapter(localAdapters.at(0).address()); + adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable); + } + //! [Create Chat Server] server = new ChatServer(this); connect(server, SIGNAL(clientConnected(QString)), this, SLOT(clientConnected(QString))); @@ -104,6 +120,32 @@ void Chat::connected(const QString &name) { ui->chat->insertPlainText(QString::fromLatin1("Joined chat with %1.\n").arg(name)); } + +void Chat::newAdapterSelected() +{ + const int newAdapterIndex = adapterFromUserSelection(); + if (currentAdapterIndex != newAdapterIndex) { + server->stopServer(); + currentAdapterIndex = newAdapterIndex; + const QBluetoothHostInfo info = localAdapters.at(currentAdapterIndex); + QBluetoothLocalDevice adapter(info.address()); + adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable); + server->startServer(info.address()); + localName = info.name(); + } +} + +int Chat::adapterFromUserSelection() const +{ + int result = 0; + QBluetoothAddress newAdapter = localAdapters.at(0).address(); + + if (ui->secondAdapter->isChecked()) { + newAdapter = localAdapters.at(1).address(); + result = 1; + } + return result; +} //! [connected] //! [clientDisconnected] @@ -123,7 +165,11 @@ void Chat::connectClicked() ui->connectButton->setEnabled(false); // scan for services - RemoteSelector remoteSelector; + const QBluetoothAddress adapter = localAdapters.isEmpty() ? + QBluetoothAddress() : + localAdapters.at(currentAdapterIndex).address(); + + RemoteSelector remoteSelector(adapter); remoteSelector.startDiscovery(QBluetoothUuid(serviceUuid)); if (remoteSelector.exec() == QDialog::Accepted) { QBluetoothServiceInfo service = remoteSelector.service(); diff --git a/examples/bluetooth/btchat/chat.h b/examples/bluetooth/btchat/chat.h index fed850f1..75d7669f 100644 --- a/examples/bluetooth/btchat/chat.h +++ b/examples/bluetooth/btchat/chat.h @@ -44,6 +44,7 @@ #include <qbluetoothserviceinfo.h> #include <qbluetoothsocket.h> +#include <qbluetoothhostinfo.h> #include <QDebug> @@ -75,11 +76,16 @@ private slots: void clientDisconnected(); void connected(const QString &name); + void newAdapterSelected(); + private: + int adapterFromUserSelection() const; + int currentAdapterIndex; Ui_Chat *ui; ChatServer *server; QList<ChatClient *> clients; + QList<QBluetoothHostInfo> localAdapters; QString localName; }; diff --git a/examples/bluetooth/btchat/chat.ui b/examples/bluetooth/btchat/chat.ui index acebc937..d7829294 100644 --- a/examples/bluetooth/btchat/chat.ui +++ b/examples/bluetooth/btchat/chat.ui @@ -15,6 +15,32 @@ </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> + <widget class="QGroupBox" name="localAdapterBox"> + <property name="title"> + <string>Local Bluetooth Adapter</string> + </property> + <property name="checkable"> + <bool>false</bool> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_3"> + <item> + <widget class="QRadioButton" name="firstAdapter"> + <property name="text"> + <string>Default</string> + </property> + </widget> + </item> + <item> + <widget class="QRadioButton" name="secondAdapter"> + <property name="text"> + <string/> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> <widget class="QTextEdit" name="chat"> <property name="focusPolicy"> <enum>Qt::NoFocus</enum> diff --git a/examples/bluetooth/btchat/chatclient.cpp b/examples/bluetooth/btchat/chatclient.cpp index b6ba7c52..06930145 100644 --- a/examples/bluetooth/btchat/chatclient.cpp +++ b/examples/bluetooth/btchat/chatclient.cpp @@ -62,7 +62,7 @@ void ChatClient::startClient(const QBluetoothServiceInfo &remoteService) socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); qDebug() << "Create socket"; socket->connectToService(remoteService); - qDebug() << "ConnecttoService done"; + qDebug() << "ConnectToService done"; connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket())); connect(socket, SIGNAL(connected()), this, SLOT(connected())); diff --git a/examples/bluetooth/btchat/chatserver.cpp b/examples/bluetooth/btchat/chatserver.cpp index 863078fc..8b1bf6f0 100644 --- a/examples/bluetooth/btchat/chatserver.cpp +++ b/examples/bluetooth/btchat/chatserver.cpp @@ -42,6 +42,7 @@ #include <qbluetoothserver.h> #include <qbluetoothsocket.h> +#include <qbluetoothlocaldevice.h> //! [Service UUID] static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8"); @@ -57,7 +58,7 @@ ChatServer::~ChatServer() stopServer(); } -void ChatServer::startServer() +void ChatServer::startServer(const QBluetoothAddress& localAdapter) { if (rfcommServer) return; @@ -65,7 +66,11 @@ void ChatServer::startServer() //! [Create the server] rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected())); - rfcommServer->listen(); + bool result = rfcommServer->listen(localAdapter); + if (!result) { + qWarning() << "Cannot bind chat server to" << localAdapter.toString(); + return; + } //! [Create the server] serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010); @@ -81,7 +86,7 @@ void ChatServer::startServer() serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Bt Chat Server")); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription, tr("Example bluetooth chat server")); - serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("Nokia, QtDF")); + serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("qt-project.org")); //! [Service name, description and provider] //! [Service UUID set] @@ -107,7 +112,7 @@ void ChatServer::startServer() //! [Protocol descriptor list] //! [Register service] - serviceInfo.registerService(); + serviceInfo.registerService(localAdapter); //! [Register service] } @@ -146,7 +151,6 @@ void ChatServer::clientConnected() connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket())); connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); clientSockets.append(socket); - emit clientConnected(socket->peerName()); } //! [clientConnected] diff --git a/examples/bluetooth/btchat/chatserver.h b/examples/bluetooth/btchat/chatserver.h index 2a3c3503..8af21f55 100644 --- a/examples/bluetooth/btchat/chatserver.h +++ b/examples/bluetooth/btchat/chatserver.h @@ -42,6 +42,7 @@ #define CHATSERVER_H #include <qbluetoothserviceinfo.h> +#include <qbluetoothaddress.h> #include <QtCore/QObject> #include <QtCore/QList> @@ -60,7 +61,7 @@ public: explicit ChatServer(QObject *parent = 0); ~ChatServer(); - void startServer(); + void startServer(const QBluetoothAddress &localAdapter = QBluetoothAddress()); void stopServer(); public slots: diff --git a/examples/bluetooth/btchat/remoteselector.cpp b/examples/bluetooth/btchat/remoteselector.cpp index f660a815..73f56a54 100644 --- a/examples/bluetooth/btchat/remoteselector.cpp +++ b/examples/bluetooth/btchat/remoteselector.cpp @@ -47,25 +47,12 @@ QT_USE_NAMESPACE -RemoteSelector::RemoteSelector(QWidget *parent) +RemoteSelector::RemoteSelector(const QBluetoothAddress &localAdapter, QWidget *parent) : QDialog(parent), ui(new Ui::RemoteSelector) { ui->setupUi(this); - //Using default Bluetooth adapter - QBluetoothLocalDevice localDevice; - QBluetoothAddress adapterAddress = localDevice.address(); - - /* - * In case of multiple Bluetooth adapters it is possible to - * set which adapter will be used by providing MAC Address. - * Example code: - * - * QBluetoothAddress adapterAddress("XX:XX:XX:XX:XX:XX"); - * m_discoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress); - */ - - m_discoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress); + m_discoveryAgent = new QBluetoothServiceDiscoveryAgent(localAdapter); connect(m_discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(serviceDiscovered(QBluetoothServiceInfo))); diff --git a/examples/bluetooth/btchat/remoteselector.h b/examples/bluetooth/btchat/remoteselector.h index 3f6e15c9..5b123127 100644 --- a/examples/bluetooth/btchat/remoteselector.h +++ b/examples/bluetooth/btchat/remoteselector.h @@ -63,7 +63,7 @@ class RemoteSelector : public QDialog Q_OBJECT public: - explicit RemoteSelector(QWidget *parent = 0); + explicit RemoteSelector(const QBluetoothAddress &localAdapter, QWidget *parent = 0); ~RemoteSelector(); void startDiscovery(const QBluetoothUuid &uuid); |