Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
btdevicepair.mm
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "btutility_p.h"
6
7#include <QtCore/qloggingcategory.h>
8#include <QtCore/qdebug.h>
9
10QT_BEGIN_NAMESPACE
11
12namespace DarwinBluetooth {
13
15{
16 if (address.isNull())
17 return {};
18
19 const BluetoothDeviceAddress &iobtAddress = iobluetooth_address(address);
20 ObjCStrongReference<IOBluetoothDevice> res([IOBluetoothDevice deviceWithAddress:&iobtAddress], RetainPolicy::doInitialRetain);
21 return res;
22}
23
24} // namespace DarwinBluetooth
25
26QT_END_NAMESPACE
27
28QT_USE_NAMESPACE
29
30@implementation DarwinBTClassicPairing
31{
32 QT_PREPEND_NAMESPACE(QBluetoothAddress) m_targetAddress;
33
34 bool m_active;
35 IOBluetoothDevicePair *m_pairing; // The real pairing request
36 QT_PREPEND_NAMESPACE(DarwinBluetooth)::PairingDelegate *m_object;
37}
38
39- (id)initWithTarget:(const QBluetoothAddress &)address
40 delegate:(DarwinBluetooth::PairingDelegate *)object
41{
42 if (self = [super init]) {
43 Q_ASSERT_X(!address.isNull(), Q_FUNC_INFO, "invalid target address");
44 Q_ASSERT_X(object, Q_FUNC_INFO, "invalid delegate (null)");
45
46 m_targetAddress = address;
47 m_object = object;
48 m_active = false;
49 }
50
51 return self;
52}
53
54- (void)dealloc
55{
56 [m_pairing stop];
57 [m_pairing release];
58 [super dealloc];
59}
60
61- (IOReturn) start
62{
63 if (m_active)
64 return kIOReturnBusy;
65
66 Q_ASSERT_X(!m_targetAddress.isNull(), Q_FUNC_INFO, "invalid target address");
67
69
70 const BluetoothDeviceAddress &iobtAddress = DarwinBluetooth::iobluetooth_address(m_targetAddress);
71 // Device is autoreleased.
72 IOBluetoothDevice *const device = [IOBluetoothDevice deviceWithAddress:&iobtAddress];
73 if (!device) {
74 qCCritical(QT_BT_DARWIN) << "failed to create a device to pair with";
75 return kIOReturnError;
76 }
77
78 m_pairing = [[IOBluetoothDevicePair pairWithDevice:device] retain];
79 if (!m_pairing) {
80 qCCritical(QT_BT_DARWIN) << "failed to create a device pair";
81 return kIOReturnError;
82 }
83
84 [m_pairing setDelegate:self];
85 const IOReturn result = [m_pairing start];
86 if (result != kIOReturnSuccess) {
87 [m_pairing release];
88 m_pairing = nil;
89 } else
90 m_active = true;
91
92 return result;
93}
94
95- (bool)isActive
96{
97 return m_active;
98}
99
100- (void)stop
101{
102 // stop: stops pairing, removes the delegate
103 // and disconnects if device was connected.
104 if (m_pairing)
105 [m_pairing stop];
106}
107
108- (const QBluetoothAddress &)targetAddress
109{
110 return m_targetAddress;
111}
112
113- (IOBluetoothDevicePair *)pairingRequest
114{
115 return [[m_pairing retain] autorelease];
116}
117
118- (IOBluetoothDevice *)targetDevice
119{
120 return [m_pairing device];//It's retained/autoreleased by pair.
121}
122
123// IOBluetoothDevicePairDelegate:
124
125- (void)devicePairingStarted:(id)sender
126{
127 Q_UNUSED(sender);
128}
129
130- (void)devicePairingConnecting:(id)sender
131{
132 Q_UNUSED(sender);
133}
134
135- (void)deviceParingPINCodeRequest:(id)sender
136{
137 Q_UNUSED(sender);
138}
139
140- (void)devicePairingUserConfirmationRequest:(id)sender
141 numericValue:(BluetoothNumericValue)numericValue
142{
143 if (sender != m_pairing) // Can never happen.
144 return;
145
146 Q_ASSERT_X(m_object, Q_FUNC_INFO, "invalid delegate (null)");
147
148 m_object->requestUserConfirmation(self, numericValue);
149}
150
151- (void)devicePairingUserPasskeyNotification:(id)sender
152 passkey:(BluetoothPasskey)passkey
153{
154 Q_UNUSED(sender);
155 Q_UNUSED(passkey);
156}
157
158- (void)devicePairingFinished:(id)sender error:(IOReturn)error
159{
160 Q_ASSERT_X(m_object, Q_FUNC_INFO, "invalid delegate (null)");
161
162 if (sender != m_pairing) // Can never happen though.
163 return;
164
165 m_active = false;
166 if (error != kIOReturnSuccess)
167 m_object->error(self, error);
168 else
169 m_object->pairingFinished(self);
170}
171
172- (void)deviceSimplePairingComplete:(id)sender
173 status:(BluetoothHCIEventStatus)status
174{
175 Q_UNUSED(sender);
176 Q_UNUSED(status);
177}
178
179@end
#define QT_BT_MAC_AUTORELEASEPOOL
Definition btutility_p.h:78
ObjCStrongReference< IOBluetoothDevice > device_with_address(const QBluetoothAddress &address)