7#include <QtCore/qloggingcategory.h>
8#include <QtCore/qtimer.h>
9#include <QtCore/qdebug.h>
15const uint8_t IOBlueoothInquiryLengthS = 15;
17@implementation DarwinBTClassicDeviceInquiry
19 IOBluetoothDeviceInquiry *m_inquiry;
21 DarwinBluetooth::DeviceInquiryDelegate *m_delegate;
23 std::unique_ptr<QTimer> watchDog;
26- (
id)initWithDelegate:(DarwinBluetooth::DeviceInquiryDelegate *)delegate
28 if (self = [super init]) {
29 Q_ASSERT_X(delegate, Q_FUNC_INFO,
"invalid device inquiry delegate (null)");
31 m_inquiry = [[IOBluetoothDeviceInquiry inquiryWithDelegate:self] retain];
40 [m_inquiry setInquiryLength:IOBlueoothInquiryLengthS];
41 [m_inquiry setUpdateNewDeviceNames:NO];
42 m_delegate = delegate;
44 qCCritical(QT_BT_DARWIN) <<
"failed to create a device inquiry";
56 [m_inquiry setDelegate:nil];
72 return kIOReturnNoPower;
78 [m_inquiry clearFoundDevices];
80 qCDebug(QT_BT_DARWIN) <<
"Starting device inquiry with"
81 << IOBlueoothInquiryLengthS <<
"second timeout limit.";
82 const IOReturn result = [m_inquiry start];
83 if (result != kIOReturnSuccess) {
86 qCWarning(QT_BT_DARWIN) <<
"device inquiry start failed with IOKit error code:" << result;
91 watchDog.reset(
new QTimer);
92 watchDog->connect(watchDog.get(), &QTimer::timeout, watchDog.get(), [self]{
93 qCWarning(QT_BT_DARWIN,
"Manually interrupting IOBluetoothDeviceInquiry");
94 qCDebug(QT_BT_DARWIN) <<
"Found devices:" << [m_inquiry foundDevices];
98 watchDog->setSingleShot(
true);
100 watchDog->setInterval((IOBlueoothInquiryLengthS + 2) * 1000);
110 return kIOReturnSuccess;
112 Q_ASSERT_X(m_inquiry, Q_FUNC_INFO,
"active but nil inquiry");
114 return [m_inquiry stop];
117- (
void)deviceInquiryComplete:(IOBluetoothDeviceInquiry *)sender
118 error:(IOReturn)error aborted:(BOOL)aborted
120 qCDebug(QT_BT_DARWIN) <<
"deviceInquiryComplete, error:" << error
121 <<
"user-stopped:" << aborted;
125 if (sender != m_inquiry)
128 Q_ASSERT_X(m_delegate, Q_FUNC_INFO,
"invalid device inquiry delegate (null)");
130 if (error != kIOReturnSuccess && !aborted) {
133 qCWarning(QT_BT_DARWIN) <<
"IOKit error code: " << error;
141 m_delegate->inquiryFinished();
145- (
void)deviceInquiryDeviceFound:(IOBluetoothDeviceInquiry *)sender
146 device:(IOBluetoothDevice *)device
148 qCDebug(QT_BT_DARWIN) <<
"deviceInquiryDeviceFound:" << [device nameOrAddress];
149 if (sender != m_inquiry)
154 qCWarning(QT_BT_DARWIN,
"IOBluetooth device found after inquiry complete/interrupted");
158 Q_ASSERT_X(m_delegate, Q_FUNC_INFO,
"invalid device inquiry delegate (null)");
159 m_delegate->classicDeviceFound(device);
162- (
void)deviceInquiryStarted:(IOBluetoothDeviceInquiry *)sender