From 4bce1481c5cfbd402973c2a488b36499ed44af2e Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Wed, 11 Oct 2017 10:10:27 +0200 Subject: IOBluetooth: warn about incorrent thread/runloop IOBluetooth is heavily based on CFRunLoops. An attempt to use it on a thread, that does not properly run CFRunLoop results in callbacks never firing and thus QBluetooth classes never finishing their jobs, including: - device discovery - service discovery - RFCOMM/LCAP2 - Bluetooth server - Bluetooth socket etc. While we cannot fix the core problem until we have a properly working CoreFoundation event dispatcher, we can at least issue a warning so that people do not waste their time debugging this well-known limitation. Task-number: QTBUG-63630 Change-Id: Iefa4d675ea0962167bdfede640d2087dbdf37b18 Reviewed-by: Alex Blasche --- src/bluetooth/osx/osxbtutility.mm | 15 ++++++++++++++- src/bluetooth/osx/osxbtutility_p.h | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'src/bluetooth/osx') diff --git a/src/bluetooth/osx/osxbtutility.mm b/src/bluetooth/osx/osxbtutility.mm index 1508c89f..e17006de 100644 --- a/src/bluetooth/osx/osxbtutility.mm +++ b/src/bluetooth/osx/osxbtutility.mm @@ -48,6 +48,7 @@ #ifndef QT_IOS_BLUETOOTH #import +#import #if QT_MAC_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_12, __IPHONE_NA) #import #endif @@ -164,7 +165,19 @@ QString qt_error_string(IOReturn errorCode) } } -#endif +void qt_test_iobluetooth_runloop() +{ + // IOBluetooth heavily relies on a CFRunLoop machinery in a way it dispatches + // its callbacks. Technically, having a QThread with CFRunLoop-based event + // dispatcher would suffice. At the moment of writing we do not have such + // event dispatcher, so we only can work on the main thread. + if (CFRunLoopGetMain() != CFRunLoopGetCurrent()) { + qCWarning(QT_BT_OSX) << "IOBluetooth works only on the main thread or a" + << "thread with a running CFRunLoop"; + } +} + +#endif // !QT_IOS_BLUETOOTH // Apple has: CBUUID, NSUUID, CFUUID, IOBluetoothSDPUUID diff --git a/src/bluetooth/osx/osxbtutility_p.h b/src/bluetooth/osx/osxbtutility_p.h index 148ebc0b..5ded5e7f 100644 --- a/src/bluetooth/osx/osxbtutility_p.h +++ b/src/bluetooth/osx/osxbtutility_p.h @@ -287,8 +287,9 @@ BluetoothDeviceAddress iobluetooth_address(const QBluetoothAddress &address); ObjCStrongReference iobluetooth_uuid(const QBluetoothUuid &uuid); QBluetoothUuid qt_uuid(IOBluetoothSDPUUID *uuid); QString qt_error_string(IOReturn errorCode); +void qt_test_iobluetooth_runloop(); -#endif +#endif // !QT_IOS_BLUETOOTH QBluetoothUuid qt_uuid(CBUUID *uuid); CFStrongReference cf_uuid(const QBluetoothUuid &qtUuid); -- cgit v1.2.3