summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/osx
diff options
context:
space:
mode:
authorTimur Pocheptsov <Timur.Pocheptsov@digia.com>2014-10-29 10:15:54 +0100
committerTimur Pocheptsov <Timur.Pocheptsov@digia.com>2014-11-10 10:01:52 +0100
commite0dc61db4af89cce14ade3582ac92e4b713c7982 (patch)
treedbdeffaf2648da1532144367953ce0d72ff86d50 /src/bluetooth/osx
parentf1372ef9a1c4ad5bb7c7bbd94116ec1203fc4243 (diff)
Bluetooth - device (LE) discovery for iOS
Add a QBluetoothDeviceDiscoveryAgent for iOS, implementation is based on Core Bluetooth framework. Low Energy devices scan only. - Ctor with address: set error as invalid bluetooth adapter error, Core Bluetooth framework does not provide access to the local adapter's address. - Adjust a test - we do not have access to the 'local device' information on iOS (no address, no number of devices etc.) - skip the last test in tst_deviceDiscovery. Change-Id: I49080d021c340016aebc548cc8ed758777c66397 Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'src/bluetooth/osx')
-rw-r--r--src/bluetooth/osx/osxbtcentralmanagerdelegate_p.h4
-rw-r--r--src/bluetooth/osx/osxbtledeviceinquiry.mm2
-rw-r--r--src/bluetooth/osx/osxbtledeviceinquiry_p.h5
-rw-r--r--src/bluetooth/osx/osxbtutility.mm45
-rw-r--r--src/bluetooth/osx/osxbtutility_p.h110
5 files changed, 152 insertions, 14 deletions
diff --git a/src/bluetooth/osx/osxbtcentralmanagerdelegate_p.h b/src/bluetooth/osx/osxbtcentralmanagerdelegate_p.h
index cb8a70d3..65b5e61f 100644
--- a/src/bluetooth/osx/osxbtcentralmanagerdelegate_p.h
+++ b/src/bluetooth/osx/osxbtcentralmanagerdelegate_p.h
@@ -41,7 +41,9 @@
#include <QtCore/qglobal.h>
-#import <Foundation/Foundation.h>
+// Foundation _MUST_ be included before 'wrapper' -
+// SDK 10.9 is broken and requires this trick.
+#include <Foundation/Foundation.h>
#include "corebluetoothwrapper_p.h"
diff --git a/src/bluetooth/osx/osxbtledeviceinquiry.mm b/src/bluetooth/osx/osxbtledeviceinquiry.mm
index 60e3224d..2f5ec0bb 100644
--- a/src/bluetooth/osx/osxbtledeviceinquiry.mm
+++ b/src/bluetooth/osx/osxbtledeviceinquiry.mm
@@ -48,6 +48,8 @@
#include <QtCore/qloggingcategory.h>
#include <QtCore/qdebug.h>
+// Foundation header is already included by this point
+// (a workaround for a broken 10.9 SDK).
#include "corebluetoothwrapper_p.h"
QT_BEGIN_NAMESPACE
diff --git a/src/bluetooth/osx/osxbtledeviceinquiry_p.h b/src/bluetooth/osx/osxbtledeviceinquiry_p.h
index 16a757a6..20004d30 100644
--- a/src/bluetooth/osx/osxbtledeviceinquiry_p.h
+++ b/src/bluetooth/osx/osxbtledeviceinquiry_p.h
@@ -47,7 +47,10 @@
#include <QtCore/qglobal.h>
#include <QtCore/qlist.h>
-#import <Foundation/Foundation.h>
+// The Foundation header must be included before
+// corebluetoothwrapper_p.h - a workaround for a broken
+// 10.9 SDK.
+#include <Foundation/Foundation.h>
@class QT_MANGLE_NAMESPACE(OSXBTLEDeviceInquiry);
diff --git a/src/bluetooth/osx/osxbtutility.mm b/src/bluetooth/osx/osxbtutility.mm
index b6818ef6..74ee84e6 100644
--- a/src/bluetooth/osx/osxbtutility.mm
+++ b/src/bluetooth/osx/osxbtutility.mm
@@ -159,6 +159,51 @@ QString qt_error_string(IOReturn errorCode)
#endif
+
+// Apple has: CBUUID, NSUUID, CFUUID, IOBluetoothSDPUUID
+// and it's handy to have several converters:
+
+QBluetoothUuid qt_uuid(CBUUID *uuid)
+{
+ if (!uuid)
+ return QBluetoothUuid();
+
+ QT_BT_MAC_AUTORELEASEPOOL;
+
+ if (uuid.data.length != 16) // TODO: warning?
+ return QBluetoothUuid();
+
+ quint128 qtUuidData = {};
+ const quint8 *const source = static_cast<const quint8 *>(uuid.data.bytes);
+ std::copy(source, source + 16, qtUuidData.data);
+
+ return QBluetoothUuid(qtUuidData);
+}
+
+CFStrongReference<CFUUIDRef> cf_uuid(const QBluetoothUuid &qtUuid)
+{
+ const quint128 qtUuidData = qtUuid.toUInt128();
+ const quint8 *const data = qtUuidData.data;
+
+ CFUUIDBytes bytes = {data[0], data[1], data[2], data[3],
+ data[4], data[5], data[6], data[7],
+ data[8], data[9], data[10], data[11],
+ data[12], data[13], data[14], data[15]};
+
+ CFUUIDRef cfUuid = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, bytes);
+ return CFStrongReference<CFUUIDRef>(cfUuid, false);// false == already retained.
+}
+
+ObjCStrongReference<CBUUID> cb_uuid(const QBluetoothUuid &qtUuid)
+{
+ CFStrongReference<CFUUIDRef> cfUuid(cf_uuid(qtUuid));
+ if (!cfUuid)
+ return ObjCStrongReference<CBUUID>();
+
+ ObjCStrongReference<CBUUID> cbUuid([CBUUID UUIDWithCFUUID:cfUuid], true); //true == retain.
+ return cbUuid;
+}
+
}
QT_END_NAMESPACE
diff --git a/src/bluetooth/osx/osxbtutility_p.h b/src/bluetooth/osx/osxbtutility_p.h
index c2a48e5f..8c4083d9 100644
--- a/src/bluetooth/osx/osxbtutility_p.h
+++ b/src/bluetooth/osx/osxbtutility_p.h
@@ -47,19 +47,10 @@
#include <QtCore/qglobal.h>
#include <Foundation/Foundation.h>
+// Only after Foundation.h!
+#include "corebluetoothwrapper_p.h"
-#ifndef QT_IOS_BLUETOOTH
-
-#include <IOBluetooth/Bluetooth.h>
-#include <IOKit/IOReturn.h>
-
-@class IOBluetoothSDPUUID;
-
-#else
-
-#include <CoreBluetooth/CoreBluetooth.h>
-
-#endif
+@class CBUUID;
QT_BEGIN_NAMESPACE
@@ -179,6 +170,97 @@ private:
T *m_ptr;
};
+// The type 'T' is some XXXRef from CoreFoundation and co.
+// In principle, we can do a trick removing a pointer from a type
+// when template is instantiated, but it's quite a lot of ugly pp-tokens
+// like OSXBluetooth::CFStrongReference<OSXBluetooth::remove_pointer<CFUUIDRref> > strongReference;
+// so instead we use 'T' everywhere, not 'T *' as can expected
+// from a smart pointer.
+template<class T>
+class CFStrongReference {
+public:
+ CFStrongReference()
+ : m_ptr(Q_NULLPTR)
+ {
+ }
+
+ CFStrongReference(T obj, bool retain)
+ : m_ptr(obj)
+ {
+ if (m_ptr && retain)
+ CFRetain(m_ptr);
+ }
+
+ CFStrongReference(const CFStrongReference &rhs)
+ {
+ if ((m_ptr = rhs.m_ptr))
+ CFRetain(m_ptr);
+ }
+
+ CFStrongReference &operator = (const CFStrongReference &rhs)
+ {
+ // "Old-style" implementation:
+ if (this != &rhs && m_ptr != rhs.m_ptr) {
+ if (m_ptr)
+ CFRelease(m_ptr);
+ if ((m_ptr = rhs.m_ptr))
+ CFRetain(m_ptr);
+ }
+
+ return *this;
+ }
+
+#ifdef Q_COMPILER_RVALUE_REFS
+ CFStrongReference(CFStrongReference &&xval)
+ {
+ m_ptr = xval.m_ptr;
+ xval.m_ptr = Q_NULLPTR;
+ }
+
+ CFStrongReference &operator = (CFStrongReference &&xval)
+ {
+ m_ptr = xval.m_ptr;
+ xval.m_ptr = Q_NULLPTR;
+ return *this;
+ }
+#endif
+
+ ~CFStrongReference()
+ {
+ if (m_ptr)
+ CFRelease(m_ptr);
+ }
+
+ void reset(T newVal)
+ {
+ if (m_ptr != newVal) {
+ if (m_ptr)
+ CFRelease(m_ptr);
+ if ((m_ptr = newVal))
+ CFRetain(m_ptr);
+ }
+ }
+
+ operator T() const
+ {
+ return m_ptr;
+ }
+
+ T data() const
+ {
+ return m_ptr;
+ }
+
+ T take()
+ {
+ T p = m_ptr;
+ m_ptr = Q_NULLPTR;
+ return p;
+ }
+private:
+ T m_ptr;
+};
+
QString qt_address(NSString *address);
#ifndef QT_IOS_BLUETOOTH
@@ -192,6 +274,10 @@ QString qt_error_string(IOReturn errorCode);
#endif
+QBluetoothUuid qt_uuid(CBUUID *uuid);
+CFStrongReference<CFUUIDRef> cf_uuid(const QBluetoothUuid &qtUuid);
+ObjCStrongReference<CBUUID> cb_uuid(const QBluetoothUuid &qtUuid);
+
} // namespace OSXBluetooth
// Logging category for both OS X and iOS.