summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/qbluetoothlocaldevice_win.cpp
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/bluetooth/qbluetoothlocaldevice_win.cpp
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/bluetooth/qbluetoothlocaldevice_win.cpp')
-rw-r--r--src/bluetooth/qbluetoothlocaldevice_win.cpp166
1 files changed, 63 insertions, 103 deletions
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