summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@qt.io>2018-07-23 13:13:49 +0200
committerAlex Blasche <alexander.blasche@qt.io>2018-07-25 11:16:30 +0000
commite84d9f24cbb7c686535cbe7e13f28a1c0baaa48d (patch)
treeaba4b661532d1843706b15b17f92e328a9ab70bb
parent81de083e4f9fdee7f1ef7d2fffc7d2147ddbc1b0 (diff)
Move QBluetoothSocket::connectToService() to private implementations
This permits each platform to customize the implementations without the need for ifdefs. Upcoming changes such as the BLuez DBuS addition will increase the platform differences. Task-number: QTBUG-68550 Change-Id: I8fc9a74d3ce704466f0bf2c16287e32f222c4376 Reviewed-by: Lubomir I. Ivanov <neolit123@gmail.com> Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
-rw-r--r--src/bluetooth/qbluetoothsocket.cpp140
-rw-r--r--src/bluetooth/qbluetoothsocket_android.cpp66
-rw-r--r--src/bluetooth/qbluetoothsocket_android_p.h7
-rw-r--r--src/bluetooth/qbluetoothsocket_bluez.cpp105
-rw-r--r--src/bluetooth/qbluetoothsocket_bluez_p.h7
-rw-r--r--src/bluetooth/qbluetoothsocket_bluezdbus.cpp23
-rw-r--r--src/bluetooth/qbluetoothsocket_bluezdbus_p.h7
-rw-r--r--src/bluetooth/qbluetoothsocket_p.cpp41
-rw-r--r--src/bluetooth/qbluetoothsocket_p.h8
-rw-r--r--src/bluetooth/qbluetoothsocket_winrt.cpp99
-rw-r--r--src/bluetooth/qbluetoothsocket_winrt_p.h8
-rw-r--r--src/bluetooth/qbluetoothsocketbase_p.h7
12 files changed, 384 insertions, 134 deletions
diff --git a/src/bluetooth/qbluetoothsocket.cpp b/src/bluetooth/qbluetoothsocket.cpp
index 1849844e..5f54da12 100644
--- a/src/bluetooth/qbluetoothsocket.cpp
+++ b/src/bluetooth/qbluetoothsocket.cpp
@@ -49,11 +49,8 @@
#include "qbluetoothsocket_p.h"
#endif
-#include "qbluetoothdeviceinfo.h"
-#include "qbluetoothserviceinfo.h"
#include "qbluetoothservicediscoveryagent.h"
-
#include <QtCore/QLoggingCategory>
#include <QSocketNotifier>
@@ -354,62 +351,7 @@ qint64 QBluetoothSocket::bytesToWrite() const
void QBluetoothSocket::connectToService(const QBluetoothServiceInfo &service, OpenMode openMode)
{
Q_D(QBluetoothSocketBase);
-
- if (state() != QBluetoothSocket::UnconnectedState && state() != QBluetoothSocket::ServiceLookupState) {
- qCWarning(QT_BT) << "QBluetoothSocket::connectToService called on busy socket";
- d->errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
- setSocketError(QBluetoothSocket::OperationError);
- return;
- }
-#if defined(QT_ANDROID_BLUETOOTH)
- if (!d->ensureNativeSocket(service.socketProtocol())) {
- d->errorString = tr("Socket type not supported");
- setSocketError(QBluetoothSocket::UnsupportedProtocolError);
- return;
- }
- d->connectToServiceHelper(service.device().address(), service.serviceUuid(), openMode);
-#else
-#if defined(QT_WINRT_BLUETOOTH)
- // Report these problems early:
- if (socketType() != QBluetoothServiceInfo::RfcommProtocol) {
- d->errorString = tr("Socket type not supported");
- setSocketError(QBluetoothSocket::UnsupportedProtocolError);
- return;
- }
-#endif // QT_WINRT_BLUETOOTH
- if (socketType() == QBluetoothServiceInfo::UnknownProtocol) {
- qCWarning(QT_BT) << "QBluetoothSocket::connectToService cannot "
- "connect with 'UnknownProtocol' type";
- d->errorString = tr("Socket type not supported");
- setSocketError(QBluetoothSocket::UnsupportedProtocolError);
- return;
- }
-
- if (service.protocolServiceMultiplexer() > 0) {
- if (!d->ensureNativeSocket(QBluetoothServiceInfo::L2capProtocol)) {
- d->errorString = tr("Unknown socket error");
- setSocketError(UnknownSocketError);
- return;
- }
- d->connectToServiceHelper(service.device().address(), service.protocolServiceMultiplexer(), openMode);
- } else if (service.serverChannel() > 0) {
- if (!d->ensureNativeSocket(QBluetoothServiceInfo::RfcommProtocol)) {
- d->errorString = tr("Unknown socket error");
- setSocketError(UnknownSocketError);
- return;
- }
- d->connectToServiceHelper(service.device().address(), service.serverChannel(), openMode);
- } else {
- // try doing service discovery to see if we can find the socket
- if (service.serviceUuid().isNull()
- && !service.serviceClassUuids().contains(QBluetoothUuid::SerialPort)) {
- qCWarning(QT_BT) << "No port, no PSM, and no UUID provided, unable to connect";
- return;
- }
- qCDebug(QT_BT) << "Need a port/psm, doing discovery";
- doDeviceDiscovery(service, openMode);
- }
-#endif
+ d->connectToService(service, openMode);
}
/*!
@@ -429,10 +371,10 @@ void QBluetoothSocket::connectToService(const QBluetoothServiceInfo &service, Op
For BlueZ, the socket first enters the \l ServiceLookupState and queries the connection parameters for
\a uuid. If the service parameters are successfully retrieved the socket enters
ConnectingState, and attempts to connect to \a address. If a connection is established,
- QBluetoothSocket enters Connected State and emits connected().
+ QBluetoothSocket enters \l ConnectedState and emits connected().
On Android, the service connection can directly be established
- using the UUID of the remote service. Therefore the platforms does not require
+ using the UUID of the remote service. Therefore the platform does not require
the \l ServiceLookupState and \l socketType() is always set to
\l QBluetoothServiceInfo::RfcommProtocol.
@@ -446,44 +388,7 @@ void QBluetoothSocket::connectToService(const QBluetoothServiceInfo &service, Op
void QBluetoothSocket::connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid, OpenMode openMode)
{
Q_D(QBluetoothSocketBase);
-
- if (state() != QBluetoothSocket::UnconnectedState) {
- qCWarning(QT_BT) << "QBluetoothSocket::connectToService called on busy socket";
- d->errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
- setSocketError(QBluetoothSocket::OperationError);
- return;
- }
-
-#if defined(QT_ANDROID_BLUETOOTH)
- if (!d->ensureNativeSocket(QBluetoothServiceInfo::RfcommProtocol)) {
- d->errorString = tr("Socket type not supported");
- setSocketError(QBluetoothSocket::UnsupportedProtocolError);
- return;
- }
- d->connectToServiceHelper(address, uuid, openMode);
-#else
-#if defined(QT_WINRT_BLUETOOTH)
- // Report these problems early, prevent device discovery:
- if (socketType() != QBluetoothServiceInfo::RfcommProtocol) {
- d->errorString = tr("Socket type not supported");
- setSocketError(QBluetoothSocket::UnsupportedProtocolError);
- return;
- }
-#endif // QT_WINRT_BLUETOOTH
- if (socketType() == QBluetoothServiceInfo::UnknownProtocol) {
- qCWarning(QT_BT) << "QBluetoothSocket::connectToService cannot "
- "connect with 'UnknownProtocol' type";
- d->errorString = tr("Socket type not supported");
- setSocketError(QBluetoothSocket::UnsupportedProtocolError);
- return;
- }
-
- QBluetoothServiceInfo service;
- QBluetoothDeviceInfo device(address, QString(), QBluetoothDeviceInfo::MiscellaneousDevice);
- service.setDevice(device);
- service.setServiceUuid(uuid);
- doDeviceDiscovery(service, openMode);
-#endif
+ d->connectToService(address, uuid, openMode);
}
/*!
@@ -497,7 +402,7 @@ void QBluetoothSocket::connectToService(const QBluetoothAddress &address, const
At any point, the socket can emit error() to signal that an error occurred.
On Android, a connection to a service can not be established using a port. Calling this function
- will emit a \l {QBluetoothSocket::ServiceNotFoundError}{ServiceNotFoundError}
+ will emit a \l {QBluetoothSocket::ServiceNotFoundError}{ServiceNotFoundError}.
Note that most platforms require a pairing prior to connecting to the remote device. Otherwise
the connection process may fail.
@@ -507,40 +412,7 @@ void QBluetoothSocket::connectToService(const QBluetoothAddress &address, const
void QBluetoothSocket::connectToService(const QBluetoothAddress &address, quint16 port, OpenMode openMode)
{
Q_D(QBluetoothSocketBase);
-#if defined(QT_ANDROID_BLUETOOTH)
- Q_UNUSED(port);
- Q_UNUSED(openMode);
- Q_UNUSED(address);
- d->errorString = tr("Connecting to port is not supported");
- setSocketError(QBluetoothSocket::ServiceNotFoundError);
- qCWarning(QT_BT) << "Connecting to port is not supported";
-#else
-#if defined(QT_WINRT_BLUETOOTH)
- // Report these problems early
- if (socketType() != QBluetoothServiceInfo::RfcommProtocol) {
- d->errorString = tr("Socket type not supported");
- setSocketError(QBluetoothSocket::UnsupportedProtocolError);
- return;
- }
-#endif // QT_WINRT_BLUETOOTH
- if (socketType() == QBluetoothServiceInfo::UnknownProtocol) {
- qCWarning(QT_BT) << "QBluetoothSocket::connectToService cannot "
- "connect with 'UnknownProtocol' type";
- d->errorString = tr("Socket type not supported");
- setSocketError(QBluetoothSocket::UnsupportedProtocolError);
- return;
- }
-
- if (state() != QBluetoothSocket::UnconnectedState) {
- qCWarning(QT_BT) << "QBluetoothSocket::connectToService called on busy socket";
- d->errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
- setSocketError(QBluetoothSocket::OperationError);
- return;
- }
-
- setOpenMode(openMode);
- d->connectToServiceHelper(address, port, openMode);
-#endif
+ d->connectToService(address, port, openMode);
}
/*!
diff --git a/src/bluetooth/qbluetoothsocket_android.cpp b/src/bluetooth/qbluetoothsocket_android.cpp
index 109d3141..9047bb31 100644
--- a/src/bluetooth/qbluetoothsocket_android.cpp
+++ b/src/bluetooth/qbluetoothsocket_android.cpp
@@ -41,6 +41,8 @@
#include "qbluetoothsocket.h"
#include "qbluetoothsocket_android_p.h"
#include "qbluetoothaddress.h"
+#include "qbluetoothdeviceinfo.h"
+#include "qbluetoothserviceinfo.h"
#include <QtCore/QLoggingCategory>
#include <QtCore/QThread>
#include <QtCore/QTime>
@@ -503,6 +505,70 @@ void QBluetoothSocketPrivateAndroid::connectToServiceHelper(const QBluetoothAddr
emit connectJavaSocket();
}
+void QBluetoothSocketPrivateAndroid::connectToService(
+ const QBluetoothServiceInfo &service, QIODevice::OpenMode openMode)
+{
+ Q_Q(QBluetoothSocket);
+
+ if (q->state() != QBluetoothSocket::UnconnectedState
+ && q->state() != QBluetoothSocket::ServiceLookupState) {
+ qCWarning(QT_BT_ANDROID) << "QBluetoothSocketPrivateAndroid::connectToService called on busy socket";
+ errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
+ q->setSocketError(QBluetoothSocket::OperationError);
+ return;
+ }
+
+ if (!ensureNativeSocket(service.socketProtocol())) {
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+ connectToServiceHelper(service.device().address(), service.serviceUuid(), openMode);
+}
+
+void QBluetoothSocketPrivateAndroid::connectToService(
+ const QBluetoothAddress &address, const QBluetoothUuid &uuid,
+ QIODevice::OpenMode openMode)
+{
+ Q_Q(QBluetoothSocket);
+
+ if (q->state() != QBluetoothSocket::UnconnectedState) {
+ qCWarning(QT_BT_ANDROID) << "QBluetoothSocketPrivateAndroid::connectToService called on busy socket";
+ errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
+ q->setSocketError(QBluetoothSocket::OperationError);
+ return;
+ }
+
+ if (q->socketType() == QBluetoothServiceInfo::UnknownProtocol) {
+ qCWarning(QT_BT_ANDROID) << "QBluetoothSocketPrivateAndroid::connectToService cannot "
+ "connect with 'UnknownProtocol' (type provided by given service)";
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
+ if (!ensureNativeSocket(q->socketType())) {
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+ connectToServiceHelper(address, uuid, openMode);
+}
+
+void QBluetoothSocketPrivateAndroid::connectToService(
+ const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode)
+{
+ Q_UNUSED(port);
+ Q_UNUSED(openMode);
+ Q_UNUSED(address);
+
+ Q_Q(QBluetoothSocket);
+
+ errorString = tr("Connecting to port is not supported");
+ q->setSocketError(QBluetoothSocket::ServiceNotFoundError);
+ qCWarning(QT_BT_ANDROID) << "Connecting to port is not supported";
+}
+
void QBluetoothSocketPrivateAndroid::socketConnectSuccess(const QAndroidJniObject &socket)
{
Q_Q(QBluetoothSocket);
diff --git a/src/bluetooth/qbluetoothsocket_android_p.h b/src/bluetooth/qbluetoothsocket_android_p.h
index 893b5fe3..14872cde 100644
--- a/src/bluetooth/qbluetoothsocket_android_p.h
+++ b/src/bluetooth/qbluetoothsocket_android_p.h
@@ -74,6 +74,13 @@ public:
void connectToServiceHelper(const QBluetoothAddress &address, const QBluetoothUuid &uuid,
QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothServiceInfo &service,
+ QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid,
+ QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothAddress &address, quint16 port,
+ QIODevice::OpenMode openMode);
+
bool fallBackConnect(QAndroidJniObject uuid, int channel);
bool fallBackReversedConnect(const QBluetoothUuid &uuid);
diff --git a/src/bluetooth/qbluetoothsocket_bluez.cpp b/src/bluetooth/qbluetoothsocket_bluez.cpp
index 0e5181f2..ccf34e7b 100644
--- a/src/bluetooth/qbluetoothsocket_bluez.cpp
+++ b/src/bluetooth/qbluetoothsocket_bluez.cpp
@@ -39,6 +39,7 @@
#include "qbluetoothsocket.h"
#include "qbluetoothsocket_bluez_p.h"
+#include "qbluetoothdeviceinfo.h"
#include "bluez/manager_p.h"
#include "bluez/adapter_p.h"
@@ -206,6 +207,110 @@ void QBluetoothSocketPrivateBluez::connectToServiceHelper(const QBluetoothAddres
}
}
+void QBluetoothSocketPrivateBluez::connectToService(
+ const QBluetoothServiceInfo &service, QIODevice::OpenMode openMode)
+{
+ Q_Q(QBluetoothSocket);
+
+ if (q->state() != QBluetoothSocket::UnconnectedState
+ && q->state() != QBluetoothSocket::ServiceLookupState) {
+ qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService called on busy socket";
+ errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
+ q->setSocketError(QBluetoothSocket::OperationError);
+ return;
+ }
+
+ // we are checking the service protocol and not socketType()
+ // socketType will change in ensureNativeSocket()
+ if (service.socketProtocol() == QBluetoothServiceInfo::UnknownProtocol) {
+ qCWarning(QT_BT_BLUEZ) << "QBluetoothSocket::connectToService cannot "
+ "connect with 'UnknownProtocol' (type provided by given service)";
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
+ if (service.protocolServiceMultiplexer() > 0) {
+ Q_ASSERT(service.socketProtocol() == QBluetoothServiceInfo::L2capProtocol);
+
+ if (!ensureNativeSocket(QBluetoothServiceInfo::L2capProtocol)) {
+ errorString = QBluetoothSocket::tr("Unknown socket error");
+ q->setSocketError(QBluetoothSocket::UnknownSocketError);
+ return;
+ }
+ connectToServiceHelper(service.device().address(), service.protocolServiceMultiplexer(),
+ openMode);
+ } else if (service.serverChannel() > 0) {
+ Q_ASSERT(service.socketProtocol() == QBluetoothServiceInfo::RfcommProtocol);
+
+ if (!ensureNativeSocket(QBluetoothServiceInfo::RfcommProtocol)) {
+ errorString = QBluetoothSocket::tr("Unknown socket error");
+ q->setSocketError(QBluetoothSocket::UnknownSocketError);
+ return;
+ }
+ connectToServiceHelper(service.device().address(), service.serverChannel(), openMode);
+ } else {
+ // try doing service discovery to see if we can find the socket
+ if (service.serviceUuid().isNull()
+ && !service.serviceClassUuids().contains(QBluetoothUuid::SerialPort)) {
+ qCWarning(QT_BT_BLUEZ) << "No port, no PSM, and no UUID provided. Unable to connect";
+ return;
+ }
+ qCDebug(QT_BT_BLUEZ) << "Need a port/psm, doing discovery";
+ q->doDeviceDiscovery(service, openMode);
+ }
+}
+
+void QBluetoothSocketPrivateBluez::connectToService(
+ const QBluetoothAddress &address, const QBluetoothUuid &uuid,
+ QIODevice::OpenMode openMode)
+{
+ Q_Q(QBluetoothSocket);
+
+ if (q->state() != QBluetoothSocket::UnconnectedState) {
+ qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService called on busy socket";
+ errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
+ q->setSocketError(QBluetoothSocket::OperationError);
+ return;
+ }
+
+ if (q->socketType() == QBluetoothServiceInfo::UnknownProtocol) {
+ qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService cannot "
+ "connect with 'UnknownProtocol' (type provided by given service)";
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
+ QBluetoothServiceInfo service;
+ QBluetoothDeviceInfo device(address, QString(), QBluetoothDeviceInfo::MiscellaneousDevice);
+ service.setDevice(device);
+ service.setServiceUuid(uuid);
+ q->doDeviceDiscovery(service, openMode);
+}
+
+void QBluetoothSocketPrivateBluez::connectToService(
+ const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode)
+{
+ Q_Q(QBluetoothSocket);
+
+ if (q->socketType() == QBluetoothServiceInfo::UnknownProtocol) {
+ qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService cannot "
+ "connect with 'UnknownProtocol' (type provided by given service)";
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
+ if (q->state() != QBluetoothSocket::UnconnectedState) {
+ qCWarning(QT_BT_BLUEZ) << "QBluetoothSocketPrivateBluez::connectToService called on busy socket";
+ errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
+ q->setSocketError(QBluetoothSocket::OperationError);
+ return;
+ }
+ connectToServiceHelper(address, port, openMode);
+}
+
void QBluetoothSocketPrivateBluez::_q_writeNotify()
{
Q_Q(QBluetoothSocket);
diff --git a/src/bluetooth/qbluetoothsocket_bluez_p.h b/src/bluetooth/qbluetoothsocket_bluez_p.h
index 3d178f85..9aaa56b3 100644
--- a/src/bluetooth/qbluetoothsocket_bluez_p.h
+++ b/src/bluetooth/qbluetoothsocket_bluez_p.h
@@ -67,6 +67,13 @@ public:
quint16 port,
QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothServiceInfo &service,
+ QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid,
+ QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothAddress &address, quint16 port,
+ QIODevice::OpenMode openMode);
+
bool ensureNativeSocket(QBluetoothServiceInfo::Protocol type);
QString localName() const;
diff --git a/src/bluetooth/qbluetoothsocket_bluezdbus.cpp b/src/bluetooth/qbluetoothsocket_bluezdbus.cpp
index 17408a9f..bdcc89ef 100644
--- a/src/bluetooth/qbluetoothsocket_bluezdbus.cpp
+++ b/src/bluetooth/qbluetoothsocket_bluezdbus.cpp
@@ -63,6 +63,29 @@ void QBluetoothSocketPrivateBluezDBus::connectToServiceHelper(const QBluetoothAd
Q_UNUSED(port);
}
+void QBluetoothSocketPrivateBluezDBus::connectToService(
+ const QBluetoothServiceInfo &service, QIODevice::OpenMode openMode)
+{
+ Q_UNUSED(openMode);
+ Q_UNUSED(service);
+}
+
+void QBluetoothSocketPrivateBluezDBus::connectToService(
+ const QBluetoothAddress &address, const QBluetoothUuid &uuid, QIODevice::OpenMode openMode)
+{
+ Q_UNUSED(openMode);
+ Q_UNUSED(address);
+ Q_UNUSED(uuid);
+}
+
+void QBluetoothSocketPrivateBluezDBus::connectToService(
+ const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode)
+{
+ Q_UNUSED(openMode);
+ Q_UNUSED(address);
+ Q_UNUSED(port);
+}
+
void QBluetoothSocketPrivateBluezDBus::abort()
{
}
diff --git a/src/bluetooth/qbluetoothsocket_bluezdbus_p.h b/src/bluetooth/qbluetoothsocket_bluezdbus_p.h
index 6a91d25b..3390566d 100644
--- a/src/bluetooth/qbluetoothsocket_bluezdbus_p.h
+++ b/src/bluetooth/qbluetoothsocket_bluezdbus_p.h
@@ -67,6 +67,13 @@ public:
quint16 port,
QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothServiceInfo &service,
+ QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid,
+ QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothAddress &address, quint16 port,
+ QIODevice::OpenMode openMode);
+
bool ensureNativeSocket(QBluetoothServiceInfo::Protocol type);
QString localName() const;
diff --git a/src/bluetooth/qbluetoothsocket_p.cpp b/src/bluetooth/qbluetoothsocket_p.cpp
index 6781b793..22f1655b 100644
--- a/src/bluetooth/qbluetoothsocket_p.cpp
+++ b/src/bluetooth/qbluetoothsocket_p.cpp
@@ -70,6 +70,47 @@ void QBluetoothSocketPrivateDummy::connectToServiceHelper(const QBluetoothAddres
Q_UNUSED(port);
}
+void QBluetoothSocketPrivateDummy::connectToService(
+ const QBluetoothServiceInfo &service, QIODevice::OpenMode openMode)
+{
+ Q_UNUSED(service);
+ Q_UNUSED(openMode);
+
+ Q_Q(QBluetoothSocket);
+
+ qWarning() << "Using non-functional QBluetoothSocketPrivateDummy";
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+}
+
+void QBluetoothSocketPrivateDummy::connectToService(
+ const QBluetoothAddress &address, const QBluetoothUuid &uuid, QIODevice::OpenMode openMode)
+{
+ Q_UNUSED(address);
+ Q_UNUSED(uuid);
+ Q_UNUSED(openMode);
+
+ Q_Q(QBluetoothSocket);
+
+ qWarning() << "Using non-functional QBluetoothSocketPrivateDummy";
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+}
+
+void QBluetoothSocketPrivateDummy::connectToService(
+ const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode)
+{
+ Q_UNUSED(address);
+ Q_UNUSED(port);
+ Q_UNUSED(openMode);
+
+ Q_Q(QBluetoothSocket);
+
+ qWarning() << "Using non-functional QBluetoothSocketPrivateDummy";
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+}
+
void QBluetoothSocketPrivateDummy::abort()
{
}
diff --git a/src/bluetooth/qbluetoothsocket_p.h b/src/bluetooth/qbluetoothsocket_p.h
index 81e540b7..f3f46d26 100644
--- a/src/bluetooth/qbluetoothsocket_p.h
+++ b/src/bluetooth/qbluetoothsocket_p.h
@@ -69,6 +69,14 @@ public:
void connectToServiceHelper(const QBluetoothAddress &address,
quint16 port,
QIODevice::OpenMode openMode);
+
+ void connectToService(const QBluetoothServiceInfo &service,
+ QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid,
+ QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothAddress &address, quint16 port,
+ QIODevice::OpenMode openMode);
+
bool ensureNativeSocket(QBluetoothServiceInfo::Protocol type);
QString localName() const;
diff --git a/src/bluetooth/qbluetoothsocket_winrt.cpp b/src/bluetooth/qbluetoothsocket_winrt.cpp
index 23d6d509..556d9fc5 100644
--- a/src/bluetooth/qbluetoothsocket_winrt.cpp
+++ b/src/bluetooth/qbluetoothsocket_winrt.cpp
@@ -47,6 +47,8 @@
#include <private/qeventdispatcher_winrt_p.h>
#include <QtBluetooth/QBluetoothLocalDevice>
+#include <QtBluetooth/qbluetoothdeviceinfo.h>
+#include <QtBluetooth/qbluetoothserviceinfo.h>
#include <QtCore/qloggingcategory.h>
#include <robuffer.h>
@@ -401,6 +403,103 @@ void QBluetoothSocketPrivateWinRT::connectToServiceHelper(const QBluetoothAddres
});
}
+void QBluetoothSocketPrivateWinRT::connectToService(
+ const QBluetoothServiceInfo &service, QIODevice::OpenMode openMode)
+{
+ Q_Q(QBluetoothSocket);
+
+ if (q->state() != QBluetoothSocket::UnconnectedState
+ && q->state() != QBluetoothSocket::ServiceLookupState) {
+ qCWarning(QT_BT_WINRT) << "QBluetoothSocket::connectToService called on busy socket";
+ errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
+ q->setSocketError(QBluetoothSocket::OperationError);
+ return;
+ }
+
+ // we are checking the service protocol and not socketType()
+ // socketType will change in ensureNativeSocket()
+ if (service.socketProtocol() != QBluetoothServiceInfo::RfcommProtocol) {
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
+ if (service.protocolServiceMultiplexer() > 0) {
+ Q_ASSERT(service.socketProtocol() == QBluetoothServiceInfo::L2capProtocol);
+
+ if (!ensureNativeSocket(QBluetoothServiceInfo::L2capProtocol)) {
+ errorString = QBluetoothSocket::tr("Unknown socket error");
+ q->setSocketError(QBluetoothSocket::UnknownSocketError);
+ return;
+ }
+ connectToServiceHelper(service.device().address(), service.protocolServiceMultiplexer(), openMode);
+ } else if (service.serverChannel() > 0) {
+ Q_ASSERT(service.socketProtocol() == QBluetoothServiceInfo::RfcommProtocol);
+
+ if (!ensureNativeSocket(QBluetoothServiceInfo::RfcommProtocol)) {
+ errorString = QBluetoothSocket::tr("Unknown socket error");
+ q->setSocketError(QBluetoothSocket::UnknownSocketError);
+ return;
+ }
+ connectToServiceHelper(service.device().address(), service.serverChannel(), openMode);
+ } else {
+ // try doing service discovery to see if we can find the socket
+ if (service.serviceUuid().isNull()
+ && !service.serviceClassUuids().contains(QBluetoothUuid::SerialPort)) {
+ qCWarning(QT_BT_WINRT) << "No port, no PSM, and no UUID provided. Unable to connect";
+ return;
+ }
+ qCDebug(QT_BT_WINRT) << "Need a port/psm, doing discovery";
+ q->doDeviceDiscovery(service, openMode);
+ }
+}
+
+void QBluetoothSocketPrivateWinRT::connectToService(
+ const QBluetoothAddress &address, const QBluetoothUuid &uuid, QIODevice::OpenMode openMode)
+{
+ Q_Q(QBluetoothSocket);
+
+ if (q->state() != QBluetoothSocket::UnconnectedState) {
+ qCWarning(QT_BT_WINRT) << "QBluetoothSocketPrivateWinRT::connectToService called on busy socket";
+ errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
+ q->setSocketError(QBluetoothSocket::OperationError);
+ return;
+ }
+
+ if (q->socketType() != QBluetoothServiceInfo::RfcommProtocol) {
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
+ QBluetoothServiceInfo service;
+ QBluetoothDeviceInfo device(address, QString(), QBluetoothDeviceInfo::MiscellaneousDevice);
+ service.setDevice(device);
+ service.setServiceUuid(uuid);
+ q->doDeviceDiscovery(service, openMode);
+}
+
+void QBluetoothSocketPrivateWinRT::connectToService(
+ const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode)
+{
+ Q_Q(QBluetoothSocket);
+
+ if (q->state() != QBluetoothSocket::UnconnectedState) {
+ qCWarning(QT_BT_WINRT) << "QBluetoothSocketPrivateWinRT::connectToService called on busy socket";
+ errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
+ q->setSocketError(QBluetoothSocket::OperationError);
+ return;
+ }
+
+ if (q->socketType() != QBluetoothServiceInfo::RfcommProtocol) {
+ errorString = QBluetoothSocket::tr("Socket type not supported");
+ q->setSocketError(QBluetoothSocket::UnsupportedProtocolError);
+ return;
+ }
+
+ connectToServiceHelper(address, port, openMode);
+}
+
void QBluetoothSocketPrivateWinRT::abort()
{
Q_Q(QBluetoothSocket);
diff --git a/src/bluetooth/qbluetoothsocket_winrt_p.h b/src/bluetooth/qbluetoothsocket_winrt_p.h
index 76c2ded9..17f0fa0d 100644
--- a/src/bluetooth/qbluetoothsocket_winrt_p.h
+++ b/src/bluetooth/qbluetoothsocket_winrt_p.h
@@ -71,6 +71,14 @@ public:
void connectToServiceHelper(const QBluetoothAddress &address,
quint16 port,
QIODevice::OpenMode openMode);
+
+ void connectToService(const QBluetoothServiceInfo &service,
+ QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid,
+ QIODevice::OpenMode openMode);
+ void connectToService(const QBluetoothAddress &address, quint16 port,
+ QIODevice::OpenMode openMode);
+
bool ensureNativeSocket(QBluetoothServiceInfo::Protocol type);
QString localName() const;
diff --git a/src/bluetooth/qbluetoothsocketbase_p.h b/src/bluetooth/qbluetoothsocketbase_p.h
index c223350d..410dcbbd 100644
--- a/src/bluetooth/qbluetoothsocketbase_p.h
+++ b/src/bluetooth/qbluetoothsocketbase_p.h
@@ -122,6 +122,7 @@ public:
QBluetoothSocket::SocketState socketState = QBluetoothSocket::ConnectedState,
QBluetoothSocket::OpenMode openMode = QBluetoothSocket::ReadWrite) = 0;
+
#if defined(QT_ANDROID_BLUETOOTH)
virtual void connectToServiceHelper(const QBluetoothAddress &address, const QBluetoothUuid &uuid,
QIODevice::OpenMode openMode) = 0;
@@ -129,6 +130,12 @@ public:
virtual void connectToServiceHelper(const QBluetoothAddress &address, quint16 port,
QIODevice::OpenMode openMode) = 0;
#endif
+ virtual void connectToService(const QBluetoothServiceInfo &service,
+ QIODevice::OpenMode openMode) = 0;
+ virtual void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid,
+ QIODevice::OpenMode openMode) = 0;
+ virtual void connectToService(const QBluetoothAddress &address, quint16 port,
+ QIODevice::OpenMode openMode) = 0;
#ifdef QT_ANDROID_BLUETOOTH
virtual bool setSocketDescriptor(const QAndroidJniObject &socket, QBluetoothServiceInfo::Protocol socketType,