summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@digia.com>2013-09-23 13:52:35 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-09-25 10:37:27 +0200
commit85de7d64e85aa058f1777bc9d1a778790b1d50a6 (patch)
treea498afb7454905736cf589d3624ab68542ae04d8 /src
parent85eac47703aa40df76e5102eb04291d8f029bfa3 (diff)
Fix peer to peer communication using two local adapters.
The code assumed to use the default adapter in a variety of places which caused wrong SDP registrations, device searches and peer names. The btchat examples was extended to cope with two local adapters. If there are more than two local adapters they will be ignored. Change-Id: I27d8bce65d943773e4e6cbd86982446fa79664a4 Reviewed-by: Fabian Bumberger <fbumberger@rim.com> Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'src')
-rw-r--r--src/bluetooth/qbluetoothservicediscoveryagent.cpp6
-rw-r--r--src/bluetooth/qbluetoothserviceinfo.cpp16
-rw-r--r--src/bluetooth/qbluetoothserviceinfo.h5
-rw-r--r--src/bluetooth/qbluetoothserviceinfo_bluez.cpp32
-rw-r--r--src/bluetooth/qbluetoothserviceinfo_p.cpp9
-rw-r--r--src/bluetooth/qbluetoothserviceinfo_p.h16
-rw-r--r--src/bluetooth/qbluetoothserviceinfo_qnx.cpp9
-rw-r--r--src/bluetooth/qbluetoothsocket_bluez.cpp9
8 files changed, 65 insertions, 37 deletions
diff --git a/src/bluetooth/qbluetoothservicediscoveryagent.cpp b/src/bluetooth/qbluetoothservicediscoveryagent.cpp
index 023ffb80..b613642c 100644
--- a/src/bluetooth/qbluetoothservicediscoveryagent.cpp
+++ b/src/bluetooth/qbluetoothservicediscoveryagent.cpp
@@ -329,7 +329,11 @@ void QBluetoothServiceDiscoveryAgentPrivate::startDeviceDiscovery()
Q_Q(QBluetoothServiceDiscoveryAgent);
if (!deviceDiscoveryAgent) {
- deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent;
+#ifdef QT_BLUEZ_BLUETOOTH
+ deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(m_deviceAdapterAddress, q);
+#else
+ deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(q);
+#endif
QObject::connect(deviceDiscoveryAgent, SIGNAL(finished()),
q, SLOT(_q_deviceDiscoveryFinished()));
QObject::connect(deviceDiscoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
diff --git a/src/bluetooth/qbluetoothserviceinfo.cpp b/src/bluetooth/qbluetoothserviceinfo.cpp
index 4e2c0499..0a1d9ee5 100644
--- a/src/bluetooth/qbluetoothserviceinfo.cpp
+++ b/src/bluetooth/qbluetoothserviceinfo.cpp
@@ -152,21 +152,27 @@ bool QBluetoothServiceInfo::isRegistered() const
}
/*!
- \fn bool QBluetoothServiceInfo::registerService() const
+ \fn bool QBluetoothServiceInfo::registerService()
Registers this service with the platform's Service Discovery Protocol (SDP) implementation,
making it findable by other devices when they perform service discovery. Returns true if the
service is successfully registered, otherwise returns false. Once registered changes to the record
cannot be made. The service must be unregistered and registered again with the changes.
+
+ The \l localAdapter parameter determines the local Bluetooth adapter under which
+ the service should be registered. If \a localAdapter is \c null the default Bluetooth adapter
+ will be used. If this service info object is already registered via a local adapter
+ and this is function is called using a different local adapter, the previous registration
+ is removed and the service reregistered using the new adapter.
*/
-bool QBluetoothServiceInfo::registerService() const
+bool QBluetoothServiceInfo::registerService(const QBluetoothAddress &localAdapter)
{
- return d_ptr->registerService();
+ return d_ptr->registerService(localAdapter);
}
/*!
- \fn bool QBluetoothServiceInfo::unregisterService() const
+ \fn bool QBluetoothServiceInfo::unregisterService()
Unregisters this service with the platform's Service Discovery Protocol (SDP) implementation.
After this, the service will no longer be findable by other devices through service discovery.
@@ -174,7 +180,7 @@ bool QBluetoothServiceInfo::registerService() const
Returns true if the service is successfully unregistered, otherwise returns false.
*/
-bool QBluetoothServiceInfo::unregisterService() const
+bool QBluetoothServiceInfo::unregisterService()
{
return d_ptr->unregisterService();
}
diff --git a/src/bluetooth/qbluetoothserviceinfo.h b/src/bluetooth/qbluetoothserviceinfo.h
index 507c7c1d..0e6555c0 100644
--- a/src/bluetooth/qbluetoothserviceinfo.h
+++ b/src/bluetooth/qbluetoothserviceinfo.h
@@ -45,6 +45,7 @@
#include <QtBluetooth/qbluetoothglobal.h>
#include <QtBluetooth/QBluetoothUuid>
+#include <QtBluetooth/QBluetoothAddress>
#include <QtCore/QMetaType>
#include <QtCore/QList>
@@ -145,8 +146,8 @@ public:
QBluetoothServiceInfo &operator=(const QBluetoothServiceInfo &other);
bool isRegistered() const;
- bool registerService() const;
- bool unregisterService() const;
+ bool registerService(const QBluetoothAddress &localAdapter = QBluetoothAddress());
+ bool unregisterService();
protected:
friend Q_BLUETOOTH_EXPORT QDebug operator<<(QDebug, const QBluetoothServiceInfo &);
diff --git a/src/bluetooth/qbluetoothserviceinfo_bluez.cpp b/src/bluetooth/qbluetoothserviceinfo_bluez.cpp
index a98b4a46..a24e8cea 100644
--- a/src/bluetooth/qbluetoothserviceinfo_bluez.cpp
+++ b/src/bluetooth/qbluetoothserviceinfo_bluez.cpp
@@ -181,12 +181,12 @@ bool QBluetoothServiceInfoPrivate::isRegistered() const
return registered;
}
-bool QBluetoothServiceInfoPrivate::unregisterService() const
+bool QBluetoothServiceInfoPrivate::unregisterService()
{
if (!registered)
return false;
- if (!ensureSdpConnection())
+ if (!ensureSdpConnection(currentLocalAdapter))
return false;
QDBusPendingReply<> reply = service->RemoveRecord(serviceRecord);
@@ -200,7 +200,7 @@ bool QBluetoothServiceInfoPrivate::unregisterService() const
return true;
}
-void QBluetoothServiceInfoPrivate::setRegisteredAttribute(quint16 attributeId, const QVariant &value) const
+void QBluetoothServiceInfoPrivate::setRegisteredAttribute(quint16 attributeId, const QVariant &value)
{
Q_UNUSED(attributeId);
Q_UNUSED(value);
@@ -208,35 +208,47 @@ void QBluetoothServiceInfoPrivate::setRegisteredAttribute(quint16 attributeId, c
registerService();
}
-void QBluetoothServiceInfoPrivate::removeRegisteredAttribute(quint16 attributeId) const
+void QBluetoothServiceInfoPrivate::removeRegisteredAttribute(quint16 attributeId)
{
Q_UNUSED(attributeId);
registerService();
}
-bool QBluetoothServiceInfoPrivate::ensureSdpConnection() const
+bool QBluetoothServiceInfoPrivate::ensureSdpConnection(const QBluetoothAddress &localAdapter)
{
- if (service)
+ if (service && currentLocalAdapter == localAdapter)
return true;
+ delete service;
+
OrgBluezManagerInterface manager(QLatin1String("org.bluez"), QLatin1String("/"),
QDBusConnection::systemBus());
- QDBusPendingReply<QDBusObjectPath> reply = manager.FindAdapter(QLatin1String("any"));
+
+ QDBusPendingReply<QDBusObjectPath> reply;
+ if (localAdapter.isNull())
+ reply = manager.DefaultAdapter();
+ else
+ reply = manager.FindAdapter(localAdapter.toString());
reply.waitForFinished();
if (reply.isError())
return false;
+ currentLocalAdapter = localAdapter;
service = new OrgBluezServiceInterface(QLatin1String("org.bluez"), reply.value().path(),
- QDBusConnection::systemBus());
+ QDBusConnection::systemBus(), this);
return true;
}
-bool QBluetoothServiceInfoPrivate::registerService() const
+bool QBluetoothServiceInfoPrivate::registerService(const QBluetoothAddress &localAdapter)
{
- if (!ensureSdpConnection()) {
+ //if new adapter unregister previous one first
+ if (registered && localAdapter != currentLocalAdapter)
+ unregisterService();
+
+ if (!ensureSdpConnection(localAdapter)) {
qWarning() << "SDP not connected. Cannot register";
return false;
}
diff --git a/src/bluetooth/qbluetoothserviceinfo_p.cpp b/src/bluetooth/qbluetoothserviceinfo_p.cpp
index 13bc8fd5..51aeb75b 100644
--- a/src/bluetooth/qbluetoothserviceinfo_p.cpp
+++ b/src/bluetooth/qbluetoothserviceinfo_p.cpp
@@ -57,24 +57,25 @@ bool QBluetoothServiceInfoPrivate::isRegistered() const
return false;
}
-bool QBluetoothServiceInfoPrivate::registerService() const
+bool QBluetoothServiceInfoPrivate::registerService(const QBluetoothAddress &localAdapter)
{
+ Q_UNUSED(localAdapter);
return false;
}
-bool QBluetoothServiceInfoPrivate::unregisterService() const
+bool QBluetoothServiceInfoPrivate::unregisterService()
{
return false;
}
-void QBluetoothServiceInfoPrivate::setRegisteredAttribute(quint16 attributeId, const QVariant &value) const
+void QBluetoothServiceInfoPrivate::setRegisteredAttribute(quint16 attributeId, const QVariant &value)
{
Q_UNUSED(attributeId);
Q_UNUSED(value);
}
-void QBluetoothServiceInfoPrivate::removeRegisteredAttribute(quint16 attributeId) const
+void QBluetoothServiceInfoPrivate::removeRegisteredAttribute(quint16 attributeId)
{
Q_UNUSED(attributeId);
}
diff --git a/src/bluetooth/qbluetoothserviceinfo_p.h b/src/bluetooth/qbluetoothserviceinfo_p.h
index 0e263fe6..a98cf0e9 100644
--- a/src/bluetooth/qbluetoothserviceinfo_p.h
+++ b/src/bluetooth/qbluetoothserviceinfo_p.h
@@ -43,6 +43,7 @@
#define QBLUETOOTHSERVICEINFO_P_H
#include "qbluetoothuuid.h"
+#include "qbluetoothaddress.h"
#include "qbluetoothdeviceinfo.h"
#include "qbluetoothserviceinfo.h"
@@ -63,14 +64,14 @@ public:
QBluetoothServiceInfoPrivate();
~QBluetoothServiceInfoPrivate();
- bool registerService() const;
+ bool registerService(const QBluetoothAddress &localAdapter = QBluetoothAddress());
bool isRegistered() const;
- bool unregisterService() const;
+ bool unregisterService();
- void setRegisteredAttribute(quint16 attributeId, const QVariant &value) const;
- void removeRegisteredAttribute(quint16 attributeId) const;
+ void setRegisteredAttribute(quint16 attributeId, const QVariant &value);
+ void removeRegisteredAttribute(quint16 attributeId);
QBluetoothDeviceInfo deviceInfo;
QMap<quint16, QVariant> attributes;
@@ -78,10 +79,11 @@ public:
int serverChannel() const;
private:
#ifdef QT_BLUEZ_BLUETOOTH
- bool ensureSdpConnection() const;
+ bool ensureSdpConnection(const QBluetoothAddress &localAdapter = QBluetoothAddress());
- mutable OrgBluezServiceInterface *service;
- mutable quint32 serviceRecord;
+ OrgBluezServiceInterface *service;
+ quint32 serviceRecord;
+ QBluetoothAddress currentLocalAdapter;
#endif
mutable bool registered;
diff --git a/src/bluetooth/qbluetoothserviceinfo_qnx.cpp b/src/bluetooth/qbluetoothserviceinfo_qnx.cpp
index cb82851c..cd1ecfef 100644
--- a/src/bluetooth/qbluetoothserviceinfo_qnx.cpp
+++ b/src/bluetooth/qbluetoothserviceinfo_qnx.cpp
@@ -61,7 +61,7 @@ bool QBluetoothServiceInfoPrivate::isRegistered() const
return registered;
}
-bool QBluetoothServiceInfoPrivate::unregisterService() const
+bool QBluetoothServiceInfoPrivate::unregisterService()
{
if (!registered)
return false;
@@ -70,7 +70,7 @@ bool QBluetoothServiceInfoPrivate::unregisterService() const
}
-void QBluetoothServiceInfoPrivate::setRegisteredAttribute(quint16 attributeId, const QVariant &value) const
+void QBluetoothServiceInfoPrivate::setRegisteredAttribute(quint16 attributeId, const QVariant &value)
{
Q_UNUSED(attributeId);
Q_UNUSED(value);
@@ -78,7 +78,7 @@ void QBluetoothServiceInfoPrivate::setRegisteredAttribute(quint16 attributeId, c
registerService();
}
-void QBluetoothServiceInfoPrivate::removeRegisteredAttribute(quint16 attributeId) const
+void QBluetoothServiceInfoPrivate::removeRegisteredAttribute(quint16 attributeId)
{
Q_UNUSED(attributeId);
registered = false;
@@ -86,8 +86,9 @@ void QBluetoothServiceInfoPrivate::removeRegisteredAttribute(quint16 attributeId
extern QHash<QBluetoothServerPrivate*, int> __fakeServerPorts;
-bool QBluetoothServiceInfoPrivate::registerService() const
+bool QBluetoothServiceInfoPrivate::registerService(const QBluetoothAddress& localAdapter)
{
+ Q_UNUSED(localAdapter); //QNX always uses default local adapter
if (protocolDescriptor(QBluetoothUuid::Rfcomm).isEmpty()) {
qWarning() << Q_FUNC_INFO << "Only SPP services can be registered on QNX";
return false;
diff --git a/src/bluetooth/qbluetoothsocket_bluez.cpp b/src/bluetooth/qbluetoothsocket_bluez.cpp
index bc7dc972..c6f332e3 100644
--- a/src/bluetooth/qbluetoothsocket_bluez.cpp
+++ b/src/bluetooth/qbluetoothsocket_bluez.cpp
@@ -362,12 +362,13 @@ QString QBluetoothSocketPrivate::peerName() const
return QString();
}
- const QString address = QBluetoothAddress(bdaddr).toString();
+ const QString peerAddress = QBluetoothAddress(bdaddr).toString();
+ const QString localAdapter = localAddress().toString();
OrgBluezManagerInterface manager(QLatin1String("org.bluez"), QLatin1String("/"),
QDBusConnection::systemBus());
- QDBusPendingReply<QDBusObjectPath> reply = manager.DefaultAdapter();
+ QDBusPendingReply<QDBusObjectPath> reply = manager.FindAdapter(localAdapter);
reply.waitForFinished();
if (reply.isError())
return QString();
@@ -375,13 +376,13 @@ QString QBluetoothSocketPrivate::peerName() const
OrgBluezAdapterInterface adapter(QLatin1String("org.bluez"), reply.value().path(),
QDBusConnection::systemBus());
- QDBusPendingReply<QDBusObjectPath> deviceObjectPath = adapter.CreateDevice(address);
+ QDBusPendingReply<QDBusObjectPath> deviceObjectPath = adapter.CreateDevice(peerAddress);
deviceObjectPath.waitForFinished();
if (deviceObjectPath.isError()) {
if (deviceObjectPath.error().name() != QLatin1String("org.bluez.Error.AlreadyExists"))
return QString();
- deviceObjectPath = adapter.FindDevice(address);
+ deviceObjectPath = adapter.FindDevice(peerAddress);
deviceObjectPath.waitForFinished();
if (deviceObjectPath.isError())
return QString();