summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/windows
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2014-11-14 20:02:16 +0300
committerDenis Shienkov <denis.shienkov@gmail.com>2014-12-04 16:10:46 +0100
commit02b415fe5d538645e95896eb70c9d38628388841 (patch)
tree25d3fb823590559604fa2d3750bae7846299dac7 /src/bluetooth/windows
parent3fa9606bc804f1bb81b45d04142dc2d9db6717bd (diff)
Add discovering for BLE devices on Windows
BLE devices are supported in Windows 8 and above. Windows has not public API to discovering/pairing of BLE devices. A user shall do it by means of standard "bluetooth" application which are inbox into Windows. Only after that there is an opportunity to display the discovered devices. Change-Id: Idd3d2949456a32c8c333744205755853aef80422 Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'src/bluetooth/windows')
-rw-r--r--src/bluetooth/windows/qwinlowenergybluetooth.cpp264
-rw-r--r--src/bluetooth/windows/qwinlowenergybluetooth_p.h74
-rw-r--r--src/bluetooth/windows/windows.pri6
3 files changed, 342 insertions, 2 deletions
diff --git a/src/bluetooth/windows/qwinlowenergybluetooth.cpp b/src/bluetooth/windows/qwinlowenergybluetooth.cpp
new file mode 100644
index 00000000..f4f34e42
--- /dev/null
+++ b/src/bluetooth/windows/qwinlowenergybluetooth.cpp
@@ -0,0 +1,264 @@
+/****************************************************************************
+**
+** 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 "qwinlowenergybluetooth_p.h"
+
+#include <QtCore/quuid.h>
+
+QT_BEGIN_NAMESPACE
+
+namespace WinLowEnergyBluetooth {
+
+static QString deviceRegistryProperty(
+ HDEVINFO deviceInfoHandle,
+ const PSP_DEVINFO_DATA deviceInfoData,
+ DWORD registryProperty)
+{
+ DWORD propertyRegDataType = 0;
+ DWORD propertyBufferSize = 0;
+ QByteArray propertyBuffer;
+
+ while (!::SetupDiGetDeviceRegistryProperty(
+ deviceInfoHandle,
+ deviceInfoData,
+ registryProperty,
+ &propertyRegDataType,
+ reinterpret_cast<PBYTE>(propertyBuffer.data()),
+ propertyBuffer.size(),
+ &propertyBufferSize)) {
+
+ const DWORD error = ::GetLastError();
+
+ if (error != ERROR_INSUFFICIENT_BUFFER
+ || (propertyRegDataType != REG_SZ
+ && propertyRegDataType != REG_EXPAND_SZ)) {
+ return QString();
+ }
+
+ propertyBuffer.fill(0, propertyBufferSize * sizeof(WCHAR));
+ }
+
+ return QString::fromWCharArray(
+ reinterpret_cast<const wchar_t *>(propertyBuffer.constData()));
+}
+
+static QString deviceFriendlyName(
+ HDEVINFO deviceInfoSet, PSP_DEVINFO_DATA deviceInfoData)
+{
+ return deviceRegistryProperty(
+ deviceInfoSet, deviceInfoData, SPDRP_FRIENDLYNAME);
+}
+
+static QString deviceService(
+ HDEVINFO deviceInfoSet, PSP_DEVINFO_DATA deviceInfoData)
+{
+ return deviceRegistryProperty(
+ deviceInfoSet, deviceInfoData, SPDRP_SERVICE);
+}
+
+static QString deviceSystemPath(
+ const PSP_INTERFACE_DEVICE_DETAIL_DATA detailData)
+{
+ return QString::fromWCharArray(detailData->DevicePath);
+}
+
+static QBluetoothAddress parseRemoteAddress(const QString &devicePath)
+{
+ const int firstbound = devicePath.indexOf(QStringLiteral("dev_"));
+ const int lastbound = devicePath.indexOf(QLatin1Char('#'), firstbound);
+
+ const QString hex =
+ devicePath.mid(firstbound + 4, lastbound - firstbound - 4);
+
+ bool ok = false;
+ return QBluetoothAddress(hex.toULongLong(&ok, 16));
+}
+
+DeviceInfo::DeviceInfo(
+ const QBluetoothAddress &address,
+ const QString &name,
+ const QString &systemPath)
+ : address(address)
+ , name(name)
+ , systemPath(systemPath)
+{
+}
+
+DeviceDiscoveryResult::DeviceDiscoveryResult()
+ : error(NO_ERROR)
+{
+}
+
+static DeviceDiscoveryResult availableSystemInterfaces(
+ const GUID &deviceInterface)
+{
+ const DWORD flags = DIGCF_PRESENT | DIGCF_DEVICEINTERFACE;
+ const HDEVINFO deviceInfoHandle = ::SetupDiGetClassDevs(
+ &deviceInterface, 0, 0, flags);
+
+ DeviceDiscoveryResult result;
+
+ if (deviceInfoHandle == INVALID_HANDLE_VALUE) {
+ result.error = ::GetLastError();
+ return result;
+ }
+
+ DWORD index = 0;
+
+ forever {
+
+ SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
+ ::ZeroMemory(&deviceInterfaceData, sizeof(deviceInterfaceData));
+ deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);
+
+ if (!::SetupDiEnumDeviceInterfaces(
+ deviceInfoHandle,
+ NULL,
+ &deviceInterface,
+ index++,
+ &deviceInterfaceData)) {
+
+ result.error = ::GetLastError();
+ break;
+ }
+
+ DWORD deviceInterfaceDetailDataSize = 0;
+ if (!::SetupDiGetDeviceInterfaceDetail(
+ deviceInfoHandle,
+ &deviceInterfaceData,
+ NULL,
+ deviceInterfaceDetailDataSize,
+ &deviceInterfaceDetailDataSize,
+ NULL)) {
+
+ const DWORD error = ::GetLastError();
+ if (error != ERROR_INSUFFICIENT_BUFFER) {
+ result.error = ::GetLastError();
+
+ break;
+ }
+ }
+
+ SP_DEVINFO_DATA deviceInfoData;
+ ::ZeroMemory(&deviceInfoData, sizeof(deviceInfoData));
+ deviceInfoData.cbSize = sizeof(deviceInfoData);
+
+ QByteArray deviceInterfaceDetailDataBuffer(
+ deviceInterfaceDetailDataSize, 0);
+
+ PSP_INTERFACE_DEVICE_DETAIL_DATA deviceInterfaceDetailData =
+ reinterpret_cast<PSP_INTERFACE_DEVICE_DETAIL_DATA>
+ (deviceInterfaceDetailDataBuffer.data());
+
+ deviceInterfaceDetailData->cbSize =
+ sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
+
+ if (!::SetupDiGetDeviceInterfaceDetail(
+ deviceInfoHandle,
+ &deviceInterfaceData,
+ deviceInterfaceDetailData,
+ deviceInterfaceDetailDataBuffer.size(),
+ &deviceInterfaceDetailDataSize,
+ &deviceInfoData)) {
+ result.error = ::GetLastError();
+ break;
+ }
+
+ const QString systemPath =
+ deviceSystemPath(deviceInterfaceDetailData);
+
+ const QBluetoothAddress address = parseRemoteAddress(systemPath);
+ if (address.isNull())
+ continue;
+
+ const QString friendlyName =
+ deviceFriendlyName(deviceInfoHandle, &deviceInfoData);
+
+ const DeviceInfo info(address, friendlyName, systemPath);
+ result.devices.append(info);
+
+ }
+
+ ::SetupDiDestroyDeviceInfoList(deviceInfoHandle);
+ return result;
+}
+
+static QStringList availableSystemServices(const GUID &deviceInterface)
+{
+ const DWORD flags = DIGCF_PRESENT;
+ const HDEVINFO deviceInfoHandle = ::SetupDiGetClassDevs(
+ &deviceInterface, NULL, 0, flags);
+
+ if (deviceInfoHandle == INVALID_HANDLE_VALUE)
+ return QStringList();
+
+ SP_DEVINFO_DATA deviceInfoData;
+ ::memset(&deviceInfoData, 0, sizeof(deviceInfoData));
+ deviceInfoData.cbSize = sizeof(deviceInfoData);
+
+ DWORD index = 0;
+ QStringList result;
+
+ while (::SetupDiEnumDeviceInfo(
+ deviceInfoHandle, index++, &deviceInfoData)) {
+
+ const QString service = deviceService(deviceInfoHandle, &deviceInfoData);
+ if (service.isEmpty())
+ continue;
+
+ result.append(service);
+ }
+
+ ::SetupDiDestroyDeviceInfoList(deviceInfoHandle);
+ return result;
+}
+
+DeviceDiscoveryResult startDiscoveryOfRemoteDevices()
+{
+ return availableSystemInterfaces(
+ QUuid("781aee18-7733-4ce4-add0-91f41c67b592"));
+}
+
+bool hasLocalRadio()
+{
+ const QStringList systemServices =
+ availableSystemServices(
+ QUuid("e0cbf06c-cd8b-4647-bb8a-263b43f0f974"));
+
+ return systemServices.contains(QStringLiteral("BthLEEnum"));
+}
+
+} // namespace WinLowEnergyBluetooth
+
+QT_END_NAMESPACE
diff --git a/src/bluetooth/windows/qwinlowenergybluetooth_p.h b/src/bluetooth/windows/qwinlowenergybluetooth_p.h
new file mode 100644
index 00000000..aa9127a4
--- /dev/null
+++ b/src/bluetooth/windows/qwinlowenergybluetooth_p.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** 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 QWINLOWENERGYBLUETOOTH_P_H
+#define QWINLOWENERGYBLUETOOTH_P_H
+
+#include <QtCore/qstringlist.h>
+
+#include <QtBluetooth/qbluetoothaddress.h>
+
+#include <qt_windows.h>
+#include <setupapi.h>
+
+QT_BEGIN_NAMESPACE
+
+namespace WinLowEnergyBluetooth {
+
+struct DeviceInfo
+{
+ DeviceInfo(const QBluetoothAddress &address,
+ const QString &name,
+ const QString &systemPath);
+ QBluetoothAddress address;
+ QString name;
+ QString systemPath;
+};
+
+struct DeviceDiscoveryResult
+{
+ DeviceDiscoveryResult();
+ QList<DeviceInfo> devices;
+ DWORD error;
+};
+
+bool hasLocalRadio();
+
+DeviceDiscoveryResult startDiscoveryOfRemoteDevices();
+
+} // namespace WinLowEnergyBluetooth
+
+QT_END_NAMESPACE
+
+#endif // QWINLOWENERGYBLUETOOTH_P_H
diff --git a/src/bluetooth/windows/windows.pri b/src/bluetooth/windows/windows.pri
index f08e0497..84e7200a 100644
--- a/src/bluetooth/windows/windows.pri
+++ b/src/bluetooth/windows/windows.pri
@@ -1,5 +1,7 @@
PRIVATE_HEADERS += \
- windows/qwinclassicbluetooth_p.h
+ windows/qwinclassicbluetooth_p.h \
+ windows/qwinlowenergybluetooth_p.h
SOURCES += \
- windows/qwinclassicbluetooth.cpp
+ windows/qwinclassicbluetooth.cpp \
+ windows/qwinlowenergybluetooth.cpp