summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2014-11-19 20:01:15 +0300
committerTimur Pocheptsov <Timur.Pocheptsov@digia.com>2014-12-01 15:17:22 +0100
commit11095f3504515f0ec2a754f64f97719340247505 (patch)
tree74c64183fbb5cd6457bd956da574585c35b8ab9e /src
parenta3c295297a4ee1365619cc49f504a9f35b4f7785 (diff)
Refactor of code for "classic" part on Windows
The previous code is based on the assumption where we can use each of local radio separatelly to discovery of remote devices. Windows API allows to use a handle of separately local bluetooth adapter to operate with its power, to start/stop detection of remote devices through it and so on. But in this API there is no opportunity to enumerate services and attributes of the given remote device via the concrete local adapter. Therefore now to discovery of remote devices are used all local bluetooth adapters in system. Also, the power and the host modes management now are belongs to all local adapters at once. Tested on Windows 8 with the USB CSR8510 A10 adapter Change-Id: I949b112158a575f5b563a78163c1e3990c952ada Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r--src/bluetooth/bluetooth.pro2
-rw-r--r--src/bluetooth/qbluetoothdevicediscoveryagent.h4
-rw-r--r--src/bluetooth/qbluetoothdevicediscoveryagent_p.h34
-rw-r--r--src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp209
-rw-r--r--src/bluetooth/qbluetoothlocaldevice_p.h24
-rw-r--r--src/bluetooth/qbluetoothlocaldevice_win.cpp166
-rw-r--r--src/bluetooth/windows/qwinclassicbluetooth.cpp136
-rw-r--r--src/bluetooth/windows/qwinclassicbluetooth_p.h77
-rw-r--r--src/bluetooth/windows/windows.pri5
9 files changed, 411 insertions, 246 deletions
diff --git a/src/bluetooth/bluetooth.pro b/src/bluetooth/bluetooth.pro
index afa0b9fa..fe00eec0 100644
--- a/src/bluetooth/bluetooth.pro
+++ b/src/bluetooth/bluetooth.pro
@@ -198,6 +198,8 @@ config_bluez:qtHaveModule(dbus) {
SOURCES -= qbluetoothdevicediscoveryagent.cpp
} else:win32:!winrt:!wince {
+ include(windows/windows.pri)
+
SOURCES += \
qbluetoothdevicediscoveryagent_win.cpp \
qbluetoothlocaldevice_win.cpp \
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent.h b/src/bluetooth/qbluetoothdevicediscoveryagent.h
index e9c0a4ed..8a375096 100644
--- a/src/bluetooth/qbluetoothdevicediscoveryagent.h
+++ b/src/bluetooth/qbluetoothdevicediscoveryagent.h
@@ -107,10 +107,6 @@ private:
Q_PRIVATE_SLOT(d_func(), void _q_PropertiesChanged(const QString &interface, const QVariantMap &changed_properties, const QStringList &invalidated_properties))
Q_PRIVATE_SLOT(d_func(), void _q_extendedDeviceDiscoveryTimeout())
#endif
-
-#ifdef Q_OS_WIN32
- Q_PRIVATE_SLOT(d_func(), void _q_handleFindResult())
-#endif
};
QT_END_NAMESPACE
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_p.h b/src/bluetooth/qbluetoothdevicediscoveryagent_p.h
index f224fe41..096f013c 100644
--- a/src/bluetooth/qbluetoothdevicediscoveryagent_p.h
+++ b/src/bluetooth/qbluetoothdevicediscoveryagent_p.h
@@ -78,20 +78,16 @@ QT_END_NAMESPACE
#ifdef Q_OS_WIN32
#include <QtConcurrent>
-#include "qbluetoothlocaldevice_p.h"
-#include <bluetoothapis.h>
+#include "windows/qwinclassicbluetooth_p.h"
#endif
QT_BEGIN_NAMESPACE
class QBluetoothDeviceDiscoveryAgentPrivate
-#if defined(QT_QNX_BLUETOOTH) || defined(QT_ANDROID_BLUETOOTH)
+#if defined(QT_QNX_BLUETOOTH) || defined(QT_ANDROID_BLUETOOTH) || defined(Q_OS_WIN32)
: public QObject
{
Q_OBJECT
-#elif defined(Q_OS_WIN32)
- : public QBluetoothLocalDevicePrivateData
-{
#else
{
#endif
@@ -119,10 +115,6 @@ public:
void _q_extendedDeviceDiscoveryTimeout();
#endif
-#ifdef Q_OS_WIN32
- void _q_handleFindResult();
-#endif
-
private:
QList<QBluetoothDeviceInfo> discoveredDevices;
QBluetoothDeviceDiscoveryAgent::InquiryType inquiryType;
@@ -191,17 +183,25 @@ private:
#endif
#ifdef Q_OS_WIN32
- void processDiscoveredDevices(const BLUETOOTH_DEVICE_INFO &info);
- void handleErrors(DWORD errorCode);
- bool isRunning() const;
+private slots:
+ void classicDeviceDiscovered();
+
+private:
+ void initialize(const QBluetoothAddress &deviceAdapter);
+
+ bool isClassicAdapterValid(const QBluetoothAddress &deviceAdapter);
+ void startDiscoveryForFirstClassicDevice();
+ void startDiscoveryForNextClassicDevice(HBLUETOOTH_DEVICE_FIND hSearch);
+ void completeClassicDiscovery(HBLUETOOTH_DEVICE_FIND hSearch);
+ void acceptDiscoveredClassicDevice(const BLUETOOTH_DEVICE_INFO &device);
- static QVariant findFirstDevice(HANDLE radioHandle);
- static QVariant findNextDevice(HBLUETOOTH_DEVICE_FIND findHandle);
- static void findClose(HBLUETOOTH_DEVICE_FIND findHandle);
+ void setError(DWORD error, const QString &str = QString());
- QFutureWatcher<QVariant> *findWatcher;
+ QFutureWatcher<WinClassicBluetooth::RemoteDeviceDiscoveryResult> *classicDiscoveryWatcher;
bool pendingCancel;
bool pendingStart;
+ bool isClassicActive;
+ bool isClassicValid;
#endif
QBluetoothDeviceDiscoveryAgent *q_ptr;
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp
index bdfb0237..53e4d5aa 100644
--- a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp
+++ b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp
@@ -45,45 +45,30 @@
#include "qbluetoothaddress.h"
#include "qbluetoothuuid.h"
-#define QT_DEVICEDISCOVERY_DEBUG
-
QT_BEGIN_NAMESPACE
-struct NativeFindResult
-{
- NativeFindResult();
-
- BLUETOOTH_DEVICE_INFO deviceInfo;
- HBLUETOOTH_DEVICE_FIND findHandle;
- DWORD errorCode;
-};
-
-NativeFindResult::NativeFindResult()
- : findHandle(NULL)
- , errorCode(ERROR_SUCCESS)
-{
- ::ZeroMemory(&deviceInfo, sizeof(deviceInfo));
- deviceInfo.dwSize = sizeof(deviceInfo);
-}
+Q_DECLARE_LOGGING_CATEGORY(QT_BT_WINDOWS)
QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate(
const QBluetoothAddress &deviceAdapter,
QBluetoothDeviceDiscoveryAgent *parent)
- : QBluetoothLocalDevicePrivateData(deviceAdapter)
- , inquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry)
+ : inquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry)
, lastError(QBluetoothDeviceDiscoveryAgent::NoError)
- , findWatcher(0)
+ , classicDiscoveryWatcher(0)
, pendingCancel(false)
, pendingStart(false)
+ , isClassicActive(false)
+ , isClassicValid(false)
, q_ptr(parent)
{
+ initialize(deviceAdapter);
}
QBluetoothDeviceDiscoveryAgentPrivate::~QBluetoothDeviceDiscoveryAgentPrivate()
{
- if (isRunning()) {
+ if (isClassicActive) {
stop();
- findWatcher->waitForFinished();
+ classicDiscoveryWatcher->waitForFinished();
}
}
@@ -94,12 +79,16 @@ bool QBluetoothDeviceDiscoveryAgentPrivate::isActive() const
if (pendingCancel)
return false;
- return isRunning();
+ return isClassicActive;
}
void QBluetoothDeviceDiscoveryAgentPrivate::start()
{
- Q_Q(QBluetoothDeviceDiscoveryAgent);
+ if (!isClassicValid) {
+ setError(ERROR_INVALID_HANDLE,
+ QBluetoothDeviceDiscoveryAgent::tr("Passed address is not a local device."));
+ return;
+ }
if (pendingCancel == true) {
pendingStart = true;
@@ -108,40 +97,27 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start()
discoveredDevices.clear();
- if (isValid() == false) {
- lastError = QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError;
- errorString = qt_error_string(ERROR_INVALID_HANDLE);
- emit q->error(lastError);
- return;
- }
-
- if (!findWatcher) {
- findWatcher = new QFutureWatcher<QVariant>(q);
- QObject::connect(findWatcher, SIGNAL(finished()), q, SLOT(_q_handleFindResult()));
- }
-
- const QFuture<QVariant> future = QtConcurrent::run(findFirstDevice, deviceHandle);
- findWatcher->setFuture(future);
+ startDiscoveryForFirstClassicDevice();
}
void QBluetoothDeviceDiscoveryAgentPrivate::stop()
{
- if (!isRunning())
+ if (!isClassicActive)
return;
pendingCancel = true;
pendingStart = false;
}
-void QBluetoothDeviceDiscoveryAgentPrivate::_q_handleFindResult()
+void QBluetoothDeviceDiscoveryAgentPrivate::classicDeviceDiscovered()
{
Q_Q(QBluetoothDeviceDiscoveryAgent);
- const QVariant result = findWatcher->result();
- const NativeFindResult nativeFindResult = result.value<NativeFindResult>();
+ const WinClassicBluetooth::RemoteDeviceDiscoveryResult result =
+ classicDiscoveryWatcher->result();
- if (nativeFindResult.errorCode == ERROR_SUCCESS
- || nativeFindResult.errorCode == ERROR_NO_MORE_ITEMS) {
+ if (result.error == ERROR_SUCCESS
+ || result.error == ERROR_NO_MORE_ITEMS) {
if (pendingCancel && !pendingStart) {
emit q->canceled();
@@ -151,101 +127,116 @@ void QBluetoothDeviceDiscoveryAgentPrivate::_q_handleFindResult()
pendingStart = false;
start();
} else {
- if (nativeFindResult.errorCode == ERROR_NO_MORE_ITEMS) {
- emit q->finished();
- } else {
- processDiscoveredDevices(nativeFindResult.deviceInfo);
- const QFuture<QVariant> future =
- QtConcurrent::run(findNextDevice, nativeFindResult.findHandle);
- findWatcher->setFuture(future);
+ if (result.error != ERROR_NO_MORE_ITEMS) {
+ acceptDiscoveredClassicDevice(result.device);
+ startDiscoveryForNextClassicDevice(result.hSearch);
return;
}
}
} else {
- handleErrors(nativeFindResult.errorCode);
+ setError(result.error);
pendingCancel = false;
pendingStart = false;
}
- findClose(nativeFindResult.findHandle);
+ completeClassicDiscovery(result.hSearch);
}
-void QBluetoothDeviceDiscoveryAgentPrivate::processDiscoveredDevices(
- const BLUETOOTH_DEVICE_INFO &info)
+void QBluetoothDeviceDiscoveryAgentPrivate::initialize(
+ const QBluetoothAddress &deviceAdapter)
{
- Q_Q(QBluetoothDeviceDiscoveryAgent);
+ isClassicValid = isClassicAdapterValid(deviceAdapter);
+}
- QBluetoothDeviceInfo deviceInfo(
- QBluetoothAddress(info.Address.ullLong),
- QString::fromWCharArray(info.szName),
- info.ulClassofDevice);
+bool QBluetoothDeviceDiscoveryAgentPrivate::isClassicAdapterValid(
+ const QBluetoothAddress &deviceAdapter)
+{
+ const WinClassicBluetooth::LocalRadiosDiscoveryResult result =
+ WinClassicBluetooth::enumerateLocalRadios();
- if (info.fRemembered)
- deviceInfo.setCached(true);
+ if (result.error != NO_ERROR
+ && result.error != ERROR_NO_MORE_ITEMS) {
+ qCWarning(QT_BT_WINDOWS) << "Occurred error during search of classic local radios";
+ return false;
+ } else if (result.radios.isEmpty()) {
+ qCWarning(QT_BT_WINDOWS) << "No any classic local radio found";
+ return false;
+ }
- discoveredDevices.append(deviceInfo);
- emit q->deviceDiscovered(deviceInfo);
+ foreach (const BLUETOOTH_RADIO_INFO &radio, result.radios) {
+ if (deviceAdapter == QBluetoothAddress()
+ || deviceAdapter == QBluetoothAddress(radio.address.ullLong)) {
+ return true;
+ }
+ }
+
+ qCWarning(QT_BT_WINDOWS) << "No matching for classic local radio:" << deviceAdapter;
+ return false;
}
-void QBluetoothDeviceDiscoveryAgentPrivate::handleErrors(DWORD errorCode)
+void QBluetoothDeviceDiscoveryAgentPrivate::startDiscoveryForFirstClassicDevice()
{
- Q_Q(QBluetoothDeviceDiscoveryAgent);
+ isClassicActive = true;
- lastError = (errorCode == ERROR_INVALID_HANDLE) ?
- QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError
- : QBluetoothDeviceDiscoveryAgent::InputOutputError;
- errorString = qt_error_string(errorCode);
- emit q->error(lastError);
+ if (!classicDiscoveryWatcher) {
+ classicDiscoveryWatcher = new QFutureWatcher<
+ WinClassicBluetooth::RemoteDeviceDiscoveryResult>(this);
+ QObject::connect(classicDiscoveryWatcher, SIGNAL(finished()),
+ this, SLOT(classicDeviceDiscovered()));
+ }
+
+ const QFuture<WinClassicBluetooth::RemoteDeviceDiscoveryResult> future =
+ QtConcurrent::run(WinClassicBluetooth::startDiscoveryOfFirstRemoteDevice);
+ classicDiscoveryWatcher->setFuture(future);
}
-bool QBluetoothDeviceDiscoveryAgentPrivate::isRunning() const
+void QBluetoothDeviceDiscoveryAgentPrivate::startDiscoveryForNextClassicDevice(
+ HBLUETOOTH_DEVICE_FIND hSearch)
{
- return findWatcher && findWatcher->isRunning();
+ Q_ASSERT(classicDiscoveryWatcher);
+
+ const QFuture<WinClassicBluetooth::RemoteDeviceDiscoveryResult> future =
+ QtConcurrent::run(WinClassicBluetooth::startDiscoveryOfNextRemoteDevice, hSearch);
+ classicDiscoveryWatcher->setFuture(future);
}
-QVariant QBluetoothDeviceDiscoveryAgentPrivate::findFirstDevice(HANDLE radioHandle)
+void QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDiscovery(
+ HBLUETOOTH_DEVICE_FIND hSearch)
{
- BLUETOOTH_DEVICE_SEARCH_PARAMS searchParams;
- ::ZeroMemory(&searchParams, sizeof(searchParams));
- searchParams.dwSize = sizeof(searchParams);
- searchParams.cTimeoutMultiplier = 10; // 12.8 sec
- searchParams.fIssueInquiry = TRUE;
- searchParams.fReturnAuthenticated = TRUE;
- searchParams.fReturnConnected = TRUE;
- searchParams.fReturnRemembered = TRUE;
- searchParams.fReturnUnknown = TRUE;
- searchParams.hRadio = radioHandle;
-
- NativeFindResult nativeFindResult;
- nativeFindResult.findHandle = ::BluetoothFindFirstDevice(
- &searchParams, &nativeFindResult.deviceInfo);
- if (!nativeFindResult.findHandle)
- nativeFindResult.errorCode = ::GetLastError();
-
- QVariant result;
- result.setValue(nativeFindResult);
- return result;
+ Q_Q(QBluetoothDeviceDiscoveryAgent);
+
+ WinClassicBluetooth::cancelRemoteDevicesDiscovery(hSearch);
+ isClassicActive = false;
+ emit q->finished();
}
-QVariant QBluetoothDeviceDiscoveryAgentPrivate::findNextDevice(HBLUETOOTH_DEVICE_FIND findHandle)
+void QBluetoothDeviceDiscoveryAgentPrivate::acceptDiscoveredClassicDevice(
+ const BLUETOOTH_DEVICE_INFO &device)
{
- NativeFindResult nativeFindResult;
- nativeFindResult.findHandle = findHandle;
- if (!::BluetoothFindNextDevice(findHandle, &nativeFindResult.deviceInfo))
- nativeFindResult.errorCode = ::GetLastError();
-
- QVariant result;
- result.setValue(nativeFindResult);
- return result;
+ Q_Q(QBluetoothDeviceDiscoveryAgent);
+
+ QBluetoothDeviceInfo deviceInfo(
+ QBluetoothAddress(device.Address.ullLong),
+ QString::fromWCharArray(device.szName),
+ device.ulClassofDevice);
+
+ if (device.fRemembered)
+ deviceInfo.setCached(true);
+
+ discoveredDevices.append(deviceInfo);
+ emit q->deviceDiscovered(deviceInfo);
}
-void QBluetoothDeviceDiscoveryAgentPrivate::findClose(HBLUETOOTH_DEVICE_FIND findHandle)
+void QBluetoothDeviceDiscoveryAgentPrivate::setError(DWORD error, const QString &str)
{
- if (findHandle)
- ::BluetoothFindDeviceClose(findHandle);
+ Q_Q(QBluetoothDeviceDiscoveryAgent);
+
+ lastError = (error == ERROR_INVALID_HANDLE) ?
+ QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError
+ : QBluetoothDeviceDiscoveryAgent::InputOutputError;
+ errorString = str.isEmpty() ? qt_error_string(error) : str;
+ emit q->error(lastError);
}
QT_END_NAMESPACE
-
-Q_DECLARE_METATYPE(QT_PREPEND_NAMESPACE(NativeFindResult))
diff --git a/src/bluetooth/qbluetoothlocaldevice_p.h b/src/bluetooth/qbluetoothlocaldevice_p.h
index c490ca85..3ed8ca62 100644
--- a/src/bluetooth/qbluetoothlocaldevice_p.h
+++ b/src/bluetooth/qbluetoothlocaldevice_p.h
@@ -83,7 +83,7 @@ QT_END_NAMESPACE
#endif
#ifdef Q_OS_WIN32
-#include <qt_windows.h>
+#include "windows/qwinclassicbluetooth_p.h"
#endif
QT_BEGIN_NAMESPACE
@@ -245,23 +245,21 @@ private:
#elif defined(Q_OS_WIN32)
-class QBluetoothLocalDevicePrivateData
+class QBluetoothLocalDevicePrivate : public QObject
{
+ Q_OBJECT
+ Q_DECLARE_PUBLIC(QBluetoothLocalDevice)
public:
- QBluetoothLocalDevicePrivateData(const QBluetoothAddress &address);
+ QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q,
+ const QBluetoothAddress &address = QBluetoothAddress());
+
+ ~QBluetoothLocalDevicePrivate();
bool isValid() const;
+ void initialize(const QBluetoothAddress &address);
- HANDLE deviceHandle;
- QString deviceName;
QBluetoothAddress deviceAddress;
-};
-
-class QBluetoothLocalDevicePrivate : public QBluetoothLocalDevicePrivateData
-{
- Q_DECLARE_PUBLIC(QBluetoothLocalDevice)
-public:
- QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q, const QBluetoothAddress &address);
- ~QBluetoothLocalDevicePrivate();
+ QString deviceName;
+ bool deviceValid;
private:
QBluetoothLocalDevice *q_ptr;
diff --git a/src/bluetooth/qbluetoothlocaldevice_win.cpp b/src/bluetooth/qbluetoothlocaldevice_win.cpp
index 476e5c65..a2dd14e6 100644
--- a/src/bluetooth/qbluetoothlocaldevice_win.cpp
+++ b/src/bluetooth/qbluetoothlocaldevice_win.cpp
@@ -47,19 +47,18 @@
#include <QtCore/QLoggingCategory>
-#include <bluetoothapis.h>
-
QT_BEGIN_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(QT_BT_WINDOWS)
QBluetoothLocalDevice::QBluetoothLocalDevice(QObject *parent) :
QObject(parent),
- d_ptr(new QBluetoothLocalDevicePrivate(this, QBluetoothAddress()))
+ d_ptr(new QBluetoothLocalDevicePrivate(this))
{
}
-QBluetoothLocalDevice::QBluetoothLocalDevice(const QBluetoothAddress &address, QObject *parent) :
+QBluetoothLocalDevice::QBluetoothLocalDevice(
+ const QBluetoothAddress &address, QObject *parent) :
QObject(parent),
d_ptr(new QBluetoothLocalDevicePrivate(this, address))
{
@@ -85,10 +84,9 @@ void QBluetoothLocalDevice::powerOn()
setHostMode(HostConnectable);
}
-void QBluetoothLocalDevice::setHostMode(QBluetoothLocalDevice::HostMode requestedMode)
+void QBluetoothLocalDevice::setHostMode(
+ QBluetoothLocalDevice::HostMode requestedMode)
{
- Q_D(QBluetoothLocalDevice);
-
if (!isValid()) {
qCWarning(QT_BT_WINDOWS) << "The local device is not initialized correctly";
return;
@@ -101,39 +99,39 @@ void QBluetoothLocalDevice::setHostMode(QBluetoothLocalDevice::HostMode requeste
return;
if (requestedMode == QBluetoothLocalDevice::HostPoweredOff) {
- if (::BluetoothIsDiscoverable(d->deviceHandle)
- && !::BluetoothEnableDiscovery(d->deviceHandle, FALSE)) {
+ if (::BluetoothIsDiscoverable(NULL)
+ && !::BluetoothEnableDiscovery(NULL, FALSE)) {
qCWarning(QT_BT_WINDOWS) << "Unable to disable the discoverable mode";
emit error(QBluetoothLocalDevice::UnknownError);
return;
}
- if (::BluetoothIsConnectable(d->deviceHandle)
- && !::BluetoothEnableIncomingConnections(d->deviceHandle, FALSE)) {
+ if (::BluetoothIsConnectable(NULL)
+ && !::BluetoothEnableIncomingConnections(NULL, FALSE)) {
qCWarning(QT_BT_WINDOWS) << "Unable to disable the connectable mode";
emit error(QBluetoothLocalDevice::UnknownError);
return;
}
} else if (requestedMode == QBluetoothLocalDevice::HostConnectable) {
- if (::BluetoothIsDiscoverable(d->deviceHandle)) {
- if (!::BluetoothEnableDiscovery(d->deviceHandle, FALSE)) {
+ if (::BluetoothIsDiscoverable(NULL)) {
+ if (!::BluetoothEnableDiscovery(NULL, FALSE)) {
qCWarning(QT_BT_WINDOWS) << "Unable to disable the discoverable mode";
emit error(QBluetoothLocalDevice::UnknownError);
return;
}
- } else if (!::BluetoothEnableIncomingConnections(d->deviceHandle, TRUE)) {
+ } else if (!::BluetoothEnableIncomingConnections(NULL, TRUE)) {
qCWarning(QT_BT_WINDOWS) << "Unable to enable the connectable mode";
emit error(QBluetoothLocalDevice::UnknownError);
return;
}
} else if (requestedMode == QBluetoothLocalDevice::HostDiscoverable
|| requestedMode == QBluetoothLocalDevice::HostDiscoverableLimitedInquiry) {
- if (!::BluetoothIsConnectable(d->deviceHandle)
- && !::BluetoothEnableIncomingConnections(d->deviceHandle, TRUE)) {
+ if (!::BluetoothIsConnectable(NULL)
+ && !::BluetoothEnableIncomingConnections(NULL, TRUE)) {
qCWarning(QT_BT_WINDOWS) << "Unable to enable the connectable mode";
emit error(QBluetoothLocalDevice::UnknownError);
return;
}
- if (!::BluetoothEnableDiscovery(d->deviceHandle, TRUE)) {
+ if (!::BluetoothEnableDiscovery(NULL, TRUE)) {
qCWarning(QT_BT_WINDOWS) << "Unable to enable the discoverable mode";
emit error(QBluetoothLocalDevice::UnknownError);
return;
@@ -145,16 +143,14 @@ void QBluetoothLocalDevice::setHostMode(QBluetoothLocalDevice::HostMode requeste
QBluetoothLocalDevice::HostMode QBluetoothLocalDevice::hostMode() const
{
- Q_D(const QBluetoothLocalDevice);
-
if (!isValid()) {
qCWarning(QT_BT_WINDOWS) << "The local device is not initialized correctly";
return HostPoweredOff;
}
- if (::BluetoothIsDiscoverable(d->deviceHandle))
+ if (::BluetoothIsDiscoverable(NULL))
return HostDiscoverable;
- if (::BluetoothIsConnectable(d->deviceHandle))
+ if (::BluetoothIsConnectable(NULL))
return HostConnectable;
return HostPoweredOff;
}
@@ -166,40 +162,17 @@ QList<QBluetoothAddress> QBluetoothLocalDevice::connectedDevices() const
QList<QBluetoothHostInfo> QBluetoothLocalDevice::allDevices()
{
- BLUETOOTH_FIND_RADIO_PARAMS findRadioParams;
- ::ZeroMemory(&findRadioParams, sizeof(findRadioParams));
- findRadioParams.dwSize = sizeof(findRadioParams);
-
- HANDLE radioHandle = NULL;
- const HBLUETOOTH_RADIO_FIND radioFindHandle = ::BluetoothFindFirstRadio(&findRadioParams,
- &radioHandle);
- if (!radioFindHandle)
- return QList<QBluetoothHostInfo>();
-
- QList<QBluetoothHostInfo> localDevices;
-
- forever {
- BLUETOOTH_RADIO_INFO radioInfo;
- ::ZeroMemory(&radioInfo, sizeof(radioInfo));
- radioInfo.dwSize = sizeof(radioInfo);
+ const WinClassicBluetooth::LocalRadiosDiscoveryResult result =
+ WinClassicBluetooth::enumerateLocalRadios();
- const DWORD retval = ::BluetoothGetRadioInfo(radioHandle, &radioInfo);
- ::CloseHandle(radioHandle);
-
- if (retval != ERROR_SUCCESS)
- break;
-
- QBluetoothHostInfo localDevice;
- localDevice.setAddress(QBluetoothAddress(radioInfo.address.ullLong));
- localDevice.setName(QString::fromWCharArray(radioInfo.szName));
- localDevices.append(localDevice);
-
- if (!::BluetoothFindNextRadio(radioFindHandle, &radioHandle))
- break;
+ QList<QBluetoothHostInfo> devices;
+ foreach (const BLUETOOTH_RADIO_INFO &radio, result.radios) {
+ QBluetoothHostInfo device;
+ device.setAddress(QBluetoothAddress(radio.address.ullLong));
+ device.setName(QString::fromWCharArray(radio.szName));
+ devices.append(device);
}
-
- ::BluetoothFindRadioClose(radioFindHandle);
- return localDevices;
+ return devices;
}
void QBluetoothLocalDevice::requestPairing(const QBluetoothAddress &address, Pairing pairing)
@@ -220,72 +193,59 @@ void QBluetoothLocalDevice::pairingConfirmation(bool confirmation)
Q_UNUSED(confirmation);
}
-QBluetoothLocalDevicePrivate::QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q,
- const QBluetoothAddress &address)
- : QBluetoothLocalDevicePrivateData(address)
+QBluetoothLocalDevicePrivate::QBluetoothLocalDevicePrivate(
+ QBluetoothLocalDevice *q, const QBluetoothAddress &address)
+ : deviceValid(false)
, q_ptr(q)
{
+ initialize(address);
}
QBluetoothLocalDevicePrivate::~QBluetoothLocalDevicePrivate()
{
- if (isValid())
- ::CloseHandle(deviceHandle);
}
-QBluetoothLocalDevicePrivateData::QBluetoothLocalDevicePrivateData(
- const QBluetoothAddress &address)
- : deviceHandle(INVALID_HANDLE_VALUE)
+bool QBluetoothLocalDevicePrivate::isValid() const
{
- BLUETOOTH_FIND_RADIO_PARAMS findRadioParams;
- ::ZeroMemory(&findRadioParams, sizeof(findRadioParams));
- findRadioParams.dwSize = sizeof(findRadioParams);
-
- HANDLE radioHandle = NULL;
- const HBLUETOOTH_RADIO_FIND radioFindHandle = ::BluetoothFindFirstRadio(&findRadioParams,
- &radioHandle);
- if (!radioFindHandle) {
- qCWarning(QT_BT_WINDOWS) << qt_error_string(::GetLastError());
- return;
- }
+ return deviceValid;
+}
- forever {
- BLUETOOTH_RADIO_INFO radioInfo;
- ::ZeroMemory(&radioInfo, sizeof(radioInfo));
- radioInfo.dwSize = sizeof(radioInfo);
+void QBluetoothLocalDevicePrivate::initialize(const QBluetoothAddress &address)
+{
+ Q_Q(QBluetoothLocalDevice);
- const DWORD retval = ::BluetoothGetRadioInfo(radioHandle, &radioInfo);
- if (retval != ERROR_SUCCESS) {
- ::CloseHandle(radioHandle);
- qCWarning(QT_BT_WINDOWS) << qt_error_string(retval);
- break;
- }
+ const WinClassicBluetooth::LocalRadiosDiscoveryResult result =
+ WinClassicBluetooth::enumerateLocalRadios();
- if (address.isNull() || (address == QBluetoothAddress(radioInfo.address.ullLong))) {
- deviceAddress = QBluetoothAddress(radioInfo.address.ullLong);
- deviceName = QString::fromWCharArray(radioInfo.szName);
- deviceHandle = radioHandle;
- break;
- }
-
- ::CloseHandle(radioHandle);
+ if (result.error != NO_ERROR
+ && result.error != ERROR_NO_MORE_ITEMS) {
+ qCWarning(QT_BT_WINDOWS) << qt_error_string(result.error);
+ QMetaObject::invokeMethod(q, "error", Qt::QueuedConnection,
+ Q_ARG(QBluetoothLocalDevice::Error,
+ QBluetoothLocalDevice::UnknownError));
+ return;
+ } else if (result.radios.isEmpty()) {
+ qCWarning(QT_BT_WINDOWS) << "No any classic local radio found";
+ QMetaObject::invokeMethod(q, "error", Qt::QueuedConnection,
+ Q_ARG(QBluetoothLocalDevice::Error,
+ QBluetoothLocalDevice::UnknownError));
+ return;
+ }
- if (!::BluetoothFindNextRadio(radioFindHandle, &radioHandle)) {
- const DWORD nativeError = ::GetLastError();
- if (nativeError != ERROR_NO_MORE_ITEMS)
- qCWarning(QT_BT_WINDOWS) << qt_error_string(nativeError);
- break;
+ foreach (const BLUETOOTH_RADIO_INFO &radio, result.radios) {
+ if (address == QBluetoothAddress()
+ || address == QBluetoothAddress(radio.address.ullLong)) {
+ deviceAddress = QBluetoothAddress(radio.address.ullLong);
+ deviceName = QString::fromWCharArray(radio.szName);
+ deviceValid = true;
+ return;
}
}
- if (!::BluetoothFindRadioClose(radioFindHandle))
- qCWarning(QT_BT_WINDOWS) << qt_error_string(::GetLastError());
-}
-
-bool QBluetoothLocalDevicePrivateData::isValid() const
-{
- return deviceHandle && (deviceHandle != INVALID_HANDLE_VALUE);
+ qCWarning(QT_BT_WINDOWS) << "Unable to find classic local radio: " << address;
+ QMetaObject::invokeMethod(q, "error", Qt::QueuedConnection,
+ Q_ARG(QBluetoothLocalDevice::Error,
+ QBluetoothLocalDevice::UnknownError));
}
-
QT_END_NAMESPACE
diff --git a/src/bluetooth/windows/qwinclassicbluetooth.cpp b/src/bluetooth/windows/qwinclassicbluetooth.cpp
new file mode 100644
index 00000000..31212d8e
--- /dev/null
+++ b/src/bluetooth/windows/qwinclassicbluetooth.cpp
@@ -0,0 +1,136 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2014 Denis Shienkov <denis.shienkov@gmail.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtBluetooth module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL21$
+** 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 Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qwinclassicbluetooth_p.h"
+
+QT_BEGIN_NAMESPACE
+
+namespace WinClassicBluetooth {
+
+LocalRadiosDiscoveryResult::LocalRadiosDiscoveryResult()
+ : error(NO_ERROR)
+{
+}
+
+RemoteDeviceDiscoveryResult::RemoteDeviceDiscoveryResult()
+ : hSearch(0)
+ , error(NO_ERROR)
+{
+ ::ZeroMemory(&device, sizeof(device));
+ device.dwSize = sizeof(device);
+}
+
+LocalRadiosDiscoveryResult enumerateLocalRadios()
+{
+ BLUETOOTH_FIND_RADIO_PARAMS params;
+ ::ZeroMemory(&params, sizeof(params));
+ params.dwSize = sizeof(params);
+
+ HANDLE hRadio = 0;
+ const HBLUETOOTH_RADIO_FIND hSearch =
+ ::BluetoothFindFirstRadio(&params, &hRadio);
+
+ LocalRadiosDiscoveryResult result;
+
+ if (!hSearch) {
+ result.error = ::GetLastError();
+ return result;
+ }
+
+ forever {
+ BLUETOOTH_RADIO_INFO radio;
+ ::ZeroMemory(&radio, sizeof(radio));
+ radio.dwSize = sizeof(radio);
+
+ const DWORD retval = ::BluetoothGetRadioInfo(hRadio, &radio);
+ ::CloseHandle(hRadio);
+
+ if (retval != ERROR_SUCCESS) {
+ result.error = ::GetLastError();
+ break;
+ }
+
+ result.radios.append(radio);
+
+ if (!::BluetoothFindNextRadio(hSearch, &hRadio)) {
+ result.error = ::GetLastError();
+ break;
+ }
+ }
+
+ ::BluetoothFindRadioClose(hSearch);
+ return result;
+}
+
+RemoteDeviceDiscoveryResult startDiscoveryOfFirstRemoteDevice()
+{
+ BLUETOOTH_DEVICE_SEARCH_PARAMS searchParams;
+ ::ZeroMemory(&searchParams, sizeof(searchParams));
+ searchParams.dwSize = sizeof(searchParams);
+ searchParams.cTimeoutMultiplier = 10; // 12.8 sec
+ searchParams.fIssueInquiry = TRUE;
+ searchParams.fReturnAuthenticated = TRUE;
+ searchParams.fReturnConnected = TRUE;
+ searchParams.fReturnRemembered = TRUE;
+ searchParams.fReturnUnknown = TRUE;
+ searchParams.hRadio = NULL;
+
+ RemoteDeviceDiscoveryResult result;
+ result.hSearch = ::BluetoothFindFirstDevice(
+ &searchParams, &result.device);
+
+ if (!result.hSearch)
+ result.error = ::GetLastError();
+ return result;
+}
+
+RemoteDeviceDiscoveryResult startDiscoveryOfNextRemoteDevice(
+ HBLUETOOTH_DEVICE_FIND hSearch)
+{
+ RemoteDeviceDiscoveryResult result;
+ result.hSearch = hSearch;
+ if (!::BluetoothFindNextDevice(hSearch, &result.device))
+ result.error = ::GetLastError();
+ return result;
+}
+
+void cancelRemoteDevicesDiscovery(HBLUETOOTH_DEVICE_FIND hSearch)
+{
+ if (hSearch)
+ ::BluetoothFindDeviceClose(hSearch);
+}
+
+} // namespace WinClassicBluetooth
+
+QT_END_NAMESPACE
diff --git a/src/bluetooth/windows/qwinclassicbluetooth_p.h b/src/bluetooth/windows/qwinclassicbluetooth_p.h
new file mode 100644
index 00000000..9c0955bf
--- /dev/null
+++ b/src/bluetooth/windows/qwinclassicbluetooth_p.h
@@ -0,0 +1,77 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2014 Denis Shienkov <denis.shienkov@gmail.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtBluetooth module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL21$
+** 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 Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QWINCLASSICBLUETOOTH_P_H
+#define QWINCLASSICBLUETOOTH_P_H
+
+#include <QtCore/qlist.h>
+#include <QtCore/qmetatype.h>
+
+#include <qt_windows.h>
+#include <setupapi.h>
+#include <bluetoothapis.h>
+
+QT_BEGIN_NAMESPACE
+
+namespace WinClassicBluetooth {
+
+struct LocalRadiosDiscoveryResult
+{
+ LocalRadiosDiscoveryResult();
+ QList<BLUETOOTH_RADIO_INFO> radios;
+ DWORD error;
+};
+
+struct RemoteDeviceDiscoveryResult
+{
+ RemoteDeviceDiscoveryResult();
+ BLUETOOTH_DEVICE_INFO device;
+ HBLUETOOTH_DEVICE_FIND hSearch;
+ DWORD error;
+};
+
+LocalRadiosDiscoveryResult enumerateLocalRadios();
+
+RemoteDeviceDiscoveryResult startDiscoveryOfFirstRemoteDevice();
+RemoteDeviceDiscoveryResult startDiscoveryOfNextRemoteDevice(HBLUETOOTH_DEVICE_FIND hSearch);
+void cancelRemoteDevicesDiscovery(HBLUETOOTH_DEVICE_FIND hSearch);
+
+} // namespace WinClassicBluetooth
+
+QT_END_NAMESPACE
+
+Q_DECLARE_METATYPE(WinClassicBluetooth::LocalRadiosDiscoveryResult)
+Q_DECLARE_METATYPE(WinClassicBluetooth::RemoteDeviceDiscoveryResult)
+
+#endif // QWINCLASSICBLUETOOTH_P_H
diff --git a/src/bluetooth/windows/windows.pri b/src/bluetooth/windows/windows.pri
new file mode 100644
index 00000000..f08e0497
--- /dev/null
+++ b/src/bluetooth/windows/windows.pri
@@ -0,0 +1,5 @@
+PRIVATE_HEADERS += \
+ windows/qwinclassicbluetooth_p.h
+
+SOURCES += \
+ windows/qwinclassicbluetooth.cpp