summaryrefslogtreecommitdiffstats
path: root/examples/bluetooth/btchat
diff options
context:
space:
mode:
authorJerome Pasion <jerome.pasion@digia.com>2012-09-25 15:20:41 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-09-27 14:43:52 +0200
commitd8a179128c0fa8e9cc482df58b9334c696602be6 (patch)
treedd5032440aa87dfd63c973265adc9947db0918f6 /examples/bluetooth/btchat
parent9d89c661395347bdda9362a77d38c86ad60f486b (diff)
Qt Bluetooth: Modularized documentation
-moved documentation to src/bluetooth/doc -added a qdocconf file for Qt Bluetooth -fixed relative paths for snippets -moved examples to examples/bluetooth Change-Id: Id41bac50dca628400568d191f1c3ccfbaac790a1 Reviewed-by: Alex <ablasche@gmail.com>
Diffstat (limited to 'examples/bluetooth/btchat')
-rw-r--r--examples/bluetooth/btchat/btchat.pro21
-rw-r--r--examples/bluetooth/btchat/chat.cpp179
-rw-r--r--examples/bluetooth/btchat/chat.h90
-rw-r--r--examples/bluetooth/btchat/chat.ui76
-rw-r--r--examples/bluetooth/btchat/chatclient.cpp108
-rw-r--r--examples/bluetooth/btchat/chatclient.h83
-rw-r--r--examples/bluetooth/btchat/chatserver.cpp182
-rw-r--r--examples/bluetooth/btchat/chatserver.h88
-rw-r--r--examples/bluetooth/btchat/doc/images/btchat-example.pngbin0 -> 6527 bytes
-rw-r--r--examples/bluetooth/btchat/doc/src/btchat.qdoc191
-rw-r--r--examples/bluetooth/btchat/main.cpp62
-rw-r--r--examples/bluetooth/btchat/remoteselector.cpp146
-rw-r--r--examples/bluetooth/btchat/remoteselector.h87
-rw-r--r--examples/bluetooth/btchat/remoteselector.ui38
14 files changed, 1351 insertions, 0 deletions
diff --git a/examples/bluetooth/btchat/btchat.pro b/examples/bluetooth/btchat/btchat.pro
new file mode 100644
index 00000000..f215cb87
--- /dev/null
+++ b/examples/bluetooth/btchat/btchat.pro
@@ -0,0 +1,21 @@
+TEMPLATE = app
+TARGET = btchat
+
+QT += concurrent bluetooth widgets
+
+SOURCES = \
+ main.cpp \
+ chat.cpp \
+ remoteselector.cpp \
+ chatserver.cpp \
+ chatclient.cpp
+
+HEADERS = \
+ chat.h \
+ remoteselector.h \
+ chatserver.h \
+ chatclient.h
+
+FORMS = \
+ chat.ui \
+ remoteselector.ui
diff --git a/examples/bluetooth/btchat/chat.cpp b/examples/bluetooth/btchat/chat.cpp
new file mode 100644
index 00000000..7047cbdd
--- /dev/null
+++ b/examples/bluetooth/btchat/chat.cpp
@@ -0,0 +1,179 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtBluetooth module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "chat.h"
+#include "remoteselector.h"
+#include "chatserver.h"
+#include "chatclient.h"
+
+#include <qbluetoothuuid.h>
+#include <qrfcommserver.h>
+#include <qbluetoothservicediscoveryagent.h>
+#include <qbluetoothdeviceinfo.h>
+#include <qbluetoothlocaldevice.h>
+
+#include <QTimer>
+
+#include <QDebug>
+
+static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
+
+Chat::Chat(QWidget *parent)
+: QDialog(parent), ui(new Ui_Chat)
+{
+ //! [Construct UI]
+ ui->setupUi(this);
+
+#if defined(Q_OS_WINCE) || defined(Q_WS_MAEMO_6)
+ setWindowState(Qt::WindowFullScreen);
+#endif
+
+ connect(ui->quitButton, SIGNAL(clicked()), this, SLOT(accept()));
+ connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(connectClicked()));
+ connect(ui->sendButton, SIGNAL(clicked()), this, SLOT(sendClicked()));
+ //! [Construct UI]
+
+ //! [Create Chat Server]
+ server = new ChatServer(this);
+ connect(server, SIGNAL(clientConnected(QString)), this, SLOT(clientConnected(QString)));
+ connect(server, SIGNAL(clientDisconnected(QString)), this, SLOT(clientDisconnected(QString)));
+ connect(server, SIGNAL(messageReceived(QString,QString)),
+ this, SLOT(showMessage(QString,QString)));
+ connect(this, SIGNAL(sendMessage(QString)), server, SLOT(sendMessage(QString)));
+ server->startServer();
+ //! [Create Chat Server]
+
+ //! [Get local device name]
+ localName = QBluetoothLocalDevice().name();
+ //! [Get local device name]
+}
+
+Chat::~Chat()
+{
+ qDeleteAll(clients);
+ delete server;
+}
+
+//! [clientConnected clientDisconnected]
+void Chat::clientConnected(const QString &name)
+{
+ ui->chat->insertPlainText(QString::fromLatin1("%1 has joined chat.\n").arg(name));
+}
+
+void Chat::clientDisconnected(const QString &name)
+{
+ ui->chat->insertPlainText(QString::fromLatin1("%1 has left.\n").arg(name));
+}
+//! [clientConnected clientDisconnected]
+
+//! [connected]
+void Chat::connected(const QString &name)
+{
+ ui->chat->insertPlainText(QString::fromLatin1("Joined chat with %1.\n").arg(name));
+}
+//! [connected]
+
+//! [clientDisconnected]
+void Chat::clientDisconnected()
+{
+ ChatClient *client = qobject_cast<ChatClient *>(sender());
+ if (client) {
+ clients.removeOne(client);
+ client->deleteLater();
+ }
+}
+//! [clientDisconnected]
+
+//! [Connect to remote service]
+void Chat::connectClicked()
+{
+ ui->connectButton->setEnabled(false);
+
+ // scan for services
+ RemoteSelector remoteSelector;
+ remoteSelector.startDiscovery(QBluetoothUuid(serviceUuid));
+ if (remoteSelector.exec() == QDialog::Accepted) {
+ QBluetoothServiceInfo service = remoteSelector.service();
+
+ qDebug() << "Connecting to service 2" << service.serviceName()
+ << "on" << service.device().name();
+
+ // Create client
+ qDebug() << "Going to create client";
+ ChatClient *client = new ChatClient(this);
+qDebug() << "Connecting...";
+
+ connect(client, SIGNAL(messageReceived(QString,QString)),
+ this, SLOT(showMessage(QString,QString)));
+ connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
+ connect(client, SIGNAL(connected(QString)), this, SLOT(connected(QString)));
+ connect(this, SIGNAL(sendMessage(QString)), client, SLOT(sendMessage(QString)));
+qDebug() << "Start client";
+ client->startClient(service);
+
+ clients.append(client);
+ }
+
+ ui->connectButton->setEnabled(true);
+}
+//! [Connect to remote service]
+
+//! [sendClicked]
+void Chat::sendClicked()
+{
+ ui->sendButton->setEnabled(false);
+ ui->sendText->setEnabled(false);
+
+ showMessage(localName, ui->sendText->text());
+ emit sendMessage(ui->sendText->text());
+
+ ui->sendText->clear();
+
+ ui->sendText->setEnabled(true);
+ ui->sendButton->setEnabled(true);
+}
+//! [sendClicked]
+
+//! [showMessage]
+void Chat::showMessage(const QString &sender, const QString &message)
+{
+ ui->chat->insertPlainText(QString::fromLatin1("%1: %2\n").arg(sender, message));
+}
+//! [showMessage]
diff --git a/examples/bluetooth/btchat/chat.h b/examples/bluetooth/btchat/chat.h
new file mode 100644
index 00000000..740999d0
--- /dev/null
+++ b/examples/bluetooth/btchat/chat.h
@@ -0,0 +1,90 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtBluetooth module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "ui_chat.h"
+
+#include <QDialog>
+
+#include <qbluetoothserviceinfo.h>
+#include <qbluetoothsocket.h>
+
+#include <QDebug>
+
+QTBLUETOOTH_BEGIN_NAMESPACE
+class QRfcommServer;
+QTBLUETOOTH_END_NAMESPACE
+
+QTBLUETOOTH_USE_NAMESPACE
+
+class ChatServer;
+class ChatClient;
+
+//! [declaration]
+class Chat : public QDialog
+{
+ Q_OBJECT
+
+public:
+ Chat(QWidget *parent = 0);
+ ~Chat();
+
+signals:
+ void sendMessage(const QString &message);
+
+private slots:
+ void connectClicked();
+ void sendClicked();
+
+ void showMessage(const QString &sender, const QString &message);
+
+ void clientConnected(const QString &name);
+ void clientDisconnected(const QString &name);
+ void clientDisconnected();
+ void connected(const QString &name);
+
+private:
+ Ui_Chat *ui;
+
+ ChatServer *server;
+ QList<ChatClient *> clients;
+
+ QString localName;
+};
+//! [declaration]
diff --git a/examples/bluetooth/btchat/chat.ui b/examples/bluetooth/btchat/chat.ui
new file mode 100644
index 00000000..acebc937
--- /dev/null
+++ b/examples/bluetooth/btchat/chat.ui
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Chat</class>
+ <widget class="QDialog" name="Chat">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>400</width>
+ <height>300</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>Bluetooth Chat</string>
+ </property>
+ <layout class="QVBoxLayout" name="verticalLayout">
+ <item>
+ <widget class="QTextEdit" name="chat">
+ <property name="focusPolicy">
+ <enum>Qt::NoFocus</enum>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout">
+ <item>
+ <widget class="QLineEdit" name="sendText"/>
+ </item>
+ <item>
+ <widget class="QPushButton" name="sendButton">
+ <property name="text">
+ <string>Send</string>
+ </property>
+ <property name="default">
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_2">
+ <item>
+ <spacer name="horizontalSpacer">
+ <property name="orientation">
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="sizeHint" stdset="0">
+ <size>
+ <width>40</width>
+ <height>20</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ <item>
+ <widget class="QPushButton" name="connectButton">
+ <property name="text">
+ <string>Connect</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ <item>
+ <widget class="QPushButton" name="quitButton">
+ <property name="text">
+ <string>Quit</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/examples/bluetooth/btchat/chatclient.cpp b/examples/bluetooth/btchat/chatclient.cpp
new file mode 100644
index 00000000..8ea66e51
--- /dev/null
+++ b/examples/bluetooth/btchat/chatclient.cpp
@@ -0,0 +1,108 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtBluetooth module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "chatclient.h"
+
+#include <qbluetoothsocket.h>
+
+ChatClient::ChatClient(QObject *parent)
+: QObject(parent), socket(0)
+{
+}
+
+ChatClient::~ChatClient()
+{
+ stopClient();
+}
+
+//! [startClient]
+void ChatClient::startClient(const QBluetoothServiceInfo &remoteService)
+{
+ if (socket)
+ return;
+
+ // Connect to service
+ socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
+ qDebug() << "Create socket";
+ socket->connectToService(remoteService);
+ qDebug() << "ConnecttoService done";
+
+ connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
+ connect(socket, SIGNAL(connected()), this, SLOT(connected()));
+ connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
+}
+//! [startClient]
+
+//! [stopClient]
+void ChatClient::stopClient()
+{
+ delete socket;
+ socket = 0;
+}
+//! [stopClient]
+
+//! [readSocket]
+void ChatClient::readSocket()
+{
+ if (!socket)
+ return;
+
+ while (socket->canReadLine()) {
+ QByteArray line = socket->readLine();
+ emit messageReceived(socket->peerName(),
+ QString::fromUtf8(line.constData(), line.length()));
+ }
+}
+//! [readSocket]
+
+//! [sendMessage]
+void ChatClient::sendMessage(const QString &message)
+{
+ QByteArray text = message.toUtf8() + '\n';
+ socket->write(text);
+}
+//! [sendMessage]
+
+//! [connected]
+void ChatClient::connected()
+{
+ emit connected(socket->peerName());
+}
+//! [connected]
diff --git a/examples/bluetooth/btchat/chatclient.h b/examples/bluetooth/btchat/chatclient.h
new file mode 100644
index 00000000..710634e5
--- /dev/null
+++ b/examples/bluetooth/btchat/chatclient.h
@@ -0,0 +1,83 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtBluetooth module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef CHATCLIENT_H
+#define CHATCLIENT_H
+
+#include <qbluetoothserviceinfo.h>
+
+#include <QtCore/QObject>
+
+QTBLUETOOTH_BEGIN_NAMESPACE
+class QBluetoothSocket;
+QTBLUETOOTH_END_NAMESPACE
+
+QTBLUETOOTH_USE_NAMESPACE
+
+//! [declaration]
+class ChatClient : public QObject
+{
+ Q_OBJECT
+
+public:
+ explicit ChatClient(QObject *parent = 0);
+ ~ChatClient();
+
+ void startClient(const QBluetoothServiceInfo &remoteService);
+ void stopClient();
+
+public slots:
+ void sendMessage(const QString &message);
+
+signals:
+ void messageReceived(const QString &sender, const QString &message);
+ void connected(const QString &name);
+ void disconnected();
+
+private slots:
+ void readSocket();
+ void connected();
+
+private:
+ QBluetoothSocket *socket;
+};
+//! [declaration]
+
+#endif // CHATCLIENT_H
diff --git a/examples/bluetooth/btchat/chatserver.cpp b/examples/bluetooth/btchat/chatserver.cpp
new file mode 100644
index 00000000..545a0b03
--- /dev/null
+++ b/examples/bluetooth/btchat/chatserver.cpp
@@ -0,0 +1,182 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtBluetooth module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "chatserver.h"
+
+#include <qrfcommserver.h>
+#include <qbluetoothsocket.h>
+
+//! [Service UUID]
+static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
+//! [Service UUID]
+
+ChatServer::ChatServer(QObject *parent)
+: QObject(parent), rfcommServer(0)
+{
+}
+
+ChatServer::~ChatServer()
+{
+ stopServer();
+}
+
+void ChatServer::startServer()
+{
+ if (rfcommServer)
+ return;
+
+ //! [Create the server]
+ rfcommServer = new QRfcommServer(this);
+ connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));
+ rfcommServer->listen();
+ //! [Create the server]
+
+ serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010);
+
+ //! [Class Uuuid must contain at least 1 entry]
+ QBluetoothServiceInfo::Sequence classId;
+ classId << QVariant::fromValue(QBluetoothUuid(serviceUuid));
+ serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
+ //! [Class Uuuid must contain at least 1 entry]
+
+
+ //! [Service name, description and provider]
+ serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Bt Chat Server"));
+ serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription,
+ tr("Example bluetooth chat server"));
+ serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("Nokia, QtDF"));
+ //! [Service name, description and provider]
+
+ //! [Service UUID set]
+ serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid));
+ //! [Service UUID set]
+
+ //! [Service Discoverability]
+ serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
+ QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
+ //! [Service Discoverability]
+
+ //! [Protocol descriptor list]
+ 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(rfcommServer->serverPort()));
+ protocolDescriptorList.append(QVariant::fromValue(protocol));
+ serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
+ protocolDescriptorList);
+ //! [Protocol descriptor list]
+
+ //! [Register service]
+ serviceInfo.registerService();
+ //! [Register service]
+}
+
+//! [stopServer]
+void ChatServer::stopServer()
+{
+ // Unregister service
+ serviceInfo.unregisterService();
+
+ // Close sockets
+ qDeleteAll(clientSockets);
+
+ // Close server
+ delete rfcommServer;
+ rfcommServer = 0;
+}
+//! [stopServer]
+
+//! [sendMessage]
+void ChatServer::sendMessage(const QString &message)
+{
+ QByteArray text = message.toUtf8() + '\n';
+
+ foreach (QBluetoothSocket *socket, clientSockets)
+ socket->write(text);
+}
+//! [sendMessage]
+
+//! [clientConnected]
+void ChatServer::clientConnected()
+{
+ QBluetoothSocket *socket = rfcommServer->nextPendingConnection();
+ if (!socket)
+ return;
+
+ connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
+ connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
+ clientSockets.append(socket);
+
+ emit clientConnected(socket->peerName());
+}
+//! [clientConnected]
+
+//! [clientDisconnected]
+void ChatServer::clientDisconnected()
+{
+ QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
+ if (!socket)
+ return;
+
+ emit clientDisconnected(socket->peerName());
+
+ clientSockets.removeOne(socket);
+
+ socket->deleteLater();
+}
+//! [clientDisconnected]
+
+//! [readSocket]
+void ChatServer::readSocket()
+{
+ QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
+ if (!socket)
+ return;
+
+ while (socket->canReadLine()) {
+ QByteArray line = socket->readLine().trimmed();
+ emit messageReceived(socket->peerName(),
+ QString::fromUtf8(line.constData(), line.length()));
+ }
+}
+//! [readSocket]
diff --git a/examples/bluetooth/btchat/chatserver.h b/examples/bluetooth/btchat/chatserver.h
new file mode 100644
index 00000000..ee4728a9
--- /dev/null
+++ b/examples/bluetooth/btchat/chatserver.h
@@ -0,0 +1,88 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtBluetooth module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef CHATSERVER_H
+#define CHATSERVER_H
+
+#include <qbluetoothserviceinfo.h>
+
+#include <QtCore/QObject>
+#include <QtCore/QList>
+
+QTBLUETOOTH_BEGIN_NAMESPACE
+class QRfcommServer;
+class QBluetoothSocket;
+QTBLUETOOTH_END_NAMESPACE
+
+QTBLUETOOTH_USE_NAMESPACE
+
+//! [declaration]
+class ChatServer : public QObject
+{
+ Q_OBJECT
+
+public:
+ explicit ChatServer(QObject *parent = 0);
+ ~ChatServer();
+
+ void startServer();
+ void stopServer();
+
+public slots:
+ void sendMessage(const QString &message);
+
+signals:
+ void messageReceived(const QString &sender, const QString &message);
+ void clientConnected(const QString &name);
+ void clientDisconnected(const QString &name);
+
+private slots:
+ void clientConnected();
+ void clientDisconnected();
+ void readSocket();
+
+private:
+ QRfcommServer *rfcommServer;
+ QBluetoothServiceInfo serviceInfo;
+ QList<QBluetoothSocket *> clientSockets;
+};
+//! [declaration]
+
+#endif // CHATSERVER_H
diff --git a/examples/bluetooth/btchat/doc/images/btchat-example.png b/examples/bluetooth/btchat/doc/images/btchat-example.png
new file mode 100644
index 00000000..762d49cb
--- /dev/null
+++ b/examples/bluetooth/btchat/doc/images/btchat-example.png
Binary files differ
diff --git a/examples/bluetooth/btchat/doc/src/btchat.qdoc b/examples/bluetooth/btchat/doc/src/btchat.qdoc
new file mode 100644
index 00000000..20f1ad93
--- /dev/null
+++ b/examples/bluetooth/btchat/doc/src/btchat.qdoc
@@ -0,0 +1,191 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the documentation of the Qt local connectivty modules.
+**
+** $QT_BEGIN_LICENSE:FDL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Free Documentation License Usage
+** Alternatively, this file may be used under the terms of the GNU Free
+** Documentation License version 1.3 as published by the Free Software
+** Foundation and appearing in the file included in the packaging of
+** this file. Please review the following information to ensure
+** the GNU Free Documentation License version 1.3 requirements
+** will be met: http://www.gnu.org/copyleft/fdl.html.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+/*!
+ \example btchat
+ \title Bluetooth Chat Example
+
+ The Bluetooth Chat example shows how to use the Qt Connectivity Bluetooth API to communicate
+ with another application on a remote device using Bluetooth.
+
+ \image btchat-example.png
+
+ The Bluetooth Chat example implements a simple chat program between multiple parties. The
+ application always acts as both a server and a client eliminating the need to determine who
+ should connect to whom.
+
+ \section1 Chat Server
+
+ The chat server is implemented by the ChatServer class. The ChatServer class is declared as:
+
+ \snippet btchat/chatserver.h declaration
+
+ The first thing the chat server needs to do is create an instance of QRfcommServer to listen
+ for incoming Bluetooth connections. Our clientConnected() slot will be called whenever a new
+ connection is created.
+
+ \snippet btchat/chatserver.cpp Create the server
+
+ The chat server is only useful if others know that it is there. To enable other devices to
+ discover it a record describing the service needs to be published in the systems SDP (Service
+ Discovery Protocol) database. The QBluetoothServiceInfo class encapsulates a service record.
+
+ We will publish a service record that contains some textural descriptions of the services, a
+ UUID that uniquely identifies the service, the discoverability attribute and connection
+ parameters.
+
+ The textural description of the service is stored in the ServiceName, ServiceDescription and
+ ServiceProvider attributes.
+
+ \snippet btchat/chatserver.cpp Service name, description and provider
+
+ Bluetooth uses UUIDs as unique identifiers. Our chat service is uses a randomly generated
+ UUID.
+
+ \snippet btchat/chatserver.cpp Service UUID
+ \snippet btchat/chatserver.cpp Service UUID set
+
+ A Bluetooth service is only discoverable if it is in the PublicBrowseGroup.
+
+ \snippet btchat/chatserver.cpp Service Discoverability
+
+ The ProtocolDescriptorList attribute is used to publish the connection parameters that the
+ remote device requires to connect to our service. Here we specify that the Rfcomm protocol is
+ used and set the port number to port that our rfcommServer instance is listening to.
+
+ \snippet btchat/chatserver.cpp Protocol descriptor list
+
+ Finally we register the service record with the system.
+
+ \snippet btchat/chatserver.cpp Register service
+
+ As mentioned earlier incoming connections are handled in the clientConnected() slot. In this
+ slot we get a pointer a QBluetoothSocket representing the next pending connection connect up
+ slots to the readyRead() and disconnected() signals and emit a signal to notify others that a
+ new client has connected.
+
+ \snippet btchat/chatserver.cpp clientConnected
+
+ The readSocket() slot is called whenever data is ready to be read from a client socket. The
+ slot reads individual lines from the socket converts them from UTF-8 and emits the
+ messageReceived() signal.
+
+ \snippet btchat/chatserver.cpp readSocket
+
+ The clientDisconnected() slot is called whenever a client disconnects from the service. The
+ slot emits a signal to notify others that a client has disconnected and deletes the socket.
+
+ \snippet btchat/chatserver.cpp clientDisconnected
+
+ The sendMessage() slot is used to send a message to all connected clients. The message is
+ converted into UTF-8 and appended with a newline before being sent to all clients.
+
+ \snippet btchat/chatserver.cpp sendMessage
+
+ When the chat server is stop the service record is removed from the system SDP database, all
+ connected client sockets are deleted and the QRfcommServer instance is deleted.
+
+ \snippet btchat/chatserver.cpp stopServer
+
+ \section1 Chat Client
+
+ The chat client is implemented by the ChatClient class. The ChatClient class is declared as:
+
+ \snippet btchat/chatclient.h declaration
+
+ The client creates a new QBluetoothSocket and connects to the remote service described by the
+ \e remoteService parameter. Slots are connected to the sockets readyRead(), connected() and
+ disconnected() signals.
+
+ \snippet btchat/chatclient.cpp startClient
+
+ On sucessfull socket connection we emit a signal to notify other.
+
+ \snippet btchat/chatclient.cpp connected
+
+ Similarily to the chat server the readSocket() slot is called when data is available from the
+ socket. Lines are read individually and converted from UTF-8. The messageReceived() signal
+ is emitted.
+
+ \snippet btchat/chatclient.cpp readSocket
+
+ The sendMessage() slot is used to send a message to the remote device. The message is
+ converted to UTF-8 and a newline appended.
+
+ \snippet btchat/chatclient.cpp sendMessage
+
+ To disconnect from the remote chat service the QBluetoothSocket instance is deleted.
+
+ \snippet btchat/chatclient.cpp stopClient
+
+ \section1 Chat Dialog
+
+ The main window of this example is the chat dialog, implemented in the Chat class. This class
+ displays a chat session between a single ChatServer and zero or more ChatClients. The Chat
+ class is declared as:
+
+ \snippet btchat/chat.h declaration
+
+ First we construct the user interface
+
+ \snippet btchat/chat.cpp Construct UI
+
+ We create an instance of the ChatServer and respond to its clientConnected(),
+ clientDiconnected() and messageReceived() signals.
+
+ \snippet btchat/chat.cpp Create Chat Server
+
+ In response to the clientConnected() and clientDisconnected() signals of the ChatServer we
+ display the typical "foo has joined chat." and "foo has left." messages in the chat session.
+
+ \snippet btchat/chat.cpp clientConnected clientDisconnected
+
+ Incoming messages from clients connected to the ChatServer are handled in the showMessage()
+ slot. The message text tagged with the remote device name is displayed in the chat session.
+
+ \snippet btchat/chat.cpp showMessage
+
+ In response to the connect button being clicked the application starts service discovery and
+ presents a list of discovered chat services on remote devices. A ChatClient for the service
+ selected by the user.
+
+ \snippet btchat/chat.cpp Connect to remote service
+
+ In reponse to connected() signals from ChatClient we display the a "Joined chat with foo."
+ message in the chat session.
+
+ \snippet btchat/chat.cpp connected
+
+ Messages are sent to all remote devices via the ChatServer and ChatClient instances by emitting
+ the sendMessage() signal.
+
+ \snippet btchat/chat.cpp sendClicked
+
+ We need to clean up ChatClient instances when the remote device forces a disconnect.
+
+ \snippet btchat/chat.cpp clientDisconnected
+*/
diff --git a/examples/bluetooth/btchat/main.cpp b/examples/bluetooth/btchat/main.cpp
new file mode 100644
index 00000000..cb32f448
--- /dev/null
+++ b/examples/bluetooth/btchat/main.cpp
@@ -0,0 +1,62 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtBluetooth module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "chat.h"
+
+#include <QApplication>
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+
+ Chat d;
+ QObject::connect(&d, SIGNAL(accepted()), &app, SLOT(quit()));
+
+#if defined(Q_WS_MAEMO_6)
+ d.showFullScreen();
+#else
+ d.show();
+#endif
+
+ app.exec();
+
+ return 0;
+}
+
diff --git a/examples/bluetooth/btchat/remoteselector.cpp b/examples/bluetooth/btchat/remoteselector.cpp
new file mode 100644
index 00000000..a63c81c2
--- /dev/null
+++ b/examples/bluetooth/btchat/remoteselector.cpp
@@ -0,0 +1,146 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtBluetooth module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "remoteselector.h"
+#include "ui_remoteselector.h"
+
+#include <qbluetoothdeviceinfo.h>
+#include <qbluetoothaddress.h>
+
+QTBLUETOOTH_USE_NAMESPACE
+
+RemoteSelector::RemoteSelector(QWidget *parent)
+: QDialog(parent), ui(new Ui::RemoteSelector),
+ m_discoveryAgent(new QBluetoothServiceDiscoveryAgent)
+{
+ ui->setupUi(this);
+
+#if defined(Q_OS_WINCE) || defined(Q_WS_MAEMO_6)
+ setWindowState(Qt::WindowFullScreen);
+#endif
+
+ connect(m_discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
+ this, SLOT(serviceDiscovered(QBluetoothServiceInfo)));
+ connect(m_discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished()));
+}
+
+RemoteSelector::~RemoteSelector()
+{
+ delete ui;
+}
+
+void RemoteSelector::startDiscovery(const QBluetoothUuid &uuid)
+{
+ if (m_discoveryAgent->isActive())
+ m_discoveryAgent->stop();
+
+ ui->remoteDevices->clear();
+
+ m_discoveryAgent->setUuidFilter(uuid);
+ m_discoveryAgent->start();
+
+ ui->status->setText(tr("Scanning..."));
+}
+
+void RemoteSelector::stopDiscovery()
+{
+ if (m_discoveryAgent){
+ m_discoveryAgent->stop();
+ }
+}
+
+QBluetoothServiceInfo RemoteSelector::service() const
+{
+ return m_service;
+}
+
+void RemoteSelector::serviceDiscovered(const QBluetoothServiceInfo &serviceInfo)
+{
+#if 0
+ qDebug() << "Discovered service on"
+ << serviceInfo.device().name() << serviceInfo.device().address().toString();
+ qDebug() << "\tService name:" << serviceInfo.serviceName();
+ qDebug() << "\tDescription:"
+ << serviceInfo.attribute(QBluetoothServiceInfo::ServiceDescription).toString();
+ qDebug() << "\tProvider:"
+ << serviceInfo.attribute(QBluetoothServiceInfo::ServiceProvider).toString();
+ qDebug() << "\tL2CAP protocol service multiplexer:"
+ << serviceInfo.protocolServiceMultiplexer();
+ qDebug() << "\tRFCOMM server channel:" << serviceInfo.serverChannel();
+#endif
+ QMapIterator<QListWidgetItem *, QBluetoothServiceInfo> i(m_discoveredServices);
+ while (i.hasNext()){
+ i.next();
+ if (serviceInfo.device().address() == i.value().device().address()){
+ return;
+ }
+ }
+
+ QString remoteName;
+ if (serviceInfo.device().name().isEmpty())
+ remoteName = serviceInfo.device().address().toString();
+ else
+ remoteName = serviceInfo.device().name();
+
+ QListWidgetItem *item =
+ new QListWidgetItem(QString::fromLatin1("%1 %2").arg(remoteName,
+ serviceInfo.serviceName()));
+
+ m_discoveredServices.insert(item, serviceInfo);
+ ui->remoteDevices->addItem(item);
+}
+
+void RemoteSelector::discoveryFinished()
+{
+ ui->status->setText(tr("Select the chat service to connect to."));
+}
+
+void RemoteSelector::on_remoteDevices_itemActivated(QListWidgetItem *item)
+{
+ qDebug() << "got click" << item->text();
+ m_service = m_discoveredServices.value(item);
+
+ accept();
+}
+
+void RemoteSelector::on_cancelButton_clicked()
+{
+ reject();
+}
diff --git a/examples/bluetooth/btchat/remoteselector.h b/examples/bluetooth/btchat/remoteselector.h
new file mode 100644
index 00000000..eff94895
--- /dev/null
+++ b/examples/bluetooth/btchat/remoteselector.h
@@ -0,0 +1,87 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtBluetooth module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef REMOTESELECTOR_H
+#define REMOTESELECTOR_H
+
+#include <QDialog>
+
+#include <qbluetoothuuid.h>
+#include <qbluetoothserviceinfo.h>
+#include <qbluetoothservicediscoveryagent.h>
+
+QT_FORWARD_DECLARE_CLASS(QModelIndex)
+QT_FORWARD_DECLARE_CLASS(QListWidgetItem)
+
+QTBLUETOOTH_USE_NAMESPACE
+
+QT_BEGIN_NAMESPACE
+namespace Ui {
+ class RemoteSelector;
+}
+QT_END_NAMESPACE
+
+class RemoteSelector : public QDialog
+{
+ Q_OBJECT
+
+public:
+ explicit RemoteSelector(QWidget *parent = 0);
+ ~RemoteSelector();
+
+ void startDiscovery(const QBluetoothUuid &uuid);
+ void stopDiscovery();
+ QBluetoothServiceInfo service() const;
+
+private:
+ Ui::RemoteSelector *ui;
+
+ QBluetoothServiceDiscoveryAgent *m_discoveryAgent;
+ QBluetoothServiceInfo m_service;
+ QMap<QListWidgetItem *, QBluetoothServiceInfo> m_discoveredServices;
+
+private slots:
+ void serviceDiscovered(const QBluetoothServiceInfo &serviceInfo);
+ void discoveryFinished();
+ void on_remoteDevices_itemActivated(QListWidgetItem *item);
+ void on_cancelButton_clicked();
+};
+
+#endif // REMOTESELECTOR_H
diff --git a/examples/bluetooth/btchat/remoteselector.ui b/examples/bluetooth/btchat/remoteselector.ui
new file mode 100644
index 00000000..d415f416
--- /dev/null
+++ b/examples/bluetooth/btchat/remoteselector.ui
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>RemoteSelector</class>
+ <widget class="QDialog" name="RemoteSelector">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>400</width>
+ <height>300</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>Available chat services</string>
+ </property>
+ <layout class="QVBoxLayout" name="verticalLayout">
+ <item>
+ <widget class="QLabel" name="status">
+ <property name="text">
+ <string>Scanning...</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QListWidget" name="remoteDevices"/>
+ </item>
+ <item>
+ <widget class="QPushButton" name="cancelButton">
+ <property name="text">
+ <string>Cancel</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>